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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
8e7816d2
Commit
8e7816d2
authored
May 09, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement read-only 0-arg vars() as locals()
parent
40fe046a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
8 deletions
+35
-8
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+12
-7
tests/run/locals.pyx
tests/run/locals.pyx
+23
-1
No files found.
Cython/Compiler/ParseTreeTransforms.py
View file @
8e7816d2
...
...
@@ -2060,29 +2060,34 @@ class TransformBuiltinMethods(EnvTransform):
return
node
def
_inject_locals
(
self
,
node
,
func_name
):
# locals()/dir() builtins
# locals()/dir()
/vars()
builtins
lenv
=
self
.
current_env
()
entry
=
lenv
.
lookup_here
(
func_name
)
if
entry
:
# not the builtin
return
node
pos
=
node
.
pos
if
func_name
==
'locals'
:
if
len
(
node
.
args
)
>
0
:
if
func_name
in
(
'locals'
,
'vars'
)
:
if
func_name
==
'locals'
and
len
(
node
.
args
)
>
0
:
error
(
self
.
pos
,
"Builtin 'locals()' called with wrong number of args, expected 0, got %d"
%
len
(
node
.
args
))
return
node
elif
func_name
==
'vars'
:
if
len
(
node
.
args
)
>
1
:
error
(
self
.
pos
,
"Builtin 'vars()' called with wrong number of args, expected 0-1, got %d"
%
len
(
node
.
args
))
if
len
(
node
.
args
)
>
0
:
return
node
# nothing to do
items
=
[
ExprNodes
.
DictItemNode
(
pos
,
key
=
ExprNodes
.
StringNode
(
pos
,
value
=
var
),
value
=
ExprNodes
.
NameNode
(
pos
,
name
=
var
))
for
var
in
lenv
.
entries
]
return
ExprNodes
.
DictNode
(
pos
,
key_value_pairs
=
items
)
else
:
else
:
# dir()
if
len
(
node
.
args
)
>
1
:
error
(
self
.
pos
,
"Builtin 'dir()' called with wrong number of args, expected 0-1, got %d"
%
len
(
node
.
args
))
return
node
elif
len
(
node
.
args
)
==
1
:
if
len
(
node
.
args
)
>
0
:
# optimised in Builtin.py
return
node
items
=
[
ExprNodes
.
StringNode
(
pos
,
value
=
var
)
for
var
in
lenv
.
entries
]
...
...
@@ -2091,7 +2096,7 @@ class TransformBuiltinMethods(EnvTransform):
def
visit_SimpleCallNode
(
self
,
node
):
if
isinstance
(
node
.
function
,
ExprNodes
.
NameNode
):
func_name
=
node
.
function
.
name
if
func_name
in
(
'dir'
,
'locals'
):
if
func_name
in
(
'dir'
,
'locals'
,
'vars'
):
return
self
.
_inject_locals
(
node
,
func_name
)
# cython.foo
...
...
tests/run/locals.pyx
View file @
8e7816d2
...
...
@@ -3,13 +3,22 @@
def
get_locals
(
x
,
*
args
,
**
kwds
):
"""
>>> sorted( get_locals(1,2,3, k=5)
.items()
)
>>> sorted( get_locals(1,2,3, k=5)
.items()
)
[('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
"""
cdef
int
z
=
5
y
=
"hi"
return
locals
()
def
get_vars
(
x
,
*
args
,
**
kwds
):
"""
>>> sorted( get_vars(1,2,3, k=5).items() )
[('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
"""
cdef
int
z
=
5
y
=
"hi"
return
vars
()
def
get_dir
(
x
,
*
args
,
**
kwds
):
"""
>>> sorted( get_dir(1,2,3, k=5) )
...
...
@@ -45,6 +54,19 @@ def in_dir(x, *args, **kwds):
y
=
"hi"
return
x
in
dir
()
def
in_vars
(
x
,
*
args
,
**
kwds
):
"""
>>> in_vars('z')
True
>>> in_vars('args')
True
>>> in_vars('X')
False
"""
cdef
int
z
=
5
y
=
"hi"
return
x
in
vars
()
def
sorted
(
it
):
l
=
list
(
it
)
l
.
sort
()
...
...
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