Commit bcc2509a authored by Joris Vankerschaver's avatar Joris Vankerschaver

Implement repr() for file objects

parent 32254d13
...@@ -213,7 +213,7 @@ Box* open(Box* arg1, Box* arg2) { ...@@ -213,7 +213,7 @@ Box* open(Box* arg1, Box* arg2) {
if (!f) if (!f)
raiseExcHelper(IOError, "%s: '%s' '%s'", strerror(errno), fn.c_str()); raiseExcHelper(IOError, "%s: '%s' '%s'", strerror(errno), fn.c_str());
return new BoxedFile(f); return new BoxedFile(f, fn, mode);
} }
extern "C" Box* chr(Box* arg) { extern "C" Box* chr(Box* arg) {
......
...@@ -112,9 +112,9 @@ void setupSys() { ...@@ -112,9 +112,9 @@ void setupSys() {
sys_module->giveAttr("argv", new BoxedList()); sys_module->giveAttr("argv", new BoxedList());
sys_module->giveAttr("stdout", new BoxedFile(stdout)); sys_module->giveAttr("stdout", new BoxedFile(stdout, "<stdout>", "w"));
sys_module->giveAttr("stdin", new BoxedFile(stdin)); sys_module->giveAttr("stdin", new BoxedFile(stdin, "<stdin>", "r"));
sys_module->giveAttr("stderr", new BoxedFile(stderr)); sys_module->giveAttr("stderr", new BoxedFile(stderr, "<stderr>", "w"));
sys_module->giveAttr("warnoptions", new BoxedList()); sys_module->giveAttr("warnoptions", new BoxedList());
sys_module->giveAttr("py3kwarning", False); sys_module->giveAttr("py3kwarning", False);
......
...@@ -26,7 +26,14 @@ namespace pyston { ...@@ -26,7 +26,14 @@ namespace pyston {
Box* fileRepr(BoxedFile* self) { Box* fileRepr(BoxedFile* self) {
assert(self->cls == file_cls); assert(self->cls == file_cls);
RELEASE_ASSERT(0, "");
void* addr = static_cast<void*>(self->f);
std::ostringstream repr;
repr << "<" << (self->closed ? "closed" : "open") << " file '" << self->fname << "', ";
repr << "mode '" << self->fmode << "' at " << addr << ">";
return boxString(repr.str());
} }
Box* fileRead(BoxedFile* self, Box* _size) { Box* fileRead(BoxedFile* self, Box* _size) {
......
...@@ -290,9 +290,12 @@ extern "C" BoxedTuple* EmptyTuple; ...@@ -290,9 +290,12 @@ extern "C" BoxedTuple* EmptyTuple;
class BoxedFile : public Box { class BoxedFile : public Box {
public: public:
FILE* f; FILE* f;
std::string fname;
std::string fmode;
bool closed; bool closed;
bool softspace; bool softspace;
BoxedFile(FILE* f) __attribute__((visibility("default"))) : Box(file_cls), f(f), closed(false), softspace(false) {} BoxedFile(FILE* f, std::string fname, std::string fmode) __attribute__((visibility("default")))
: Box(file_cls), f(f), fname(fname), fmode(fmode), closed(false), softspace(false) {}
}; };
struct PyHasher { struct PyHasher {
......
import sys
f = open("/dev/null") f = open("/dev/null")
print repr(f.read()) print repr(f.read())
...@@ -6,3 +8,23 @@ print repr(f2.read()) ...@@ -6,3 +8,23 @@ print repr(f2.read())
with open("/dev/null") as f3: with open("/dev/null") as f3:
print repr(f3.read()) print repr(f3.read())
# String representation of file objects.
def chop(s):
"""Chop off the last bit of a file object repr, which is the address
of the raw file pointer.
"""
return ' '.join(s.split()[:-1])
for desc in [sys.stderr, sys.stdout, sys.stdin]:
print chop(str(desc))
f = open("/dev/null", 'w')
print chop(str(f))
f.close()
print chop(str(f))
f = open("/dev/null", 'r')
print chop(str(f))
f.close()
print chop(str(f))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment