Python CTypes translation of (pv != NULL)

Gabriel Genellina gagsl-py at yahoo.com.ar
Tue Sep 26 20:14:07 EDT 2006


At Tuesday 26/9/2006 16:00, p.lavarre at ieee.org wrote:

> > >Q: The C idea of (pv != NULL) is said most directly in Python ctypes
> > >how?
> >
> > Perhaps reading the ctypes tutorial? (both in the 2.5 docs and in
> > <http://starship.python.net/crew/theller/ctypes/tutorial.html>)
>
>Actually, no.  I see three answers or zero, depending on how you like
>to count.
>
>Yes that flat edition helps makes this clear, thank you - there I can
>more easily grab an ordered list of all matches of text than in the
>more prominent shadow:
>http://docs.python.org/dev/lib/module-ctypes.html
>
>I see direct discussion only of how to construct null pointers, no
>discussion of how to test for them.  Specifically:
>
>The hint { bool(null_ptr) == False } could be read to mean try ==
>False, in the "Pointers" text.

It says:

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

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), so you can test a NULL pointer with a 
simple "if ptr: whatever"

--- 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 ---

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?)



Gabriel Genellina
Softlab SRL 


	
	
		
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas




More information about the Python-list mailing list