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
f6160517
Commit
f6160517
authored
Feb 23, 2015
by
Travis Hance
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
__name__ for builtin functions
parent
befacc74
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 @
f6160517
This diff is collapsed.
Click to expand it.
src/runtime/builtin_modules/gc.cpp
View file @
f6160517
...
@@ -40,9 +40,11 @@ static Box* enable() {
...
@@ -40,9 +40,11 @@ static Box* enable() {
void
setupGC
()
{
void
setupGC
()
{
BoxedModule
*
gc_module
=
createModule
(
"gc"
,
"__builtin__"
);
BoxedModule
*
gc_module
=
createModule
(
"gc"
,
"__builtin__"
);
gc_module
->
giveAttr
(
"collect"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
gcCollect
,
NONE
,
0
)));
gc_module
->
giveAttr
(
"collect"
,
gc_module
->
giveAttr
(
"isenabled"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
isEnabled
,
BOXED_BOOL
,
0
)));
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
gcCollect
,
NONE
,
0
),
"collect"
));
gc_module
->
giveAttr
(
"disable"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
disable
,
NONE
,
0
)));
gc_module
->
giveAttr
(
"isenabled"
,
gc_module
->
giveAttr
(
"enable"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
enable
,
NONE
,
0
)));
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 @
f6160517
...
@@ -51,6 +51,7 @@ static Box* setOption(Box* option, Box* value) {
...
@@ -51,6 +51,7 @@ static Box* setOption(Box* option, Box* value) {
void
setupPyston
()
{
void
setupPyston
()
{
pyston_module
=
createModule
(
"__pyston__"
,
"__builtin__"
);
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 @
f6160517
...
@@ -233,11 +233,12 @@ void setupSys() {
...
@@ -233,11 +233,12 @@ void setupSys() {
sys_module
->
giveAttr
(
"stdin"
,
new
BoxedFile
(
stdin
,
"<stdin>"
,
"r"
));
sys_module
->
giveAttr
(
"stdin"
,
new
BoxedFile
(
stdin
,
"<stdin>"
,
"r"
));
sys_module
->
giveAttr
(
"stderr"
,
new
BoxedFile
(
stderr
,
"<stderr>"
,
"w"
));
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
(
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
(
"warnoptions"
,
new
BoxedList
());
sys_module
->
giveAttr
(
"py3kwarning"
,
False
);
sys_module
->
giveAttr
(
"py3kwarning"
,
False
);
...
...
src/runtime/builtin_modules/thread.cpp
View file @
f6160517
...
@@ -169,14 +169,15 @@ Box* stackSize() {
...
@@ -169,14 +169,15 @@ Box* stackSize() {
void
setupThread
()
{
void
setupThread
()
{
thread_module
=
createModule
(
"thread"
,
"__builtin__"
);
thread_module
=
createModule
(
"thread"
,
"__builtin__"
);
thread_module
->
giveAttr
(
"start_new_thread"
,
thread_module
->
giveAttr
(
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
startNewThread
,
BOXED_INT
,
2
)));
"start_new_thread"
,
thread_module
->
giveAttr
(
"allocate_lock"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
startNewThread
,
BOXED_INT
,
2
),
"start_new_thread"
));
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
allocateLock
,
UNKNOWN
,
0
)));
thread_module
->
giveAttr
(
"allocate_lock"
,
new
BoxedBuiltinFunctionOrMethod
(
thread_module
->
giveAttr
(
"get_ident"
,
boxRTFunction
((
void
*
)
allocateLock
,
UNKNOWN
,
0
),
"allocate_lock"
));
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
getIdent
,
BOXED_INT
,
0
)));
thread_module
->
giveAttr
(
thread_module
->
giveAttr
(
"stack_size"
,
"get_ident"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
getIdent
,
BOXED_INT
,
0
),
"get_ident"
));
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
stackSize
,
BOXED_INT
,
0
)));
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
=
new
BoxedHeapClass
(
object_cls
,
NULL
,
0
,
sizeof
(
BoxedThreadLock
),
false
,
"lock"
);
thread_lock_cls
->
giveAttr
(
"__module__"
,
boxStrConstant
(
"thread"
));
thread_lock_cls
->
giveAttr
(
"__module__"
,
boxStrConstant
(
"thread"
));
...
...
src/runtime/import.cpp
View file @
f6160517
...
@@ -302,7 +302,7 @@ Box* impFindModule(Box* _name) {
...
@@ -302,7 +302,7 @@ Box* impFindModule(Box* _name) {
void
setupImport
()
{
void
setupImport
()
{
BoxedModule
*
imp_module
=
createModule
(
"imp"
,
"__builtin__"
);
BoxedModule
*
imp_module
=
createModule
(
"imp"
,
"__builtin__"
);
imp_module
->
giveAttr
(
"find_module"
,
imp_module
->
giveAttr
(
"find_module"
,
new
BoxedBuiltinFunctionOrMethod
(
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
impFindModule
,
UNKNOWN
,
1
)
));
boxRTFunction
((
void
*
)
impFindModule
,
UNKNOWN
,
1
),
"find_module"
));
}
}
}
}
src/runtime/objmodel.cpp
View file @
f6160517
...
@@ -1533,8 +1533,8 @@ bool dataDescriptorSetSpecialCases(Box* obj, Box* val, Box* descr, SetattrRewrit
...
@@ -1533,8 +1533,8 @@ bool dataDescriptorSetSpecialCases(Box* obj, Box* val, Box* descr, SetattrRewrit
// TODO type checking goes here
// TODO type checking goes here
if
(
getset_descr
->
set
==
NULL
)
{
if
(
getset_descr
->
set
==
NULL
)
{
raiseExcHelper
(
AttributeError
,
"attribute '%s' of '%s' object is not writable"
,
attr_name
.
c_str
(),
raiseExcHelper
(
AttributeError
,
"attribute '%s' of '%s' object
s
is not writable"
,
attr_name
.
c_str
(),
getTypeName
(
getset_descr
));
getTypeName
(
obj
));
}
}
if
(
rewrite_args
)
{
if
(
rewrite_args
)
{
...
...
src/runtime/types.cpp
View file @
f6160517
...
@@ -321,6 +321,19 @@ BoxedFunction::BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults
...
@@ -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
)
{
extern
"C"
void
functionGCHandler
(
GCVisitor
*
v
,
Box
*
b
)
{
boxGCHandler
(
v
,
b
);
boxGCHandler
(
v
,
b
);
...
@@ -685,6 +698,18 @@ static void func_set_name(Box* b, Box* v, void*) {
...
@@ -685,6 +698,18 @@ static void func_set_name(Box* b, Box* v, void*) {
func
->
name
=
static_cast
<
BoxedString
*>
(
v
);
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
)
{
static
Box
*
functionNonzero
(
BoxedFunction
*
self
)
{
return
True
;
return
True
;
}
}
...
@@ -1302,6 +1327,8 @@ void setupRuntime() {
...
@@ -1302,6 +1327,8 @@ void setupRuntime() {
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedBuiltinFunctionOrMethod
,
modname
)));
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedBuiltinFunctionOrMethod
,
modname
)));
builtin_function_or_method_cls
->
giveAttr
(
builtin_function_or_method_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
builtinFunctionOrMethodRepr
,
STR
,
1
)));
"__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
();
builtin_function_or_method_cls
->
freeze
();
instancemethod_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instancemethodRepr
,
STR
,
1
)));
instancemethod_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
instancemethodRepr
,
STR
,
1
)));
...
...
src/runtime/types.h
View file @
f6160517
...
@@ -476,10 +476,9 @@ public:
...
@@ -476,10 +476,9 @@ public:
class
BoxedBuiltinFunctionOrMethod
:
public
BoxedFunctionBase
{
class
BoxedBuiltinFunctionOrMethod
:
public
BoxedFunctionBase
{
public:
public:
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
)
:
BoxedFunctionBase
(
f
)
{}
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
const
char
*
name
);
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
std
::
initializer_list
<
Box
*>
defaults
,
BoxedClosure
*
closure
=
NULL
,
BoxedBuiltinFunctionOrMethod
(
CLFunction
*
f
,
const
char
*
name
,
std
::
initializer_list
<
Box
*>
defaults
,
bool
isGenerator
=
false
)
BoxedClosure
*
closure
=
NULL
,
bool
isGenerator
=
false
);
:
BoxedFunctionBase
(
f
,
defaults
,
closure
,
isGenerator
)
{}
DEFAULT_CLASS
(
builtin_function_or_method_cls
);
DEFAULT_CLASS
(
builtin_function_or_method_cls
);
};
};
...
...
class_and_func_name.py
→
test/tests/
class_and_func_name.py
View file @
f6160517
...
@@ -32,6 +32,7 @@ set_name(int, "bob")
...
@@ -32,6 +32,7 @@ set_name(int, "bob")
set_name
(
C
,
5
)
set_name
(
C
,
5
)
set_name
(
C
,
"b
\
0
b"
)
set_name
(
C
,
"b
\
0
b"
)
set_name
(
C
,
"car"
)
set_name
(
C
,
"car"
)
set_name
(
C
,
""
)
def
g
():
def
g
():
pass
pass
...
@@ -39,6 +40,7 @@ print g.__name__
...
@@ -39,6 +40,7 @@ print g.__name__
set_name
(
g
,
"bob"
)
set_name
(
g
,
"bob"
)
set_name
(
g
,
5
)
set_name
(
g
,
5
)
set_name
(
g
,
"b
\
0
b"
)
set_name
(
g
,
"b
\
0
b"
)
set_name
(
g
,
""
)
f
=
lambda
x
:
5
f
=
lambda
x
:
5
print
f
.
__name__
print
f
.
__name__
...
@@ -46,3 +48,9 @@ set_name(f, "bob")
...
@@ -46,3 +48,9 @@ set_name(f, "bob")
set_name
(
f
,
5
)
set_name
(
f
,
5
)
set_name
(
f
,
"b
\
0
b"
)
set_name
(
f
,
"b
\
0
b"
)
#del_name(f)
#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