diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index 59378acf626952fef6de3c2eab85a373a8ac97f3..7435180f8070c1b1dde76c1123d8a502f4d7e3b5 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -457,6 +457,8 @@ builtin_types_table = [ ("dict", "PyDict_Type", [BuiltinMethod("items", "T", "O", "PyDict_Items"), # FIXME: Py3 mode? BuiltinMethod("keys", "T", "O", "PyDict_Keys"), # FIXME: Py3 mode? BuiltinMethod("values","T", "O", "PyDict_Values"), # FIXME: Py3 mode? + BuiltinMethod("clear", "T", "r", "__Pyx_PyDict_Clear", + utility_code = UtilityCode.load_cached("py_dict_clear", "Optimize.c")), BuiltinMethod("copy", "T", "T", "PyDict_Copy")]), ("slice", "PySlice_Type", [BuiltinAttribute('start'), diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index ba068e2d74f0f2ec6d1da083506cca1b163323d3..489d15da508d352b4812d2ca677142949f925981 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -2288,34 +2288,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ### methods of builtin types - PyDict_Clear_func_type = PyrexTypes.CFuncType( - PyrexTypes.c_void_type, [ - PyrexTypes.CFuncTypeArg("dict", Builtin.dict_type, None) - ]) - - PyDict_Clear_Retval_func_type = PyrexTypes.CFuncType( - PyrexTypes.py_object_type, [ - PyrexTypes.CFuncTypeArg("dict", Builtin.dict_type, None) - ]) - - def _handle_simple_method_dict_clear(self, node, args, is_unbound_method): - """Optimise dict.clear() differently, depending on the use (or - non-use) of the return value. - """ - if len(args) != 1: - return node - if node.result_is_used: - return self._substitute_method_call( - node, "__Pyx_PyDict_Clear", self.PyDict_Clear_Retval_func_type, - 'clear', is_unbound_method, args, - may_return_none=True, is_temp=True, - utility_code=load_c_utility('py_dict_clear') - ).coerce_to(node.type, self.current_env) - else: - return self._substitute_method_call( - node, "PyDict_Clear", self.PyDict_Clear_func_type, - 'clear', is_unbound_method, args, is_temp=False) - PyObject_Append_func_type = PyrexTypes.CFuncType( PyrexTypes.py_object_type, [ PyrexTypes.CFuncTypeArg("list", PyrexTypes.py_object_type, None), diff --git a/Cython/Utility/Optimize.c b/Cython/Utility/Optimize.c index 7832ac6bd33f815da301a0c89ad3768dcd46e2dc..72126f59884bf4e09454513874df1521b7de6e9c 100644 --- a/Cython/Utility/Optimize.c +++ b/Cython/Utility/Optimize.c @@ -309,11 +309,7 @@ static PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *d /////////////// py_dict_clear.proto /////////////// -static CYTHON_INLINE PyObject* __Pyx_PyDict_Clear(PyObject* d) { - PyDict_Clear(d); - Py_INCREF(Py_None); - return Py_None; -} +#define __Pyx_PyDict_Clear(d) (PyDict_Clear(d), 0) /////////////// dict_iter.proto /////////////// diff --git a/tests/run/builtin_methods_return_values.pyx b/tests/run/builtin_methods_return_values.pyx index 0d17f64560b3c1f40080821e101586601357a55e..50f1427ca8427469e643a98042a122a8ba59002d 100644 --- a/tests/run/builtin_methods_return_values.pyx +++ b/tests/run/builtin_methods_return_values.pyx @@ -68,6 +68,9 @@ def set_clear(set s): """ >>> set_clear(_set([1,2,3])) is None True + >>> set_clear(None) + Traceback (most recent call last): + AttributeError: 'NoneType' object has no attribute 'clear' """ s.clear() return s.clear() @@ -92,6 +95,9 @@ def dict_clear(dict d): """ >>> dict_clear({1:2,3:4}) is None True + >>> dict_clear(None) + Traceback (most recent call last): + AttributeError: 'NoneType' object has no attribute 'clear' """ d.clear() return d.clear()