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
1d452dde
Commit
1d452dde
authored
Aug 27, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(oldstyle) instance.nonzero
parent
06c7d568
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
3 deletions
+39
-3
src/runtime/classobj.cpp
src/runtime/classobj.cpp
+23
-2
src/runtime/objmodel.cpp
src/runtime/objmodel.cpp
+3
-1
test/tests/oldstyle_classes.py
test/tests/oldstyle_classes.py
+13
-0
No files found.
src/runtime/classobj.cpp
View file @
1d452dde
...
@@ -154,7 +154,7 @@ Box* classobjStr(Box* _obj) {
...
@@ -154,7 +154,7 @@ Box* classobjStr(Box* _obj) {
return
boxString
(
static_cast
<
BoxedString
*>
(
_mod
)
->
s
+
"."
+
cls
->
name
->
s
);
return
boxString
(
static_cast
<
BoxedString
*>
(
_mod
)
->
s
+
"."
+
cls
->
name
->
s
);
}
}
Box
*
instanceGetattribute
(
Box
*
_inst
,
Box
*
_attr
)
{
static
Box
*
_instanceGetattribute
(
Box
*
_inst
,
Box
*
_attr
,
bool
raise_on_missing
)
{
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
...
@@ -177,15 +177,22 @@ Box* instanceGetattribute(Box* _inst, Box* _attr) {
...
@@ -177,15 +177,22 @@ Box* instanceGetattribute(Box* _inst, Box* _attr) {
Box
*
getattr
=
classLookup
(
inst
->
inst_cls
,
getattr_str
);
Box
*
getattr
=
classLookup
(
inst
->
inst_cls
,
getattr_str
);
RELEASE_ASSERT
(
getattr
==
NULL
,
"unimplemented"
);
RELEASE_ASSERT
(
getattr
==
NULL
,
"unimplemented"
);
if
(
!
raise_on_missing
)
return
NULL
;
raiseExcHelper
(
AttributeError
,
"%s instance has no attribute '%s'"
,
inst
->
inst_cls
->
name
->
s
.
c_str
(),
raiseExcHelper
(
AttributeError
,
"%s instance has no attribute '%s'"
,
inst
->
inst_cls
->
name
->
s
.
c_str
(),
attr
->
s
.
c_str
());
attr
->
s
.
c_str
());
}
}
Box
*
instanceGetattribute
(
Box
*
_inst
,
Box
*
_attr
)
{
return
_instanceGetattribute
(
_inst
,
_attr
,
true
);
}
Box
*
instanceStr
(
Box
*
_inst
)
{
Box
*
instanceStr
(
Box
*
_inst
)
{
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
Box
*
str_func
=
instanceGetattribute
(
inst
,
boxStrConstant
(
"__str__"
)
);
Box
*
str_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__str__"
),
false
);
if
(
str_func
)
{
if
(
str_func
)
{
return
runtimeCall
(
str_func
,
ArgPassSpec
(
0
),
NULL
,
NULL
,
NULL
,
NULL
,
NULL
);
return
runtimeCall
(
str_func
,
ArgPassSpec
(
0
),
NULL
,
NULL
,
NULL
,
NULL
,
NULL
);
...
@@ -194,6 +201,19 @@ Box* instanceStr(Box* _inst) {
...
@@ -194,6 +201,19 @@ Box* instanceStr(Box* _inst) {
}
}
}
}
Box
*
instanceNonzero
(
Box
*
_inst
)
{
RELEASE_ASSERT
(
_inst
->
cls
==
instance_cls
,
""
);
BoxedInstance
*
inst
=
static_cast
<
BoxedInstance
*>
(
_inst
);
Box
*
nonzero_func
=
_instanceGetattribute
(
inst
,
boxStrConstant
(
"__nonzero__"
),
false
);
if
(
nonzero_func
)
{
return
runtimeCall
(
nonzero_func
,
ArgPassSpec
(
0
),
NULL
,
NULL
,
NULL
,
NULL
,
NULL
);
}
else
{
return
True
;
}
}
void
setupClassobj
()
{
void
setupClassobj
()
{
classobj_cls
=
new
BoxedClass
(
type_cls
,
object_cls
,
&
BoxedClassobj
::
gcHandler
,
offsetof
(
BoxedClassobj
,
attrs
),
classobj_cls
=
new
BoxedClass
(
type_cls
,
object_cls
,
&
BoxedClassobj
::
gcHandler
,
offsetof
(
BoxedClassobj
,
attrs
),
sizeof
(
BoxedClassobj
),
false
);
sizeof
(
BoxedClassobj
),
false
);
...
@@ -218,6 +238,7 @@ void setupClassobj() {
...
@@ -218,6 +238,7 @@ void setupClassobj() {
instance_cls
->
giveAttr
(
"__getattribute__"
,
instance_cls
->
giveAttr
(
"__getattribute__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceGetattribute
,
UNKNOWN
,
2
)));
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceGetattribute
,
UNKNOWN
,
2
)));
instance_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceStr
,
UNKNOWN
,
1
)));
instance_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceStr
,
UNKNOWN
,
1
)));
instance_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instanceNonzero
,
UNKNOWN
,
1
)));
instance_cls
->
freeze
();
instance_cls
->
freeze
();
}
}
...
...
src/runtime/objmodel.cpp
View file @
1d452dde
...
@@ -38,6 +38,7 @@
...
@@ -38,6 +38,7 @@
#include "gc/collector.h"
#include "gc/collector.h"
#include "gc/heap.h"
#include "gc/heap.h"
#include "runtime/capi.h"
#include "runtime/capi.h"
#include "runtime/classobj.h"
#include "runtime/float.h"
#include "runtime/float.h"
#include "runtime/generator.h"
#include "runtime/generator.h"
#include "runtime/iterobject.h"
#include "runtime/iterobject.h"
...
@@ -1547,7 +1548,8 @@ extern "C" bool nonzero(Box* obj) {
...
@@ -1547,7 +1548,8 @@ extern "C" bool nonzero(Box* obj) {
Box
*
func
=
getclsattr_internal
(
obj
,
"__nonzero__"
,
NULL
);
Box
*
func
=
getclsattr_internal
(
obj
,
"__nonzero__"
,
NULL
);
if
(
func
==
NULL
)
{
if
(
func
==
NULL
)
{
RELEASE_ASSERT
(
isUserDefined
(
obj
->
cls
),
"%s.__nonzero__"
,
getTypeName
(
obj
)
->
c_str
());
// TODO
ASSERT
(
isUserDefined
(
obj
->
cls
)
||
obj
->
cls
==
classobj_cls
,
"%s.__nonzero__"
,
getTypeName
(
obj
)
->
c_str
());
// TODO
return
true
;
return
true
;
}
}
...
...
test/tests/oldstyle_classes.py
View file @
1d452dde
...
@@ -53,3 +53,16 @@ def str2():
...
@@ -53,3 +53,16 @@ def str2():
return
"str2"
return
"str2"
e
.
__str__
=
str2
e
.
__str__
=
str2
print
e
print
e
print
bool
(
e
)
print
bool
(
E
)
class
F
:
def
__init__
(
self
,
n
):
self
.
n
=
n
def
__nonzero__
(
self
):
return
self
.
n
print
bool
(
F
(
0
))
print
bool
(
F
(
1
))
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