[Python-bugs-list] [ python-Bugs-628842 ] Deprecate PyNumber_Check

SourceForge.net noreply@sourceforge.net
Tue, 18 Feb 2003 09:50:17 -0800


Bugs item #628842, was opened at 2002-10-25 21:35
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=628842&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 4
Submitted By: Neil Schemenauer (nascheme)
Assigned to: Neil Schemenauer (nascheme)
Summary: Deprecate PyNumber_Check

Initial Comment:
I think PyNumber_Check should be deprecated.  An type
having tp_as_number does not mean it is a number.  If we
don't depreciate it, perhaps we should modify the test.
 For
example, we could check for a __int__ method.

Guido, if you decide what to do, assign the bug back to
me and I will work on a patch.

----------------------------------------------------------------------

>Comment By: Neil Schemenauer (nascheme)
Date: 2003-02-18 17:50

Message:
Logged In: YES 
user_id=35752

Progress update:

Guido has changed PyNumber_Check() to check for
nb_int and nb_float.  I improved the PyFloat_AsDouble()
exception message.  Still need to check the usage of
PySequence_Check() and PyMapping_Check().

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-10-29 23:02

Message:
Logged In: YES 
user_id=6380

Adding a test for nb_int *or* nb_float would seem an
adequate redefinition of PyNumber_Check(). But it's still
not perfect -- a class instance *always* passes this test,
for example, and you can get select() to report strange
errors by passing a class instance. (And it also doesn't
help for complex.) The problem is, what to use in the select
module instead of PyNumber_Check()? There really isn't a
decent alternative; we don't want to call PyInt_Check(),
PyLong_Check() and PyFloat_Check() directly, and
PyFloat_AsDouble() reports an unhelpful "bad argument type
for built-in operation" when given a non-number. Hm, maybe
PyFloat_AsDouble() should be changed to report a better error???

I've also noticed that at least some of the calls to
PySequence_Check() seem bogus -- e.g. the ones in
PySequence_List() and in list_fill() (which look like the
same code BTW, as do several others). In fact, I think most
(all?) uses of this should not be there, since they are only
designed to give "nice" error messages, but they are neither
necessary nor sufficient to catch all cases. (They aren't
necessary because all the other PySequence_XXX() APIs do
proper checking anyway.)

I also noticed that PyMapping_Check() again gives a false
sense of security in e.g. posix_execve().

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-10-28 13:10

Message:
Logged In: YES 
user_id=80475

A grep of Modules, Objects, and Python shows that 
PyNumber_Check is only used in selectmodule.c

The sequence and mapping checks are used much more 
frequently but not naively.  They are used as guards before 
the slot is referenced.



----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-10-28 02:39

Message:
Logged In: YES 
user_id=6380

I agree that checking for tp_as_number is bogus. (Ditto for
sequence and mapping -- are these similarly naive?)

Is there any code that currently uses PyNumber_Check? 
Depending on what it is used for, I'd make a different decision.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=628842&group_id=5470