Python CTypes translation of (pv != NULL)

p.lavarre at ieee.org p.lavarre at ieee.org
Wed Sep 27 12:40:38 EDT 2006


> It says:
>
> NULL pointers have a False boolean value:
>  >>> null_ptr = POINTER(c_int)()
>  >>> print bool(null_ptr)
> False

Yes.

> That means that NULL pointers are considered False in a boolean
> expression (and you could assume that non-NULL pointers are True, as
> any other object in general),

I see this now that you show the clueless newbie me, yes thank you.
Except now by showing me here we have provoked the authority Thomas
Heller to say:

> > > Generally pointer instances have a False boolean value, so
> > > 'if pv: ....'
> > > should work.  Except for c_void_p, c_char_p and c_wchar_p instances.

That English I do not understand.  "Except" how?

> so you can test a NULL pointer with a
> simple "if ptr: whatever"

Apparently, yes, except for the "except" above.

> --- cut ---
> from ctypes import *
> null_ptr = POINTER(c_int)()
> x=c_int(1234)
> other_ptr = pointer(x)
>
> if null_ptr: print "null_ptr is not NULL"
> else: print "null_ptr is NULL"
>
> if other_ptr: print "other_ptr is not NULL"
> else: print "other_ptr is NULL"
> --- cut ---

Yes.

> It's really the same as bool([])==False, bool(any_non_empty_list)==True:
>
> L = [1,2,3]
> while L:
>    print L.pop()
>
> You don't say:
> while bool(L)==True:
>
> (do you?)

Oh that's exactly how my newbie innocence led me astray.

Rather than working first to translate the C 'if pv', I worked first to
translate the C '(pv != NULL)',  and then I got lost (and as yet still
am lost) in newbie astonishment over some nulls feeling more like None
than others.




More information about the Python-list mailing list