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
b2f7eddc
Commit
b2f7eddc
authored
May 31, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make int more subclass-friendly
parent
fd8ea7ce
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
65 additions
and
35 deletions
+65
-35
src/core/types.h
src/core/types.h
+1
-1
src/runtime/inline/boxing.cpp
src/runtime/inline/boxing.cpp
+1
-1
src/runtime/int.cpp
src/runtime/int.cpp
+40
-20
src/runtime/list.cpp
src/runtime/list.cpp
+3
-3
src/runtime/objmodel.cpp
src/runtime/objmodel.cpp
+3
-3
src/runtime/types.cpp
src/runtime/types.cpp
+2
-5
src/runtime/types.h
src/runtime/types.h
+12
-1
test/tests/inheritance.py
test/tests/inheritance.py
+3
-1
No files found.
src/core/types.h
View file @
b2f7eddc
...
...
@@ -323,7 +323,7 @@ public:
Box
(
const
ObjectFlavor
*
flavor
,
BoxedClass
*
cls
);
HCAttrs
*
getAttrs
();
HCAttrs
*
getAttrs
Ptr
();
void
setattr
(
const
std
::
string
&
attr
,
Box
*
val
,
SetattrRewriteArgs2
*
rewrite_args2
);
void
giveAttr
(
const
std
::
string
&
attr
,
Box
*
val
)
{
...
...
src/runtime/inline/boxing.cpp
View file @
b2f7eddc
...
...
@@ -56,7 +56,7 @@ Box* boxInt(int64_t n) {
if
(
0
<=
n
&&
n
<
NUM_INTERNED_INTS
)
{
return
interned_ints
[
n
];
}
return
new
BoxedInt
(
n
);
return
new
BoxedInt
(
int_cls
,
n
);
}
// BoxedInt::BoxedInt(int64_t n) : Box(int_cls), n(n) {}
...
...
src/runtime/int.cpp
View file @
b2f7eddc
...
...
@@ -120,8 +120,10 @@ extern "C" Box* intAddFloat(BoxedInt* lhs, BoxedFloat* rhs) {
}
extern
"C"
Box
*
intAdd
(
BoxedInt
*
lhs
,
Box
*
rhs
)
{
assert
(
lhs
->
cls
==
int_cls
);
if
(
rhs
->
cls
==
int_cls
)
{
if
(
!
isSubclass
(
lhs
->
cls
,
int_cls
))
raiseExcHelper
(
TypeError
,
"descriptor '__add__' requires a 'int' object but received a '%s'"
,
getTypeName
(
rhs
)
->
c_str
());
if
(
isSubclass
(
rhs
->
cls
,
int_cls
))
{
BoxedInt
*
rhs_int
=
static_cast
<
BoxedInt
*>
(
rhs
);
return
boxInt
(
lhs
->
n
+
rhs_int
->
n
);
}
else
if
(
rhs
->
cls
==
float_cls
)
{
...
...
@@ -410,8 +412,12 @@ extern "C" Box* intInvert(BoxedInt* v) {
}
extern
"C"
Box
*
intPos
(
BoxedInt
*
v
)
{
assert
(
v
->
cls
==
int_cls
);
return
v
;
if
(
!
isSubclass
(
v
->
cls
,
int_cls
))
raiseExcHelper
(
TypeError
,
"descriptor '__pos__' requires a 'int' object but received a '%s'"
,
getTypeName
(
rhs
)
->
c_str
());
if
(
v
->
cls
==
int_cls
)
return
v
;
return
boxInt
(
v
->
n
);
}
extern
"C"
Box
*
intNeg
(
BoxedInt
*
v
)
{
...
...
@@ -425,7 +431,9 @@ extern "C" Box* intNonzero(BoxedInt* v) {
}
extern
"C"
BoxedString
*
intRepr
(
BoxedInt
*
v
)
{
assert
(
v
->
cls
==
int_cls
);
if
(
!
isSubclass
(
v
->
cls
,
int_cls
))
raiseExcHelper
(
TypeError
,
"descriptor '__repr__' requires a 'int' object but received a '%s'"
,
getTypeName
(
rhs
)
->
c_str
());
char
buf
[
80
];
int
len
=
snprintf
(
buf
,
80
,
"%ld"
,
v
->
n
);
return
new
BoxedString
(
std
::
string
(
buf
,
len
));
...
...
@@ -437,42 +445,53 @@ extern "C" Box* intHash(BoxedInt* self) {
}
extern
"C"
Box
*
intNew1
(
Box
*
cls
)
{
assert
(
cls
==
int_cls
);
return
new
BoxedInt
(
0
);
if
(
!
is_subclass
(
_cls
->
cls
,
type_cls
))
raiseExcHelper
(
TypeError
,
"int.__new__(X): X is not a type object (%s)"
,
getTypeName
(
_cls
)
->
c_str
());
return
boxInt
(
0
);
}
extern
"C"
Box
*
intNew2
(
Box
*
cls
,
Box
*
val
)
{
assert
(
cls
==
int_cls
);
extern
"C"
Box
*
intNew2
(
Box
*
_cls
,
Box
*
val
)
{
if
(
!
is_subclass
(
_cls
->
cls
,
type_cls
))
raiseExcHelper
(
TypeError
,
"int.__new__(X): X is not a type object (%s)"
,
getTypeName
(
_cls
)
->
c_str
());
BoxedClass
*
cls
=
static_cast
<
BoxedClass
*>
(
_cls
);
if
(
!
isSubclass
(
cls
,
int_cls
))
raiseExcHelper
(
TypeError
,
"int.__new__(%s): %s is not a subtype of int"
,
getNameOfClass
(
cls
)
->
c_str
(),
getNameOfClass
(
cls
)
->
c_str
());
assert
(
cls
->
instance_size
>=
sizeof
(
BoxedInt
));
void
*
mem
=
rt_alloc
(
cls
->
instance_size
);
BoxedInt
*
rtn
=
::
new
(
mem
)
BoxedInt
(
cls
,
0
);
initUserAttrs
(
rtn
,
cls
);
if
(
val
->
cls
==
int_cls
)
{
r
eturn
val
;
r
tn
->
n
=
static_cast
<
BoxedInt
*>
(
val
)
->
n
;
}
else
if
(
val
->
cls
==
str_cls
)
{
BoxedString
*
s
=
static_cast
<
BoxedString
*>
(
val
);
std
::
istringstream
ss
(
s
->
s
);
int64_t
n
;
ss
>>
n
;
r
eturn
boxInt
(
n
)
;
r
tn
->
n
=
n
;
}
else
if
(
val
->
cls
==
float_cls
)
{
double
d
=
static_cast
<
BoxedFloat
*>
(
val
)
->
d
;
r
eturn
boxInt
(
d
)
;
r
tn
->
n
=
d
;
}
else
{
fprintf
(
stderr
,
"TypeError: int() argument must be a string or a number, not '%s'
\n
"
,
getTypeName
(
val
)
->
c_str
());
raiseExcHelper
(
TypeError
,
""
);
}
return
rtn
;
}
extern
"C"
Box
*
intInit1
(
BoxedInt
*
self
)
{
assert
(
self
->
cls
==
int_cls
);
extern
"C"
Box
*
intInit1
(
Box
*
self
)
{
// int.__init__ will actually let you call it with anything
return
None
;
}
extern
"C"
Box
*
intInit2
(
BoxedInt
*
self
,
Box
*
val
)
{
assert
(
self
->
cls
==
int_cls
);
// int.__init__ will actually let you call it with anything
return
None
;
}
...
...
@@ -483,8 +502,9 @@ static void _addFuncIntFloatUnknown(const char* name, void* int_func, void* floa
v_ii
.
push_back
(
BOXED_INT
);
v_if
.
push_back
(
BOXED_INT
);
v_if
.
push_back
(
BOXED_FLOAT
);
v_iu
.
push_back
(
BOXED_INT
);
v_iu
.
push_back
(
NULL
);
// Only the unknown version can accept non-ints (ex if you access the function directly ex via int.__add__)
v_iu
.
push_back
(
UNKNOWN
);
v_iu
.
push_back
(
UNKNOWN
);
CLFunction
*
cl
=
createRTFunction
();
addRTFunction
(
cl
,
int_func
,
BOXED_INT
,
v_ii
,
false
);
...
...
@@ -548,7 +568,7 @@ void setupInt() {
int_cls
->
freeze
();
for
(
int
i
=
0
;
i
<
NUM_INTERNED_INTS
;
i
++
)
{
interned_ints
[
i
]
=
new
BoxedInt
(
i
);
interned_ints
[
i
]
=
new
BoxedInt
(
i
nt_cls
,
i
);
gc
::
registerStaticRootObj
(
interned_ints
[
i
]);
}
}
...
...
src/runtime/list.cpp
View file @
b2f7eddc
...
...
@@ -85,7 +85,7 @@ extern "C" Box* listPop2(BoxedList* self, Box* idx) {
}
extern
"C"
Box
*
listLen
(
BoxedList
*
self
)
{
return
new
Boxed
Int
(
self
->
size
);
return
box
Int
(
self
->
size
);
}
Box
*
_listSlice
(
BoxedList
*
self
,
i64
start
,
i64
stop
,
i64
step
)
{
...
...
@@ -361,7 +361,7 @@ Box* listCount(BoxedList* self, Box* elt) {
if
(
b
)
count
++
;
}
return
new
Boxed
Int
(
count
);
return
box
Int
(
count
);
}
Box
*
listIndex
(
BoxedList
*
self
,
Box
*
elt
)
{
...
...
@@ -372,7 +372,7 @@ Box* listIndex(BoxedList* self, Box* elt) {
Box
*
cmp
=
compareInternal
(
e
,
elt
,
AST_TYPE
::
Eq
,
NULL
);
bool
b
=
nonzero
(
cmp
);
if
(
b
)
return
new
Boxed
Int
(
i
);
return
box
Int
(
i
);
}
BoxedString
*
tostr
=
static_cast
<
BoxedString
*>
(
repr
(
elt
));
...
...
src/runtime/objmodel.cpp
View file @
b2f7eddc
...
...
@@ -346,7 +346,7 @@ Box::Box(const ObjectFlavor* flavor, BoxedClass* cls) : GCObject(flavor), cls(cl
}
HCAttrs
*
Box
::
getAttrs
()
{
HCAttrs
*
Box
::
getAttrs
Ptr
()
{
assert
(
cls
->
instancesHaveAttrs
());
char
*
p
=
reinterpret_cast
<
char
*>
(
this
);
...
...
@@ -376,7 +376,7 @@ Box* Box::getattr(const std::string& attr, GetattrRewriteArgs* rewrite_args, Get
if
(
!
cls
->
instancesHaveAttrs
())
return
NULL
;
HCAttrs
*
attrs
=
getAttrs
();
HCAttrs
*
attrs
=
getAttrs
Ptr
();
HiddenClass
*
hcls
=
attrs
->
hcls
;
if
(
rewrite_args
)
{
...
...
@@ -464,7 +464,7 @@ void Box::setattr(const std::string& attr, Box* val, SetattrRewriteArgs2* rewrit
}
}
HCAttrs
*
attrs
=
getAttrs
();
HCAttrs
*
attrs
=
getAttrs
Ptr
();
HiddenClass
*
hcls
=
attrs
->
hcls
;
int
numattrs
=
hcls
->
attr_offsets
.
size
();
...
...
src/runtime/types.cpp
View file @
b2f7eddc
...
...
@@ -105,7 +105,7 @@ extern "C" void boxGCHandler(GCVisitor* v, void* p) {
v
->
visit
(
b
->
cls
);
if
(
b
->
cls
->
instancesHaveAttrs
())
{
HCAttrs
*
attrs
=
b
->
getAttrs
();
HCAttrs
*
attrs
=
b
->
getAttrs
Ptr
();
v
->
visit
(
attrs
->
hcls
);
int
nattrs
=
attrs
->
hcls
->
attr_offsets
.
size
();
...
...
@@ -388,10 +388,7 @@ Box* objectNew1(BoxedClass* cls) {
void
*
mem
=
rt_alloc
(
cls
->
instance_size
);
Box
*
rtn
=
::
new
(
mem
)
Box
(
&
object_flavor
,
cls
);
if
(
cls
->
attrs_offset
)
{
HCAttrs
*
attrs
=
rtn
->
getAttrs
();
attrs
=
new
((
void
*
)
attrs
)
HCAttrs
();
}
initUserAttrs
(
rtn
,
cls
);
return
rtn
;
}
...
...
src/runtime/types.h
View file @
b2f7eddc
...
...
@@ -163,7 +163,7 @@ class BoxedInt : public Box {
public:
int64_t
n
;
BoxedInt
(
int64_t
n
)
__attribute__
((
visibility
(
"default"
)))
:
Box
(
&
int_flavor
,
int_
cls
),
n
(
n
)
{}
BoxedInt
(
BoxedClass
*
cls
,
int64_t
n
)
__attribute__
((
visibility
(
"default"
)))
:
Box
(
&
int_flavor
,
cls
),
n
(
n
)
{}
};
class
BoxedFloat
:
public
Box
{
...
...
@@ -300,5 +300,16 @@ Box* exceptionNew2(BoxedClass* cls, Box* message);
extern
BoxedClass
*
Exception
,
*
AssertionError
,
*
AttributeError
,
*
TypeError
,
*
NameError
,
*
KeyError
,
*
IndexError
,
*
IOError
,
*
OSError
,
*
ZeroDivisionError
,
*
ValueError
,
*
UnboundLocalError
,
*
RuntimeError
,
*
ImportError
;
// cls should be obj->cls.
// Added as parameter because it should typically be available
inline
void
initUserAttrs
(
Box
*
obj
,
BoxedClass
*
cls
)
{
assert
(
obj
->
cls
==
cls
);
if
(
cls
->
attrs_offset
)
{
HCAttrs
*
attrs
=
obj
->
getAttrsPtr
();
attrs
=
new
((
void
*
)
attrs
)
HCAttrs
();
}
}
}
#endif
test/tests/inheritance.py
View file @
b2f7eddc
...
...
@@ -42,7 +42,7 @@ def f2():
f2
()
def
f3
():
class
I
(
objec
t
):
class
I
(
in
t
):
def
foo
(
self
):
print
self
+
1
...
...
@@ -56,6 +56,8 @@ def f3():
e
=
a
+
c
print
d
,
type
(
d
)
print
e
,
type
(
e
)
f
=
+
a
print
f
,
type
(
f
)
print
a
.
foo
()
f3
()
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