Commit c4ba71f4 authored by Robert Bradshaw's avatar Robert Bradshaw

Raise TypeError for non-string hasattr.

This fixes #1702.
parent aa669574
...@@ -144,7 +144,8 @@ builtin_function_table = [ ...@@ -144,7 +144,8 @@ builtin_function_table = [
utility_code=getattr3_utility_code), utility_code=getattr3_utility_code),
BuiltinFunction('getattr', "OO", "O", "__Pyx_GetAttr", BuiltinFunction('getattr', "OO", "O", "__Pyx_GetAttr",
utility_code=getattr_utility_code), 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"), BuiltinFunction('hash', "O", "h", "PyObject_Hash"),
#('hex', "", "", ""), #('hex', "", "", ""),
#('id', "", "", ""), #('id', "", "", ""),
......
...@@ -183,6 +183,30 @@ bad: ...@@ -183,6 +183,30 @@ bad:
return NULL; 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 //////////////////// //////////////////// Intern.proto ////////////////////
static PyObject* __Pyx_Intern(PyObject* s); /* proto */ static PyObject* __Pyx_Intern(PyObject* s); /* proto */
......
...@@ -17,6 +17,8 @@ def wrap_hasattr(obj, name): ...@@ -17,6 +17,8 @@ def wrap_hasattr(obj, name):
True True
>>> wrap_hasattr(Foo(), "foo") >>> wrap_hasattr(Foo(), "foo")
True True
>>> wrap_hasattr(Foo(), u"foo")
True
>>> wrap_hasattr(Foo(), "spam") >>> wrap_hasattr(Foo(), "spam")
False False
>>> wrap_hasattr(Foo(), "bar") >>> wrap_hasattr(Foo(), "bar")
...@@ -27,5 +29,9 @@ def wrap_hasattr(obj, name): ...@@ -27,5 +29,9 @@ def wrap_hasattr(obj, name):
ZeroDivisionError: ... ZeroDivisionError: ...
>>> wrap_hasattr(Foo(), "baz") >>> wrap_hasattr(Foo(), "baz")
False False
>>> hasattr(Foo(), None) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: hasattr(): attribute name must be string
""" """
return hasattr(obj, name) 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