Commit c4ba71f4 authored by Robert Bradshaw's avatar Robert Bradshaw

Raise TypeError for non-string hasattr.

This fixes #1702.
parent aa669574
......@@ -111,7 +111,7 @@ builtin_function_table = [
t.real_type, [
PyrexTypes.CFuncTypeArg("arg", t, None)
],
is_strict_signature = True, nogil=True))
is_strict_signature = True, nogil=True))
for t in (PyrexTypes.c_float_complex_type,
PyrexTypes.c_double_complex_type,
PyrexTypes.c_longdouble_complex_type)
......@@ -144,7 +144,8 @@ builtin_function_table = [
utility_code=getattr3_utility_code),
BuiltinFunction('getattr', "OO", "O", "__Pyx_GetAttr",
utility_code=getattr_utility_code),
BuiltinFunction('hasattr', "OO", "b", "PyObject_HasAttr"),
BuiltinFunction('hasattr', "OO", "b", "__Pyx_HasAttr",
utility_code = UtilityCode.load("HasAttr", "Builtins.c")),
BuiltinFunction('hash', "O", "h", "PyObject_Hash"),
#('hex', "", "", ""),
#('id', "", "", ""),
......
......@@ -183,6 +183,30 @@ bad:
return NULL;
}
//////////////////// HasAttr.proto ////////////////////
static CYTHON_INLINE int __Pyx_HasAttr(PyObject *, PyObject *); /*proto*/
//////////////////// HasAttr ////////////////////
//@requires: ObjectHandling.c::GetAttr
static CYTHON_INLINE int __Pyx_HasAttr(PyObject *o, PyObject *n) {
PyObject *r;
if (unlikely(!__Pyx_PyBaseString_Check(n))) {
PyErr_SetString(PyExc_TypeError,
"hasattr(): attribute name must be string");
return -1;
}
r = __Pyx_GetAttr(o, n);
if (unlikely(!r)) {
PyErr_Clear();
return 0;
} else {
Py_DECREF(r);
return 1;
}
}
//////////////////// Intern.proto ////////////////////
static PyObject* __Pyx_Intern(PyObject* s); /* proto */
......
......@@ -17,6 +17,8 @@ def wrap_hasattr(obj, name):
True
>>> wrap_hasattr(Foo(), "foo")
True
>>> wrap_hasattr(Foo(), u"foo")
True
>>> wrap_hasattr(Foo(), "spam")
False
>>> wrap_hasattr(Foo(), "bar")
......@@ -27,5 +29,9 @@ def wrap_hasattr(obj, name):
ZeroDivisionError: ...
>>> wrap_hasattr(Foo(), "baz")
False
>>> hasattr(Foo(), None) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: hasattr(): attribute name must be string
"""
return hasattr(obj, name)
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