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
3d94e842
Commit
3d94e842
authored
Feb 23, 2015
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #320 from tjhance/builtin-function-name
__name__ for builtin functions
parents
0084e712
f6160517
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
122 additions
and
70 deletions
+122
-70
src/runtime/builtin_modules/builtins.cpp
src/runtime/builtin_modules/builtins.cpp
+58
-45
src/runtime/builtin_modules/gc.cpp
src/runtime/builtin_modules/gc.cpp
+6
-4
src/runtime/builtin_modules/pyston.cpp
src/runtime/builtin_modules/pyston.cpp
+2
-1
src/runtime/builtin_modules/sys.cpp
src/runtime/builtin_modules/sys.cpp
+5
-4
src/runtime/builtin_modules/thread.cpp
src/runtime/builtin_modules/thread.cpp
+9
-8
src/runtime/import.cpp
src/runtime/import.cpp
+2
-2
src/runtime/objmodel.cpp
src/runtime/objmodel.cpp
+2
-2
src/runtime/types.cpp
src/runtime/types.cpp
+27
-0
src/runtime/types.h
src/runtime/types.h
+3
-4
test/tests/class_and_func_name.py
test/tests/class_and_func_name.py
+8
-0
No files found.
src/runtime/builtin_modules/builtins.cpp
View file @
3d94e842
This diff is collapsed.
Click to expand it.
src/runtime/builtin_modules/gc.cpp
View file @
3d94e842
...
...
@@ -40,9 +40,11 @@ static Box* enable() {
void
setupGC
()
{
BoxedModule
*
gc_module
=
createModule
(
"gc"
,
"__builtin__"
);
gc_module
->
giveAttr
(
"collect"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
gcCollect
,
NONE
,
0
)));
gc_module
->
giveAttr
(
"isenabled"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
isEnabled
,
BOXED_BOOL
,
0
)));
gc_module
->
giveAttr
(
"disable"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
disable
,
NONE
,
0
)));
gc_module
->
giveAttr
(
"enable"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
enable
,
NONE
,
0
)));
gc_module
->
giveAttr
(
"collect"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
gcCollect
,
NONE
,
0
),
"collect"
));
gc_module
->
giveAttr
(
"isenabled"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
isEnabled
,
BOXED_BOOL
,
0
),
"isenabled"
));
gc_module
->
giveAttr
(
"disable"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
disable
,
NONE
,
0
),
"disable"
));
gc_module
->
giveAttr
(
"enable"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
enable
,
NONE
,
0
),
"enable"
));
}
}
src/runtime/builtin_modules/pyston.cpp
View file @
3d94e842
...
...
@@ -51,6 +51,7 @@ static Box* setOption(Box* option, Box* value) {
void
setupPyston
()
{
pyston_module
=
createModule
(
"__pyston__"
,
"__builtin__"
);
pyston_module
->
giveAttr
(
"setOption"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
setOption
,
UNKNOWN
,
2
)));
pyston_module
->
giveAttr
(
"setOption"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
setOption
,
UNKNOWN
,
2
),
"setOption"
));
}
}
src/runtime/builtin_modules/sys.cpp
View file @
3d94e842
...
...
@@ -233,11 +233,12 @@ void setupSys() {
sys_module
->
giveAttr
(
"stdin"
,
new
BoxedFile
(
stdin
,
"<stdin>"
,
"r"
));
sys_module
->
giveAttr
(
"stderr"
,
new
BoxedFile
(
stderr
,
"<stderr>"
,
"w"
));
sys_module
->
giveAttr
(
"exc_info"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
sysExcInfo
,
BOXED_TUPLE
,
0
)));
sys_module
->
giveAttr
(
"exc_clear"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
sysExcClear
,
NONE
,
0
)));
sys_module
->
giveAttr
(
"exit"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
sysExit
,
NONE
,
1
,
1
,
false
,
false
),
{
None
}));
"exc_info"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
sysExcInfo
,
BOXED_TUPLE
,
0
),
"exc_info"
));
sys_module
->
giveAttr
(
"exc_clear"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
sysExcClear
,
NONE
,
0
),
"exc_clear"
));
sys_module
->
giveAttr
(
"exit"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
sysExit
,
NONE
,
1
,
1
,
false
,
false
),
"exit"
,
{
None
}));
sys_module
->
giveAttr
(
"warnoptions"
,
new
BoxedList
());
sys_module
->
giveAttr
(
"py3kwarning"
,
False
);
...
...
src/runtime/builtin_modules/thread.cpp
View file @
3d94e842
...
...
@@ -169,14 +169,15 @@ Box* stackSize() {
void
setupThread
()
{
thread_module
=
createModule
(
"thread"
,
"__builtin__"
);
thread_module
->
giveAttr
(
"start_new_thread"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
startNewThread
,
BOXED_INT
,
2
)));
thread_module
->
giveAttr
(
"allocate_lock"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
allocateLock
,
UNKNOWN
,
0
)));
thread_module
->
giveAttr
(
"get_ident"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
getIdent
,
BOXED_INT
,
0
)));
thread_module
->
giveAttr
(
"stack_size"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
stackSize
,
BOXED_INT
,
0
)));
thread_module
->
giveAttr
(
"start_new_thread"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
startNewThread
,
BOXED_INT
,
2
),
"start_new_thread"
));
thread_module
->
giveAttr
(
"allocate_lock"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
allocateLock
,
UNKNOWN
,
0
),
"allocate_lock"
));
thread_module
->
giveAttr
(
"get_ident"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
getIdent
,
BOXED_INT
,
0
),
"get_ident"
));
thread_module
->
giveAttr
(
"stack_size"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
stackSize
,
BOXED_INT
,
0
),
"stack_size"
));
thread_lock_cls
=
new
BoxedHeapClass
(
object_cls
,
NULL
,
0
,
sizeof
(
BoxedThreadLock
),
false
,
"lock"
);
thread_lock_cls
->
giveAttr
(
"__module__"
,
boxStrConstant
(
"thread"
));
...
...
src/runtime/import.cpp
View file @
3d94e842
...
...
@@ -302,7 +302,7 @@ Box* impFindModule(Box* _name) {
void
setupImport
()
{
BoxedModule
*
imp_module
=
createModule
(
"imp"
,
"__builtin__"
);
imp_module
->
giveAttr
(
"find_module"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
impFindModule
,
UNKNOWN
,
1
)
));
imp_module
->
giveAttr
(
"find_module"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
impFindModule
,
UNKNOWN
,
1
),
"find_module"
));
}
}
src/runtime/objmodel.cpp
View file @
3d94e842
...
...
@@ -1533,8 +1533,8 @@ bool dataDescriptorSetSpecialCases(Box* obj, Box* val, Box* descr, SetattrRewrit
// TODO type checking goes here
if
(
getset_descr
->
set
==
NULL
)
{
raiseExcHelper
(
AttributeError
,
"attribute '%s' of '%s' object is not writable"
,
attr_name
.
c_str
(),
getTypeName
(
getset_descr
));
raiseExcHelper
(
AttributeError
,
"attribute '%s' of '%s' object
s
is not writable"
,
attr_name
.
c_str
(),
getTypeName
(
obj
));
}
if
(
rewrite_args
)
{
...
...
src/runtime/types.cpp
View file @
3d94e842
...
...
@@ -321,6 +321,19 @@ BoxedFunction::BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults
}
}
BoxedBuiltinFunctionOrMethod
::
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
const
char
*
name
)
:
BoxedBuiltinFunctionOrMethod
(
f
,
name
,
{})
{
}
BoxedBuiltinFunctionOrMethod
::
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
const
char
*
name
,
std
::
initializer_list
<
Box
*>
defaults
,
BoxedClosure
*
closure
,
bool
isGenerator
)
:
BoxedFunctionBase
(
f
,
defaults
,
closure
,
isGenerator
)
{
assert
(
name
);
this
->
name
=
static_cast
<
BoxedString
*>
(
boxString
(
name
));
}
extern
"C"
void
functionGCHandler
(
GCVisitor
*
v
,
Box
*
b
)
{
boxGCHandler
(
v
,
b
);
...
...
@@ -685,6 +698,18 @@ static void func_set_name(Box* b, Box* v, void*) {
func
->
name
=
static_cast
<
BoxedString
*>
(
v
);
}
static
Box
*
builtin_function_or_method_name
(
Box
*
b
,
void
*
)
{
// In CPython, these guys just store char*, and it gets wrapped here
// But we already share the BoxedString* field with BoxedFunctions...
// so it's more convenient to just use that, which is what we do here.
// Is there any advantage to using the char* way, here?
assert
(
b
->
cls
==
builtin_function_or_method_cls
);
BoxedBuiltinFunctionOrMethod
*
func
=
static_cast
<
BoxedBuiltinFunctionOrMethod
*>
(
b
);
assert
(
func
->
name
);
return
func
->
name
;
}
static
Box
*
functionNonzero
(
BoxedFunction
*
self
)
{
return
True
;
}
...
...
@@ -1302,6 +1327,8 @@ void setupRuntime() {
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedBuiltinFunctionOrMethod
,
modname
)));
builtin_function_or_method_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
builtinFunctionOrMethodRepr
,
STR
,
1
)));
builtin_function_or_method_cls
->
giveAttr
(
"__name__"
,
new
BoxedGetsetDescriptor
(
builtin_function_or_method_name
,
NULL
,
NULL
));
builtin_function_or_method_cls
->
freeze
();
instancemethod_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instancemethodRepr
,
STR
,
1
)));
...
...
src/runtime/types.h
View file @
3d94e842
...
...
@@ -476,10 +476,9 @@ public:
class
BoxedBuiltinFunctionOrMethod
:
public
BoxedFunctionBase
{
public:
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
)
:
BoxedFunctionBase
(
f
)
{}
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
std
::
initializer_list
<
Box
*>
defaults
,
BoxedClosure
*
closure
=
NULL
,
bool
isGenerator
=
false
)
:
BoxedFunctionBase
(
f
,
defaults
,
closure
,
isGenerator
)
{}
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
const
char
*
name
);
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
const
char
*
name
,
std
::
initializer_list
<
Box
*>
defaults
,
BoxedClosure
*
closure
=
NULL
,
bool
isGenerator
=
false
);
DEFAULT_CLASS
(
builtin_function_or_method_cls
);
};
...
...
class_and_func_name.py
→
test/tests/
class_and_func_name.py
View file @
3d94e842
...
...
@@ -32,6 +32,7 @@ set_name(int, "bob")
set_name
(
C
,
5
)
set_name
(
C
,
"b
\
0
b"
)
set_name
(
C
,
"car"
)
set_name
(
C
,
""
)
def
g
():
pass
...
...
@@ -39,6 +40,7 @@ print g.__name__
set_name
(
g
,
"bob"
)
set_name
(
g
,
5
)
set_name
(
g
,
"b
\
0
b"
)
set_name
(
g
,
""
)
f
=
lambda
x
:
5
print
f
.
__name__
...
...
@@ -46,3 +48,9 @@ set_name(f, "bob")
set_name
(
f
,
5
)
set_name
(
f
,
"b
\
0
b"
)
#del_name(f)
set_name
(
f
,
""
)
print
sorted
.
__name__
# should all fail:
set_name
(
sorted
,
"blah"
)
set_name
(
sorted
,
5
)
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