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
376e2b98
Commit
376e2b98
authored
Oct 29, 2014
by
Joris Vankerschaver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add iterator support for file objects
parent
bcc2509a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
src/runtime/file.cpp
src/runtime/file.cpp
+21
-0
test/tests/file.py
test/tests/file.py
+11
-0
No files found.
src/runtime/file.cpp
View file @
376e2b98
...
...
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include <cstring>
#include <sstream>
...
...
@@ -161,6 +162,22 @@ Box* fileNew(BoxedClass* cls, Box* s, Box* m) {
return
open
(
s
,
m
);
}
Box
*
fileIterNext
(
BoxedFile
*
s
)
{
return
fileReadline1
(
s
);
}
bool
fileEof
(
BoxedFile
*
self
)
{
char
ch
=
fgetc
(
self
->
f
);
ungetc
(
ch
,
self
->
f
);
return
feof
(
self
->
f
);
}
Box
*
fileIterHasNext
(
Box
*
s
)
{
assert
(
s
->
cls
==
file_cls
);
BoxedFile
*
self
=
static_cast
<
BoxedFile
*>
(
s
);
return
boxBool
(
!
fileEof
(
self
));
}
void
setupFile
()
{
file_cls
->
giveAttr
(
"__name__"
,
boxStrConstant
(
"file"
));
...
...
@@ -179,6 +196,10 @@ void setupFile() {
file_cls
->
giveAttr
(
"__enter__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
fileEnter
,
typeFromClass
(
file_cls
),
1
)));
file_cls
->
giveAttr
(
"__exit__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
fileExit
,
UNKNOWN
,
4
)));
file_cls
->
giveAttr
(
"__iter__"
,
file_cls
->
getattr
(
"__enter__"
));
file_cls
->
giveAttr
(
"__hasnext__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
fileIterHasNext
,
BOXED_BOOL
,
1
)));
file_cls
->
giveAttr
(
"next"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
fileIterNext
,
STR
,
1
)));
file_cls
->
giveAttr
(
"softspace"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
BYTE
,
offsetof
(
BoxedFile
,
softspace
)));
...
...
test/tests/file.py
View file @
376e2b98
...
...
@@ -28,3 +28,14 @@ f = open("/dev/null", 'r')
print
chop
(
str
(
f
))
f
.
close
()
print
chop
(
str
(
f
))
# Support for iteration protocol.
f
=
open
(
'/dev/null'
)
print
iter
(
f
)
is
f
f
.
close
()
with
open
(
'../README.md'
)
as
f
:
lines
=
list
(
f
)
print
lines
[:
5
]
print
lines
[
-
5
:]
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