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
5c2214f9
Commit
5c2214f9
authored
7 years ago
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix #2047: optimize dict.pop() using _PyDict_Pop()
parent
e1ae63dd
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
0 deletions
+55
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+21
-0
tests/run/dict_pop.pyx
tests/run/dict_pop.pyx
+34
-0
No files found.
Cython/Compiler/Optimize.py
View file @
5c2214f9
...
...
@@ -2913,6 +2913,27 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
may_return_none
=
True
,
utility_code
=
load_c_utility
(
'dict_setdefault'
))
PyDict_Pop_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
py_object_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"dict"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"key"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"default"
,
PyrexTypes
.
py_object_type
,
None
),
])
def
_handle_simple_method_dict_pop
(
self
,
node
,
function
,
args
,
is_unbound_method
):
"""Replace dict.pop() by a call to _PyDict_Pop().
"""
if
len
(
args
)
==
2
:
args
.
append
(
ExprNodes
.
NullNode
(
node
.
pos
))
elif
len
(
args
)
!=
3
:
self
.
_error_wrong_arg_count
(
'dict.pop'
,
node
,
args
,
"2 or 3"
)
return
node
return
self
.
_substitute_method_call
(
node
,
function
,
"_PyDict_Pop"
,
self
.
PyDict_Pop_func_type
,
'split'
,
is_unbound_method
,
args
)
Pyx_PyInt_BinopInt_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
py_object_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"op1"
,
PyrexTypes
.
py_object_type
,
None
),
...
...
This diff is collapsed.
Click to expand it.
tests/run/dict_pop.pyx
0 → 100644
View file @
5c2214f9
cimport
cython
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
)
@
cython
.
test_fail_if_path_exists
(
"//AttributeNode"
)
def
dict_pop
(
dict
d
,
key
):
"""
>>> d = { 1: 10, 2: 20 }
>>> dict_pop(d, 1)
(10, {2: 20})
>>> dict_pop(d, 3)
Traceback (most recent call last):
KeyError: 3
>>> dict_pop(d, 2)
(20, {})
"""
return
d
.
pop
(
key
),
d
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
)
@
cython
.
test_fail_if_path_exists
(
"//AttributeNode"
)
def
dict_pop_default
(
dict
d
,
key
,
default
):
"""
>>> d = { 1: 10, 2: 20 }
>>> dict_pop_default(d, 1, "default")
(10, {2: 20})
>>> dict_pop_default(d, 3, None)
(None, {2: 20})
>>> dict_pop_default(d, 3, "default")
('default', {2: 20})
>>> dict_pop_default(d, 2, "default")
(20, {})
"""
return
d
.
pop
(
key
,
default
),
d
This diff is collapsed.
Click to expand it.
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