Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
0a9f0bd0
Commit
0a9f0bd0
authored
Sep 10, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
let func.__code__.varnames include local variable names
parent
36780b2a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
21 deletions
+45
-21
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+5
-5
tests/run/cyfunction.pyx
tests/run/cyfunction.pyx
+40
-16
No files found.
Cython/Compiler/ExprNodes.py
View file @
0a9f0bd0
...
@@ -5256,7 +5256,7 @@ class CodeObjectNode(ExprNode):
...
@@ -5256,7 +5256,7 @@ class CodeObjectNode(ExprNode):
# Create a PyCodeObject for a CyFunction instance.
# Create a PyCodeObject for a CyFunction instance.
#
#
# def_node DefNode the Python function node
# def_node DefNode the Python function node
# varnames TupleNode a tuple with all variable names
# varnames TupleNode a tuple with all
local
variable names
subexprs
=
[
'varnames'
]
subexprs
=
[
'varnames'
]
is_temp
=
False
is_temp
=
False
...
@@ -5268,12 +5268,12 @@ class CodeObjectNode(ExprNode):
...
@@ -5268,12 +5268,12 @@ class CodeObjectNode(ExprNode):
args
.
append
(
def_node
.
star_arg
)
args
.
append
(
def_node
.
star_arg
)
if
def_node
.
starstar_arg
:
if
def_node
.
starstar_arg
:
args
.
append
(
def_node
.
starstar_arg
)
args
.
append
(
def_node
.
starstar_arg
)
# FIXME: lacks non-args !
local_vars
=
def_node
.
local_scope
.
var_entries
self
.
varnames
=
TupleNode
(
self
.
varnames
=
TupleNode
(
def_node
.
pos
,
def_node
.
pos
,
args
=
[
IdentifierStringNode
(
arg
.
pos
,
unicode_value
=
arg
.
name
,
args
=
[
IdentifierStringNode
(
arg
.
pos
,
unicode_value
=
arg
.
name
,
value
=
StringEncoding
.
BytesLiteral
(
arg
.
name
.
utf8encode
()))
value
=
StringEncoding
.
BytesLiteral
(
arg
.
name
.
utf8encode
()))
for
arg
in
args
],
for
arg
in
args
+
local_vars
],
is_temp
=
0
,
is_temp
=
0
,
is_literal
=
1
)
is_literal
=
1
)
...
@@ -5299,8 +5299,8 @@ class CodeObjectNode(ExprNode):
...
@@ -5299,8 +5299,8 @@ class CodeObjectNode(ExprNode):
len
(
self
.
varnames
.
args
),
# nlocals
len
(
self
.
varnames
.
args
),
# nlocals
Naming
.
empty_bytes
,
# code
Naming
.
empty_bytes
,
# code
Naming
.
empty_tuple
,
# consts
Naming
.
empty_tuple
,
# consts
Naming
.
empty_tuple
,
# names (FIXME
: all local non-args!
)
Naming
.
empty_tuple
,
# names (FIXME)
self
.
varnames
.
result
(),
# varnames
self
.
varnames
.
result
(),
# varnames
Naming
.
empty_tuple
,
# freevars (FIXME)
Naming
.
empty_tuple
,
# freevars (FIXME)
Naming
.
empty_tuple
,
# cellvars (FIXME)
Naming
.
empty_tuple
,
# cellvars (FIXME)
file_path_const
,
# filename
file_path_const
,
# filename
...
...
tests/run/cyfunction.pyx
View file @
0a9f0bd0
...
@@ -93,17 +93,25 @@ def codeof(func):
...
@@ -93,17 +93,25 @@ def codeof(func):
else
:
else
:
return
func
.
func_code
return
func
.
func_code
def
cy_no_arg
():
pass
def
cy_no_arg
():
def
cy_one_arg
(
a
):
pass
l
=
0.5
def
cy_two_args
(
x
,
b
):
pass
m
=
1
def
cy_default_args
(
x
=
1
,
b
=
2
):
pass
def
cy_one_arg
(
a
):
l
=
0.5
m
=
1
def
cy_two_args
(
x
,
b
):
l
=
0.5
m
=
1
def
cy_default_args
(
x
=
1
,
b
=
2
):
l
=
0.5
m
=
1
def
test_code
():
def
test_code
():
"""
"""
>>> def no_arg():
pass
>>> def no_arg():
l = m = 1
>>> def one_arg(a):
pass
>>> def one_arg(a):
l = m = 1
>>> def two_args(x, b):
pass
>>> def two_args(x, b):
l = m = 1
>>> def default_args(x=1, b=2):
pass
>>> def default_args(x=1, b=2):
l = m = 1
>>> codeof(no_arg).co_argcount
>>> codeof(no_arg).co_argcount
0
0
...
@@ -113,10 +121,14 @@ def test_code():
...
@@ -113,10 +121,14 @@ def test_code():
no_arg
no_arg
>>> print(codeof(cy_no_arg).co_name)
>>> print(codeof(cy_no_arg).co_name)
cy_no_arg
cy_no_arg
>>> codeof(no_arg).co_
var
names
>>> codeof(no_arg).co_names
()
()
>>> codeof(cy_no_arg).co_
var
names
>>> codeof(cy_no_arg).co_names
()
()
>>> codeof(no_arg).co_varnames
('l', 'm')
>>> codeof(cy_no_arg).co_varnames
('l', 'm')
>>> codeof(one_arg).co_argcount
>>> codeof(one_arg).co_argcount
1
1
...
@@ -126,26 +138,38 @@ def test_code():
...
@@ -126,26 +138,38 @@ def test_code():
one_arg
one_arg
>>> print(codeof(cy_one_arg).co_name)
>>> print(codeof(cy_one_arg).co_name)
cy_one_arg
cy_one_arg
>>> codeof(one_arg).co_names
()
>>> codeof(cy_one_arg).co_names
()
>>> codeof(one_arg).co_varnames
>>> codeof(one_arg).co_varnames
('a',)
('a',
'l', 'm'
)
>>> codeof(cy_one_arg).co_varnames
>>> codeof(cy_one_arg).co_varnames
('a',)
('a',
'l', 'm'
)
>>> codeof(two_args).co_argcount
>>> codeof(two_args).co_argcount
2
2
>>> codeof(cy_two_args).co_argcount
>>> codeof(cy_two_args).co_argcount
2
2
>>> codeof(two_args).co_names
()
>>> codeof(cy_two_args).co_names
()
>>> codeof(two_args).co_varnames
>>> codeof(two_args).co_varnames
('x', 'b')
('x', 'b'
, 'l', 'm'
)
>>> codeof(cy_two_args).co_varnames
>>> codeof(cy_two_args).co_varnames
('x', 'b')
('x', 'b'
, 'l', 'm'
)
>>> codeof(default_args).co_argcount
>>> codeof(default_args).co_argcount
2
2
>>> codeof(cy_default_args).co_argcount
>>> codeof(cy_default_args).co_argcount
2
2
>>> codeof(default_args).co_names
()
>>> codeof(cy_default_args).co_names
()
>>> codeof(default_args).co_varnames
>>> codeof(default_args).co_varnames
('x', 'b')
('x', 'b'
, 'l', 'm'
)
>>> codeof(cy_default_args).co_varnames
>>> codeof(cy_default_args).co_varnames
('x', 'b')
('x', 'b'
, 'l', 'm'
)
"""
"""
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