Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C api and checking for integers

40 views
Skip to first unread message

lallous

unread,
Nov 12, 2009, 4:28:32 AM11/12/09
to
Hello,

I am a little confused on how to check if a python variable is an integer or
not.

Sometimes PyInt_Check() fails and PyLong_Check() succeeds.

How to properly check for integer values?

OTOH, I tried PyNumber_Check() and:

(1) The doc says: Returns 1 if the object o provides numeric protocols, and
false otherwise. This function always succeeds.

What do they mean: "always succeeds" ?

(2) It seems PyNumber_check(py_val) returns true when passed an instance!

Please advise.

--
Elias

Daniel Molina Wegener

unread,
Nov 12, 2009, 5:23:07 AM11/12/09
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

on Jue 12 Nov 2009 06:28,
lallous wrote:

> Hello,
>
> I am a little confused on how to check if a python variable is an integer
> or not.
>
> Sometimes PyInt_Check() fails and PyLong_Check() succeeds.

Have you tried *PyInt_CheckExact()* and *PyLong_CheckExact()*?
Those functions can check if the instance is a subtype instead
of a simple instantce of Int and Long.

>
> How to properly check for integer values?

It depends on /how/ do you want to check those values...

>
> OTOH, I tried PyNumber_Check() and:
>
> (1) The doc says: Returns 1 if the object o provides numeric protocols,
> and false otherwise. This function always succeeds.
>
> What do they mean: "always succeeds" ?

This means that no error or exception is generated, even
if the object passed to the function is a number or not (int,
long, and others).

>
> (2) It seems PyNumber_check(py_val) returns true when passed an instance!

It returns 1 only if the instance provides *Numeric Protocols*:
'Returns 1 if the object o provides numeric protocols'.

>
> Please advise.
>
> --
> Elias

Best regards,
- --
| Daniel Molina Wegener == dmw [at] coder [dot] cl |
| IT Consulting & Freelance Software Developer |
| http://coder.cl/ |
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQIcBAEBCgAGBQJK++ILAAoJEHxqfq6Y4O5Nf6kP/3gXmV/kgKRw5ywTCnG4nMyy
G7FuAT/JWXGlpBtE9y7QJuLy4O2yBRSmOf3V8MX984haw0rZzLRQs6OGfqF/3UxX
tjLswlWt9zxnBOpQ4N5jYYfa7MxFBFeGyAugtLBzlNXci0DhqTQF7ETcRY8mgUuW
LTK6T5FwppSeRnLbOGmta4jFHPZ8ob8/RlV08OyWFKtF1/4BMzve7W8f+RS7DoSE
bg7CQQpUDPSYiuSnGn1cCwx13cXEmBmYwbsUlhLC/ioCLBFl+LcDVLGZZrKeAunY
xUv1DQzBGS8VV1Ba6LlGCGWzK/7EqsPkP9VkqR5JoQoIGqdqH0OhIWoGkIw1qulh
2QpG2w73F5a0v0re9yPUdecqeCTx6QuH1WecjQKFA8zf4CoyUDOY6Ho4NCsdVTDs
9Aa5GhTcCAiTm+5ZTlPjSWCgwu3YV7JNqocZU0835GApPcVQN8/1PU2MFRkmKdjn
oZtHSt6MzI62p1GFWCcKJF1llTEe6tkn9YFhRVW8/p6R/MFKqOcKcWZLThwzIxBb
KOdKteNEJpj9cC5bQ2QHhmLQ632kyELRmHJZ89YHQERfM6i2ji+13PfUcWTpLwbi
T2FYRv1xOs97QrIOnHxWsSYFY9lc/j8sfUYFnLl7yzLf8bSRu/JFAdAnhKif3CS+
45va5u6atxT+xvveieqw
=AGIS
-----END PGP SIGNATURE-----

casevh

unread,
Nov 12, 2009, 9:38:40 AM11/12/09
to
On Nov 12, 1:28 am, "lallous" <lall...@lgwm.org> wrote:
> Hello,
>
> I am a little confused on how to check if a python variable is an integer or
> not.
>
> Sometimes PyInt_Check() fails and PyLong_Check() succeeds.

I assume you are using Python 2.x. There are two integer types: (1)
PyInt which stores small values that can be stored in a single C long
and (2) PyLong which stores values that may or may not fit in a single
C long. The number 2 could arrive as either a PyInt or a PyLong.

Try something like the following:

if PyInt_CheckExact()
myvar = PyInt_AS_LONG()
else if PyLong_CheckExact()
myvar = PyLong_As_Long()
if ((myvar == -1) && (PyErr_Occurred())
# Too big to fit in a C long

Python 3.x is a little easier since everything is a PyLong.

>
> How to properly check for integer values?
>
> OTOH, I tried PyNumber_Check() and:
>
> (1) The doc says: Returns 1 if the object o provides numeric protocols, and
> false otherwise. This function always succeeds.
>
> What do they mean: "always succeeds" ?

That it will always return true or false; it won't raise an error.

>
> (2) It seems PyNumber_check(py_val) returns true when passed an instance!
>
> Please advise.
>
> --
> Elias

casevh

0 new messages