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
4f05de43
Commit
4f05de43
authored
Apr 13, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ticket #685: implement no-arg dir()
parent
69cd1c34
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
22 deletions
+55
-22
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+27
-17
tests/run/locals.pyx
tests/run/locals.pyx
+28
-5
No files found.
Cython/Compiler/ParseTreeTransforms.py
View file @
4f05de43
...
...
@@ -1750,25 +1750,35 @@ class TransformBuiltinMethods(EnvTransform):
error
(
node
.
pos
,
u"'%s' not a valid cython attribute or is being used incorrectly"
%
attribute
)
return
node
def
visit_SimpleCallNode
(
self
,
node
):
def
_inject_locals
(
self
,
node
,
func_name
):
# locals()/dir() builtins
lenv
=
self
.
current_env
()
entry
=
lenv
.
lookup_here
(
func_name
)
if
entry
:
# not the builtin
return
node
max_args_count
=
{
'dir'
:
1
,
'locals'
:
0
}[
func_name
]
if
len
(
node
.
args
)
>
max_args_count
:
error
(
self
.
pos
,
"Builtin '%s()' called with wrong number of args, expected %d, got %d"
%
(
func_name
,
max_args_count
,
len
(
node
.
args
)))
return
node
pos
=
node
.
pos
if
func_name
==
'locals'
:
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
:
items
=
[
ExprNodes
.
StringNode
(
pos
,
value
=
var
)
for
var
in
lenv
.
entries
]
return
ExprNodes
.
ListNode
(
pos
,
args
=
items
)
# locals builtin
def
visit_SimpleCallNode
(
self
,
node
):
if
isinstance
(
node
.
function
,
ExprNodes
.
NameNode
):
if
node
.
function
.
name
==
'locals'
:
lenv
=
self
.
current_env
()
entry
=
lenv
.
lookup_here
(
'locals'
)
if
entry
:
# not the builtin 'locals'
return
node
if
len
(
node
.
args
)
>
0
:
error
(
self
.
pos
,
"Builtin 'locals()' called with wrong number of args, expected 0, got %d"
%
len
(
node
.
args
))
return
node
pos
=
node
.
pos
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
)
func_name
=
node
.
function
.
name
if
func_name
in
(
'dir'
,
'locals'
):
return
self
.
_inject_locals
(
node
,
func_name
)
# cython.foo
function
=
node
.
function
.
as_cython_attribute
()
...
...
tests/run/locals.pyx
View file @
4f05de43
__doc__
=
u"""
>>> sorted( get_locals(1,2,3, k=5) .items())
[('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
"""
# mode: run
# tag: builtins, locals, dir
def
get_locals
(
x
,
*
args
,
**
kwds
):
"""
>>> 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_dir
(
x
,
*
args
,
**
kwds
):
"""
>>> sorted( get_dir(1,2,3, k=5) )
['args', 'kwds', 'x', 'y', 'z']
"""
cdef
int
z
=
5
y
=
"hi"
return
dir
()
def
in_locals
(
x
,
*
args
,
**
kwds
):
"""
>>> in_locals('z')
...
...
@@ -22,6 +32,19 @@ def in_locals(x, *args, **kwds):
y
=
"hi"
return
x
in
locals
()
def
in_dir
(
x
,
*
args
,
**
kwds
):
"""
>>> in_dir('z')
True
>>> in_dir('args')
True
>>> in_dir('X')
False
"""
cdef
int
z
=
5
y
=
"hi"
return
x
in
dir
()
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