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
b974b146
Commit
b974b146
authored
Apr 24, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simple implementations of file.readline(), map()
parent
0d5ba1e2
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
95 additions
and
17 deletions
+95
-17
src/runtime/builtin_modules/builtins.cpp
src/runtime/builtin_modules/builtins.cpp
+26
-0
src/runtime/file.cpp
src/runtime/file.cpp
+23
-0
src/runtime/str.cpp
src/runtime/str.cpp
+17
-0
test/tests/map.py
test/tests/map.py
+5
-0
test/tests/sys_stdout.py
test/tests/sys_stdout.py
+0
-17
test/tests/sys_stdout_changing.py
test/tests/sys_stdout_changing.py
+24
-0
No files found.
src/runtime/builtin_modules/builtins.cpp
View file @
b974b146
...
...
@@ -17,6 +17,8 @@
#include "core/ast.h"
#include "core/types.h"
#include "codegen/compvars.h"
#include "runtime/gc_runtime.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
...
...
@@ -217,6 +219,29 @@ Box* getattr3(Box* obj, Box* _str, Box* default_value) {
return
rtn
;
}
Box
*
map2
(
Box
*
f
,
Box
*
container
)
{
static
std
::
string
_iter
(
"__iter__"
);
static
std
::
string
_hasnext
(
"__hasnext__"
);
static
std
::
string
_next
(
"next"
);
Box
*
iter
=
callattr
(
container
,
&
_iter
,
true
,
0
,
NULL
,
NULL
,
NULL
,
NULL
);
Box
*
rtn
=
new
BoxedList
();
while
(
true
)
{
Box
*
hasnext
=
callattr
(
iter
,
&
_hasnext
,
true
,
0
,
NULL
,
NULL
,
NULL
,
NULL
);
bool
hasnext_bool
=
nonzero
(
hasnext
);
if
(
!
hasnext_bool
)
break
;
Box
*
next
=
callattr
(
iter
,
&
_next
,
true
,
0
,
NULL
,
NULL
,
NULL
,
NULL
);
Box
*
r
=
runtimeCall
(
f
,
1
,
next
,
NULL
,
NULL
,
NULL
);
listAppendInternal
(
rtn
,
r
);
}
return
rtn
;
}
extern
"C"
const
ObjectFlavor
notimplemented_flavor
(
&
boxGCHandler
,
NULL
);
BoxedClass
*
notimplemented_cls
;
BoxedModule
*
builtins_module
;
...
...
@@ -279,6 +304,7 @@ void setupBuiltins() {
open_obj
=
new
BoxedFunction
(
open
);
builtins_module
->
giveAttr
(
"open"
,
open_obj
);
builtins_module
->
giveAttr
(
"map"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
map2
,
LIST
,
2
,
false
)));
builtins_module
->
setattr
(
"str"
,
str_cls
,
NULL
,
NULL
);
builtins_module
->
setattr
(
"int"
,
int_cls
,
NULL
,
NULL
);
...
...
src/runtime/file.cpp
View file @
b974b146
...
...
@@ -15,6 +15,8 @@
#include <cstring>
#include <sstream>
#include "codegen/compvars.h"
#include "core/common.h"
#include "core/stats.h"
#include "core/types.h"
...
...
@@ -64,6 +66,24 @@ Box* fileRead1(BoxedFile* self) {
return
_fileRead
(
self
,
-
1
);
}
Box
*
fileReadline1
(
BoxedFile
*
self
)
{
assert
(
self
->
cls
==
file_cls
);
std
::
ostringstream
os
(
""
);
while
(
true
)
{
char
c
;
int
nread
=
fread
(
&
c
,
1
,
1
,
self
->
f
);
if
(
nread
==
0
)
break
;
os
<<
c
;
if
(
c
==
'\n'
)
break
;
}
return
boxString
(
os
.
str
());
}
Box
*
fileRead2
(
BoxedFile
*
self
,
Box
*
size
)
{
assert
(
self
->
cls
==
file_cls
);
if
(
size
->
cls
!=
int_cls
)
{
...
...
@@ -160,6 +180,9 @@ void setupFile() {
addRTFunction
(
read
,
(
void
*
)
fileRead2
,
NULL
,
2
,
false
);
file_cls
->
giveAttr
(
"read"
,
new
BoxedFunction
(
read
));
CLFunction
*
readline
=
boxRTFunction
((
void
*
)
fileReadline1
,
STR
,
1
,
false
);
file_cls
->
giveAttr
(
"readline"
,
new
BoxedFunction
(
readline
));
file_cls
->
giveAttr
(
"write"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
fileWrite
,
NULL
,
2
,
false
)));
file_cls
->
giveAttr
(
"close"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
fileClose
,
NULL
,
1
,
false
)));
...
...
src/runtime/str.cpp
View file @
b974b146
...
...
@@ -323,6 +323,22 @@ Box* strJoin(BoxedString* self, Box* rhs) {
}
}
Box
*
strSplit1
(
BoxedString
*
self
)
{
assert
(
self
->
cls
==
str_cls
);
BoxedList
*
rtn
=
new
BoxedList
();
std
::
ostringstream
os
(
""
);
for
(
char
c
:
self
->
s
)
{
if
(
c
==
' '
||
c
==
'\t'
||
c
==
'\n'
||
c
==
'\r'
||
c
==
'\f'
)
{
listAppendInternal
(
rtn
,
boxString
(
os
.
str
()));
os
.
str
(
""
);
}
}
listAppendInternal
(
rtn
,
boxString
(
os
.
str
()));
return
rtn
;
}
extern
"C"
Box
*
strGetitem
(
BoxedString
*
self
,
Box
*
slice
)
{
if
(
slice
->
cls
==
int_cls
)
{
BoxedInt
*
islice
=
static_cast
<
BoxedInt
*>
(
slice
);
...
...
@@ -368,6 +384,7 @@ void setupStr() {
str_cls
->
giveAttr
(
"__getitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strGetitem
,
NULL
,
2
,
false
)));
str_cls
->
giveAttr
(
"join"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strJoin
,
NULL
,
2
,
false
)));
str_cls
->
giveAttr
(
"split"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strSplit1
,
LIST
,
1
,
false
)));
CLFunction
*
__new__
=
boxRTFunction
((
void
*
)
strNew1
,
NULL
,
1
,
false
);
addRTFunction
(
__new__
,
(
void
*
)
strNew2
,
NULL
,
2
,
false
);
...
...
test/tests/map.py
0 → 100644
View file @
b974b146
def
f
(
x
):
print
"f(%s)"
%
x
return
-
x
print
map
(
f
,
range
(
10
))
test/tests/sys_stdout.py
View file @
b974b146
...
...
@@ -4,20 +4,3 @@ import sys
sys
.
stdout
.
write
(
"hello world
\
n
"
)
print
>>
sys
.
stdout
,
"hello world"
class
StringBuf
(
object
):
def
__init__
(
self
):
self
.
s
=
""
def
write
(
self
,
s
):
self
.
s
+=
s
def
getvalue
(
self
):
return
self
.
s
sys_stdout
=
sys
.
stdout
sys
.
stdout
=
StringBuf
()
print
"hello world"
print
>>
sys_stdout
,
"stringio contains:"
,
repr
(
sys
.
stdout
.
getvalue
())
test/tests/sys_stdout_changing.py
0 → 100644
View file @
b974b146
# expected: fail
# prints without an explicit destination should go to sys.stdout, not to the real stdout,
# in case sys.stdout gets changed:
import
sys
class
StringBuf
(
object
):
def
__init__
(
self
):
self
.
s
=
""
def
write
(
self
,
s
):
self
.
s
+=
s
def
getvalue
(
self
):
return
self
.
s
sys_stdout
=
sys
.
stdout
sys
.
stdout
=
StringBuf
()
print
"hello world"
print
>>
sys_stdout
,
"stringio contains:"
,
repr
(
sys
.
stdout
.
getvalue
())
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