Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
bcc2509a
Commit
bcc2509a
authored
Oct 28, 2014
by
Joris Vankerschaver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement repr() for file objects
parent
32254d13
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
6 deletions
+38
-6
src/runtime/builtin_modules/builtins.cpp
src/runtime/builtin_modules/builtins.cpp
+1
-1
src/runtime/builtin_modules/sys.cpp
src/runtime/builtin_modules/sys.cpp
+3
-3
src/runtime/file.cpp
src/runtime/file.cpp
+8
-1
src/runtime/types.h
src/runtime/types.h
+4
-1
test/tests/file.py
test/tests/file.py
+22
-0
No files found.
src/runtime/builtin_modules/builtins.cpp
View file @
bcc2509a
...
...
@@ -213,7 +213,7 @@ Box* open(Box* arg1, Box* arg2) {
if
(
!
f
)
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
)
{
...
...
src/runtime/builtin_modules/sys.cpp
View file @
bcc2509a
...
...
@@ -112,9 +112,9 @@ void setupSys() {
sys_module
->
giveAttr
(
"argv"
,
new
BoxedList
());
sys_module
->
giveAttr
(
"stdout"
,
new
BoxedFile
(
stdout
));
sys_module
->
giveAttr
(
"stdin"
,
new
BoxedFile
(
stdin
));
sys_module
->
giveAttr
(
"stderr"
,
new
BoxedFile
(
stderr
));
sys_module
->
giveAttr
(
"stdout"
,
new
BoxedFile
(
stdout
,
"<stdout>"
,
"w"
));
sys_module
->
giveAttr
(
"stdin"
,
new
BoxedFile
(
stdin
,
"<stdin>"
,
"r"
));
sys_module
->
giveAttr
(
"stderr"
,
new
BoxedFile
(
stderr
,
"<stderr>"
,
"w"
));
sys_module
->
giveAttr
(
"warnoptions"
,
new
BoxedList
());
sys_module
->
giveAttr
(
"py3kwarning"
,
False
);
...
...
src/runtime/file.cpp
View file @
bcc2509a
...
...
@@ -26,7 +26,14 @@ namespace pyston {
Box
*
fileRepr
(
BoxedFile
*
self
)
{
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
)
{
...
...
src/runtime/types.h
View file @
bcc2509a
...
...
@@ -290,9 +290,12 @@ extern "C" BoxedTuple* EmptyTuple;
class
BoxedFile
:
public
Box
{
public:
FILE
*
f
;
std
::
string
fname
;
std
::
string
fmode
;
bool
closed
;
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
{
...
...
test/tests/file.py
View file @
bcc2509a
import
sys
f
=
open
(
"/dev/null"
)
print
repr
(
f
.
read
())
...
...
@@ -6,3 +8,23 @@ print repr(f2.read())
with
open
(
"/dev/null"
)
as
f3
:
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
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment