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
2899fa38
Commit
2899fa38
authored
Apr 30, 2015
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add func.__defaults__ (including setting), instancemethod.__func__
parent
318be2fc
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
60 additions
and
3 deletions
+60
-3
src/gc/heap.h
src/gc/heap.h
+1
-1
src/runtime/import.cpp
src/runtime/import.cpp
+1
-1
src/runtime/types.cpp
src/runtime/types.cpp
+30
-1
test/tests/functions.py
test/tests/functions.py
+21
-0
test/tests/instance_methods.py
test/tests/instance_methods.py
+7
-0
No files found.
src/gc/heap.h
View file @
2899fa38
...
...
@@ -136,7 +136,7 @@ public:
assert
(((
uint8_t
*
)
cur
+
size
)
<
end
&&
"arena full"
);
void
*
mrtn
=
mmap
(
cur
,
size
,
PROT_READ
|
PROT_WRITE
,
MAP_FIXED
|
MAP_PRIVATE
|
MAP_ANONYMOUS
,
-
1
,
0
);
assert
((
uintptr_t
)
mrtn
!=
-
1
&&
"failed to allocate memory from OS"
);
RELEASE_ASSERT
((
uintptr_t
)
mrtn
!=
-
1
,
"failed to allocate memory from OS"
);
ASSERT
(
mrtn
==
cur
,
"%p %p
\n
"
,
mrtn
,
cur
);
cur
=
(
uint8_t
*
)
cur
+
size
;
return
mrtn
;
...
...
src/runtime/import.cpp
View file @
2899fa38
...
...
@@ -471,7 +471,7 @@ static bool loadNext(Box* mod, Box* altmod, std::string& name, std::string& buf,
static
void
ensureFromlist
(
Box
*
module
,
Box
*
fromlist
,
std
::
string
&
buf
,
bool
recursive
);
Box
*
importModuleLevel
(
const
std
::
string
&
name
,
Box
*
globals
,
Box
*
from_imports
,
int
level
)
{
assert
(
!
globals
||
globals
==
None
||
isSubclass
(
globals
->
cls
,
module_cls
)
);
RELEASE_ASSERT
(
!
globals
||
globals
==
None
||
isSubclass
(
globals
->
cls
,
module_cls
),
""
);
bool
return_first
=
from_imports
==
None
;
static
StatCounter
slowpath_import
(
"slowpath_import"
);
...
...
src/runtime/types.cpp
View file @
2899fa38
...
...
@@ -834,6 +834,32 @@ static Box* functionDefaults(Box* self, void*) {
return
BoxedTuple
::
create
(
func
->
ndefaults
,
&
func
->
defaults
->
elts
[
0
]);
}
static
void
functionSetDefaults
(
Box
*
b
,
Box
*
v
,
void
*
)
{
RELEASE_ASSERT
(
v
,
"can't delete __defaults__"
);
assert
(
b
->
cls
==
function_cls
);
BoxedFunction
*
func
=
static_cast
<
BoxedFunction
*>
(
b
);
if
(
v
==
None
)
v
=
EmptyTuple
;
if
(
!
isSubclass
(
v
->
cls
,
tuple_cls
))
{
raiseExcHelper
(
TypeError
,
"__defaults__ must be set to a tuple object"
);
}
BoxedTuple
*
t
=
static_cast
<
BoxedTuple
*>
(
v
);
if
(
t
->
size
()
==
func
->
ndefaults
)
{
for
(
int
i
=
0
;
i
<
func
->
ndefaults
;
i
++
)
{
func
->
defaults
->
elts
[
i
]
=
t
->
elts
[
i
];
}
return
;
}
else
{
RELEASE_ASSERT
(
0
,
"can't change number of defaults on a function for now"
);
}
abort
();
}
static
Box
*
functionNonzero
(
BoxedFunction
*
self
)
{
return
True
;
}
...
...
@@ -2250,7 +2276,8 @@ void setupRuntime() {
function_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
functionNonzero
,
BOXED_BOOL
,
1
)));
function_cls
->
giveAttr
(
"func_code"
,
new
(
pyston_getset_cls
)
BoxedGetsetDescriptor
(
functionCode
,
NULL
,
NULL
));
function_cls
->
giveAttr
(
"func_defaults"
,
new
(
pyston_getset_cls
)
BoxedGetsetDescriptor
(
functionDefaults
,
NULL
,
NULL
));
new
(
pyston_getset_cls
)
BoxedGetsetDescriptor
(
functionDefaults
,
functionSetDefaults
,
NULL
));
function_cls
->
giveAttr
(
"__defaults__"
,
function_cls
->
getattr
(
"func_defaults"
));
function_cls
->
freeze
();
builtin_function_or_method_cls
->
giveAttr
(
...
...
@@ -2275,8 +2302,10 @@ void setupRuntime() {
"__call__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instancemethodCall
,
UNKNOWN
,
1
,
0
,
true
,
true
)));
instancemethod_cls
->
giveAttr
(
"im_func"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedInstanceMethod
,
func
)));
instancemethod_cls
->
giveAttr
(
"__func__"
,
instancemethod_cls
->
getattr
(
"im_func"
));
instancemethod_cls
->
giveAttr
(
"im_self"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedInstanceMethod
,
obj
)));
instancemethod_cls
->
giveAttr
(
"__self__"
,
instancemethod_cls
->
getattr
(
"im_self"
));
instancemethod_cls
->
freeze
();
slice_cls
->
giveAttr
(
"__new__"
,
...
...
test/tests/functions.py
View file @
2899fa38
...
...
@@ -13,3 +13,24 @@ print type(f).__call__(f)
print
type
(
f
).
__call__
(
g
)
print
bool
(
f
)
def
func_with_defaults
(
a
,
b
=
1
):
print
a
,
b
func_with_defaults
(
0
)
print
type
(
func_with_defaults
.
func_code
)
print
func_with_defaults
.
func_defaults
print
func_with_defaults
.
__defaults__
try
:
func_with_defaults
.
func_defaults
=
[
2
]
except
TypeError
as
e
:
print
e
# del func_with_defaults.__defaults__
# func_with_defaults.__defaults__ = (1, 2)
# func_with_defaults()
def
func_without_defaults
():
pass
print
repr
(
func_without_defaults
.
__defaults__
)
test/tests/instance_methods.py
0 → 100644
View file @
2899fa38
class
C
(
object
):
def
foo
(
self
):
pass
print
type
(
C
.
foo
)
print
type
(
C
.
foo
.
im_func
),
type
(
C
.
foo
.
__func__
)
print
type
(
C
.
foo
.
im_self
),
type
(
C
.
foo
.
__self__
)
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