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
bf51b63a
Commit
bf51b63a
authored
May 21, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement set.__len__, dict.pop
parent
d450c321
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
62 additions
and
8 deletions
+62
-8
src/runtime/dict.cpp
src/runtime/dict.cpp
+40
-0
src/runtime/set.cpp
src/runtime/set.cpp
+7
-0
test/tests/dict.py
test/tests/dict.py
+13
-0
test/tests/set.py
test/tests/set.py
+2
-0
test/tests/str_iteration.py
test/tests/str_iteration.py
+0
-8
No files found.
src/runtime/dict.cpp
View file @
bf51b63a
...
...
@@ -16,6 +16,8 @@
#include "core/stats.h"
#include "core/types.h"
#include "codegen/compvars.h"
#include "runtime/gc_runtime.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
...
...
@@ -101,6 +103,40 @@ Box* dictSetitem(BoxedDict* self, Box* k, Box* v) {
return
None
;
}
Box
*
dictPop2
(
BoxedDict
*
self
,
Box
*
k
)
{
assert
(
self
->
cls
==
dict_cls
);
auto
it
=
self
->
d
.
find
(
k
);
if
(
it
==
self
->
d
.
end
())
{
Box
*
s
=
NULL
;
try
{
s
=
repr
(
k
);
}
catch
(
Box
*
e
)
{
s
=
NULL
;
}
if
(
s
)
raiseExcHelper
(
KeyError
,
"%s"
,
static_cast
<
BoxedString
*>
(
s
)
->
s
.
c_str
());
else
raiseExcHelper
(
KeyError
,
""
);
}
Box
*
rtn
=
it
->
second
;
self
->
d
.
erase
(
it
);
return
rtn
;
}
Box
*
dictPop3
(
BoxedDict
*
self
,
Box
*
k
,
Box
*
d
)
{
assert
(
self
->
cls
==
dict_cls
);
auto
it
=
self
->
d
.
find
(
k
);
if
(
it
==
self
->
d
.
end
())
return
d
;
Box
*
rtn
=
it
->
second
;
self
->
d
.
erase
(
it
);
return
rtn
;
}
void
setupDict
()
{
dict_cls
->
giveAttr
(
"__name__"
,
boxStrConstant
(
"dict"
));
// dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, NULL, 1, false)));
...
...
@@ -119,6 +155,10 @@ void setupDict() {
dict_cls
->
giveAttr
(
"keys"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
dictKeys
,
NULL
,
1
,
false
)));
dict_cls
->
setattr
(
"iterkeys"
,
dict_cls
->
peekattr
(
"keys"
),
NULL
,
NULL
);
CLFunction
*
pop
=
boxRTFunction
((
void
*
)
dictPop2
,
UNKNOWN
,
2
,
false
);
addRTFunction
(
pop
,
(
void
*
)
dictPop3
,
UNKNOWN
,
3
,
false
);
dict_cls
->
giveAttr
(
"pop"
,
new
BoxedFunction
(
pop
));
dict_cls
->
giveAttr
(
"__getitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
dictGetitem
,
NULL
,
2
,
false
)));
dict_cls
->
giveAttr
(
"__setitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
dictSetitem
,
NULL
,
3
,
false
)));
...
...
src/runtime/set.cpp
View file @
bf51b63a
...
...
@@ -165,6 +165,11 @@ Box* setIter(BoxedSet* self) {
return
new
BoxedSetIterator
(
self
);
}
Box
*
setLen
(
BoxedSet
*
self
)
{
assert
(
self
->
cls
==
set_cls
);
return
boxInt
(
self
->
s
.
size
());
}
}
// namespace set
using
namespace
pyston
::
set
;
...
...
@@ -212,6 +217,8 @@ void setupSet() {
set_cls
->
giveAttr
(
"__iter__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
setIter
,
typeFromClass
(
set_iterator_cls
),
1
,
false
)));
set_cls
->
giveAttr
(
"__len__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
setLen
,
BOXED_INT
,
1
,
false
)));
set_cls
->
freeze
();
}
...
...
test/tests/dict.py
View file @
bf51b63a
...
...
@@ -9,3 +9,16 @@ for i in xrange(10):
print
sorted
(
d
.
items
())
print
sorted
(
d
.
values
())
print
sorted
(
d
.
keys
())
print
d
.
pop
(
5
,
5
)
print
sorted
(
d
.
items
())
print
d
.
pop
(
5
,
5
)
print
sorted
(
d
.
items
())
print
d
.
pop
(
4
)
try
:
d
.
pop
(
5
)
assert
0
except
KeyError
:
print
"ok"
print
sorted
(
d
.
items
())
test/tests/set.py
View file @
bf51b63a
...
...
@@ -15,3 +15,5 @@ print sorted(s2 - s1)
print
sorted
(
s1
^
s2
)
print
sorted
(
s1
&
s2
)
print
sorted
(
s1
|
s2
)
print
len
(
set
(
range
(
5
)))
test/tests/str_iteration.py
deleted
100644 → 0
View file @
d450c321
# expected: fail
# - string iteration
# This should probably be moved into the str_functions test, once it's no longer failing
for
c
in
"hello world"
:
print
repr
(
c
)
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