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
c4e675ab
Commit
c4e675ab
authored
Aug 27, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add oldstyle-instance len, getitem, setitem
parent
1d452dde
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
0 deletions
+58
-0
src/runtime/classobj.cpp
src/runtime/classobj.cpp
+30
-0
test/tests/oldstyle_classes.py
test/tests/oldstyle_classes.py
+28
-0
No files found.
src/runtime/classobj.cpp
View file @
c4e675ab
...
...
@@ -207,6 +207,9 @@ Box* instanceNonzero(Box* _inst) {
Box
*
nonzero_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__nonzero__"
),
false
);
if
(
nonzero_func
==
NULL
)
nonzero_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__len__"
),
false
);
if
(
nonzero_func
)
{
return
runtimeCall
(
nonzero_func
,
ArgPassSpec
(
0
),
NULL
,
NULL
,
NULL
,
NULL
,
NULL
);
}
else
{
...
...
@@ -214,6 +217,30 @@ Box* instanceNonzero(Box* _inst) {
}
}
Box
*
instanceLen
(
Box
*
_inst
)
{
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
Box
*
len_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__len__"
),
true
);
return
runtimeCall
(
len_func
,
ArgPassSpec
(
0
),
NULL
,
NULL
,
NULL
,
NULL
,
NULL
);
}
Box
*
instanceGetitem
(
Box
*
_inst
,
Box
*
key
)
{
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
Box
*
getitem_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__getitem__"
),
true
);
return
runtimeCall
(
getitem_func
,
ArgPassSpec
(
1
),
key
,
NULL
,
NULL
,
NULL
,
NULL
);
}
Box
*
instanceSetitem
(
Box
*
_inst
,
Box
*
key
,
Box
*
value
)
{
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
Box
*
setitem_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__setitem__"
),
true
);
return
runtimeCall
(
setitem_func
,
ArgPassSpec
(
2
),
key
,
value
,
NULL
,
NULL
,
NULL
);
}
void
setupClassobj
()
{
classobj_cls
=
new
BoxedClass
(
type_cls
,
object_cls
,
&
BoxedClassobj
::
gcHandler
,
offsetof
(
BoxedClassobj
,
attrs
),
sizeof
(
BoxedClassobj
),
false
);
...
...
@@ -239,6 +266,9 @@ void setupClassobj() {
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceGetattribute
,
UNKNOWN
,
2
)));
instance_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceStr
,
UNKNOWN
,
1
)));
instance_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceNonzero
,
UNKNOWN
,
1
)));
instance_cls
->
giveAttr
(
"__len__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceLen
,
UNKNOWN
,
1
)));
instance_cls
->
giveAttr
(
"__getitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceGetitem
,
UNKNOWN
,
2
)));
instance_cls
->
giveAttr
(
"__setitem__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceSetitem
,
UNKNOWN
,
3
)));
instance_cls
->
freeze
();
}
...
...
test/tests/oldstyle_classes.py
View file @
c4e675ab
...
...
@@ -44,10 +44,21 @@ class E():
def
__str__
(
self
):
return
"E(%d)"
%
self
.
n
def
__getitem__
(
self
,
x
):
print
"getitem"
return
x
def
__len__
(
self
):
print
"len"
return
self
.
n
e
=
E
(
1
)
print
e
print
e
.
n
print
e
.
foo
()
print
e
[
1
]
print
e
[
1
:
2
]
print
len
(
e
)
def
str2
():
return
"str2"
...
...
@@ -55,6 +66,7 @@ e.__str__ = str2
print
e
print
bool
(
e
)
print
bool
(
E
(
0
))
print
bool
(
E
)
class
F
:
...
...
@@ -66,3 +78,19 @@ class F:
print
bool
(
F
(
0
))
print
bool
(
F
(
1
))
f
=
F
(
0
)
try
:
len
(
f
)
except
AttributeError
,
e
:
print
e
try
:
f
[
1
]
except
AttributeError
,
e
:
print
e
try
:
f
[
1
]
=
2
except
AttributeError
,
e
:
print
e
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