Commit 2f1c338a authored by da-woods's avatar da-woods Committed by Stefan Behnel

Fixed over-zealous optimization of append attribute usage to "__Pyx_PyObject_Append" (GH-4834)

Fixes https://github.com/cython/cython/issues/4828
parent e4ef0c1e
...@@ -306,8 +306,8 @@ class ExprNode(Node): ...@@ -306,8 +306,8 @@ class ExprNode(Node):
# Cached result of subexpr_nodes() # Cached result of subexpr_nodes()
# use_managed_ref boolean use ref-counted temps/assignments/etc. # use_managed_ref boolean use ref-counted temps/assignments/etc.
# result_is_used boolean indicates that the result will be dropped and the # result_is_used boolean indicates that the result will be dropped and the
# is_numpy_attribute boolean Is a Numpy module attribute
# result_code/temp_result can safely be set to None # result_code/temp_result can safely be set to None
# is_numpy_attribute boolean Is a Numpy module attribute
# annotation ExprNode or None PEP526 annotation for names or expressions # annotation ExprNode or None PEP526 annotation for names or expressions
result_ctype = None result_ctype = None
......
...@@ -2860,7 +2860,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2860,7 +2860,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
"""Optimistic optimisation as X.append() is almost always """Optimistic optimisation as X.append() is almost always
referring to a list. referring to a list.
""" """
if len(args) != 2 or node.result_is_used: if len(args) != 2 or node.result_is_used or node.function.entry:
return node return node
return ExprNodes.PythonCapiCallNode( return ExprNodes.PythonCapiCallNode(
......
cimport cython
class A: class A:
def append(self, x): def append(self, x):
print u"appending", x print u"appending", x
...@@ -94,3 +96,35 @@ def method_name(): ...@@ -94,3 +96,35 @@ def method_name():
'append' 'append'
""" """
return [].append.__name__ return [].append.__name__
@cython.test_assert_path_exists(
'//PythonCapiCallNode')
def append_optimized(probably_list):
"""
>>> l = []
>>> append_optimized(l)
>>> l
[1]
"""
probably_list.append(1)
cdef class AppendBug:
# https://github.com/cython/cython/issues/4828
# if the attribute "append" is found it shouldn't be replaced with
# __Pyx_PyObject_Append
cdef object append
def __init__(self, append):
self.append = append
@cython.test_fail_if_path_exists(
'//PythonCapiCallNode')
def specific_attribute(AppendBug a):
"""
>>> def append_to_default_arg(a, arg=[]):
... arg.append(a)
... return arg
>>> specific_attribute(AppendBug(append_to_default_arg))
>>> append_to_default_arg(None)
[1, None]
"""
a.append(1)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment