From ac3536e95656434aefad62112bc5bc494c139771 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 31 Oct 2009 14:17:41 +0100 Subject: [PATCH] add a None check, except for plain extension type names --- Cython/Compiler/Optimize.py | 8 +++++++- tests/run/tp_new.pyx | 26 +++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index c26a32a5..44a9cda0 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -1076,9 +1076,15 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): PyrexTypes.CFuncTypeArg("type", PyrexTypes.py_object_type, None) ]) + if not type_arg.type_entry: + # arbitrary variable, needs a None check for safety + type_arg = ExprNodes.NoneCheckNode( + type_arg, "PyExc_TypeError", + "object.__new__(X): X is not a type object (NoneType)") + return ExprNodes.PythonCapiCallNode( node.pos, "__Pyx_tp_new", func_type, - args = args, + args = [type_arg], utility_code = tpnew_utility_code, is_temp = node.is_temp ) diff --git a/tests/run/tp_new.pyx b/tests/run/tp_new.pyx index 87ad202e..d176fc3b 100644 --- a/tests/run/tp_new.pyx +++ b/tests/run/tp_new.pyx @@ -41,6 +41,17 @@ def make_new_builtin(): m = tuple.__new__(tuple) return m +@cython.test_assert_path_exists('//PythonCapiCallNode') +@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode') +def make_new_none(type t=None): + """ + >>> isinstance(make_new_none(), MyType) + Traceback (most recent call last): + TypeError: object.__new__(X): X is not a type object (NoneType) + """ + m = t.__new__(t) + return m + # these cannot: @cython.test_assert_path_exists('//SimpleCallNode/AttributeNode') @@ -81,24 +92,13 @@ def make_new_args(type t1=None, type t2=None): @cython.test_assert_path_exists('//SimpleCallNode/AttributeNode') @cython.test_fail_if_path_exists('//PythonCapiCallNode') -def make_new_none(type t1=None, type t2=None): - """ - >>> isinstance(make_new_none(), MyType) - Traceback (most recent call last): - TypeError: object.__new__(X): X is not a type object (NoneType) - """ - m = t1.__new__(t2) - return m - -@cython.test_assert_path_exists('//SimpleCallNode/AttributeNode') -@cython.test_fail_if_path_exists('//PythonCapiCallNode') -def make_new_none_typed(tuple t1=None, tuple t2=None): +def make_new_none_typed(tuple t=None): """ >>> isinstance(make_new_none(), MyType) Traceback (most recent call last): TypeError: object.__new__(X): X is not a type object (NoneType) """ - m = t1.__new__(t2) + m = t.__new__(t) return m @cython.test_assert_path_exists('//SimpleCallNode/AttributeNode') -- 2.26.2