From 9215510dc86327705bec40c1c03e45c2ff59e427 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 21 Jul 2010 00:44:02 +0200 Subject: [PATCH] fix 'set' type checks (prevent match with frozenset) --- Cython/Compiler/ModuleNode.py | 6 ++++++ Cython/Compiler/PyrexTypes.py | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 143ccbfb..15022436 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -548,6 +548,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): #define PyBytes_Repr PyString_Repr #define PyBytes_Concat PyString_Concat #define PyBytes_ConcatAndDel PyString_ConcatAndDel + #define PySet_Check(obj) PyObject_TypeCheck(obj, &PySet_Type) + #define PyFrozenSet_Check(obj) PyObject_TypeCheck(obj, &PyFrozenSet_Type) +#endif + +#ifndef PySet_CheckExact +# define PySet_CheckExact(obj) (Py_TYPE(obj) == PySet_Type) #endif #if PY_MAJOR_VERSION >= 3 diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index f1ad4eaf..eafe8e36 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -416,20 +416,27 @@ class BuiltinObjectType(PyObjectType): def subtype_of(self, type): return type.is_pyobject and self.assignable_from(type) - - def type_test_code(self, arg, notnone=False): + + def type_check_function(self, exact=True): type_name = self.name + if type_name == 'bool': + return 'PyBool_Check' + if type_name == 'str': - type_check = 'PyString_CheckExact' - elif type_name == 'set': - type_check = 'PyAnySet_CheckExact' + type_check = 'PyString_Check' elif type_name == 'frozenset': - type_check = 'PyFrozenSet_CheckExact' - elif type_name == 'bool': - type_check = 'PyBool_Check' + type_check = 'PyFrozenSet_Check' else: - type_check = 'Py%s_CheckExact' % type_name.capitalize() + type_check = 'Py%s_Check' % type_name.capitalize() + if exact: + type_check += 'Exact' + return type_check + + def isinstance_code(self, arg): + return '%s(%s)' % (self.type_check_function(exact=False), arg) + def type_test_code(self, arg, notnone=False): + type_check = self.type_check_function(exact=True) check = 'likely(%s(%s))' % (type_check, arg) if not notnone: check = check + ('||((%s) == Py_None)' % arg) -- 2.26.2