hasattr() swallows any exception (Py<3.2)
Take a look here: http://bugs.python.org/issue9666 'hasattr' default behaviour should be changed to suppress only AttributeError exceptions. Other should pass through. Should we do something about this in Cython? We currently use PyObject_HasAttr(), but even in Python 3.2 this is not the same as builtins.hasattr(), it swallows any exception (do you think this is a bug in core Python?). I'm inclined to fix the behavior for ALL Python versions to suppress only AttributeError. -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo 3000 Santa Fe, Argentina Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169
Lisandro Dalcin, 22.02.2011 21:41:
Take a look here: http://bugs.python.org/issue9666
'hasattr' default behaviour should be changed to suppress only AttributeError exceptions. Other should pass through.
+1, I think I even faintly recall that discussion. What a lengthy thread... http://mail.python.org/pipermail/python-dev/2010-August/103178.html I don't even recall when I last used hasattr(). Given how dumb it is to ask for an attribute, let someone else throw it away for you and then ask again to get it, I don't see much point in using it at all. I can write exception catching code myself, thanks.
Should we do something about this in Cython? We currently use PyObject_HasAttr(), but even in Python 3.2 this is not the same as builtins.hasattr(), it swallows any exception (do you think this is a bug in core Python?).
It was at least considered: http://mail.python.org/pipermail/python-dev/2010-August/103203.html
I'm inclined to fix the behavior for ALL Python versions to suppress only AttributeError.
How? Would you implement a hasattr() helper that uses PyObject_GetAttr() and Does The Right Thing? In any case, I personally don't care so much about hasattr(), but I'm +1 for Python compatibility, and +1 for getting the fixed Py3.2 behaviour in all Python versions. There's a reason this was fixed in CPython, it's a pretty clear bug. Stefan
On 22 February 2011 18:21, Stefan Behnel <stefan_ml@behnel.de> wrote:
Lisandro Dalcin, 22.02.2011 21:41:
I'm inclined to fix the behavior for ALL Python versions to suppress only AttributeError.
How? Would you implement a hasattr() helper that uses PyObject_GetAttr() and Does The Right Thing?
Yes, more or less the implementation of builtin_hasattr from bltinmodule.c from Py3.2
In any case, I personally don't care so much about hasattr(), but I'm +1 for Python compatibility, and +1 for getting the fixed Py3.2 behaviour in all Python versions. There's a reason this was fixed in CPython, it's a pretty clear bug.
OK, I'll go for it. Thanks. -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo 3000 Santa Fe, Argentina Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169
On 22 February 2011 19:09, Lisandro Dalcin <dalcinl@gmail.com> wrote:
On 22 February 2011 18:21, Stefan Behnel <stefan_ml@behnel.de> wrote:
Lisandro Dalcin, 22.02.2011 21:41:
I'm inclined to fix the behavior for ALL Python versions to suppress only AttributeError.
How? Would you implement a hasattr() helper that uses PyObject_GetAttr() and Does The Right Thing?
Yes, more or less the implementation of builtin_hasattr from bltinmodule.c from Py3.2
In any case, I personally don't care so much about hasattr(), but I'm +1 for Python compatibility, and +1 for getting the fixed Py3.2 behaviour in all Python versions. There's a reason this was fixed in CPython, it's a pretty clear bug.
OK, I'll go for it. Thanks.
https://github.com/cython/cython/commit/90120314bcc8bd5a4f71f2629e1065f5c943... -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo 3000 Santa Fe, Argentina Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169
Lisandro Dalcin, 23.02.2011 01:59:
On 22 February 2011 19:09, Lisandro Dalcin wrote:
On 22 February 2011 18:21, Stefan Behnel wrote:
Lisandro Dalcin, 22.02.2011 21:41:
I'm inclined to fix the behavior for ALL Python versions to suppress only AttributeError.
How? Would you implement a hasattr() helper that uses PyObject_GetAttr() and Does The Right Thing?
Yes, more or less the implementation of builtin_hasattr from bltinmodule.c from Py3.2
In any case, I personally don't care so much about hasattr(), but I'm +1 for Python compatibility, and +1 for getting the fixed Py3.2 behaviour in all Python versions. There's a reason this was fixed in CPython, it's a pretty clear bug.
OK, I'll go for it. Thanks.
https://github.com/cython/cython/commit/90120314bcc8bd5a4f71f2629e1065f5c943...
Looks good. Thanks. Stefan
participants (2)
-
Lisandro Dalcin -
Stefan Behnel