At Wednesday 27/9/2006 13:35, [EMAIL PROTECTED] wrote:
> > http://starship.python.net/crew/theller/ctypes/tutorial.html
> ...
> direct discussion only of how to construct null pointers,
> no discussion of how to test for them ...
> ...
> could be read to mean try ... == ... is ... False ... None ...
CTypes nulls fetched from a struct feel more like None than do CTypes
nulls fetched from elsewhere, astonishing me the Python newbie, e.g.:
No, the difference comes from the kind of pointer type, char* gets
converted to string or None. See the table on section 14.14.1.4
Fundamental data types
$ python nulls.py
True True True None None
True True True None None
True False False None c_char_p(None)
$
$ cat nulls.py
from ctypes import *
class struct_aa(Structure):
_fields_ = [("chars", c_char_p)]
pv = None
print not pv, pv == None, pv is None, cast(pv, c_void_p).value, pv
pv = struct_aa().chars
print not pv, pv == None, pv is None, cast(pv, c_void_p).value, pv
pv = cast(c_void_p(0), c_char_p)
print not pv, pv == None, pv is None, cast(pv, c_void_p).value, pv
$
c_char_p is *not* a pointer in Python, since Python does not have pointers.
Either the original C pointer was NULL: then the Python value is None
Or the original C pointer was not NULL: then the Python value is a string
Possibly relevant, though grabbed at random, is:
/// PEP 8 -- Style Guide for Python Code
/// http://www.python.org/dev/peps/pep-0008/
... beware of writing "if x" when you really mean "if x is not None" --
e.g. when testing whether a variable or argument that defaults to None
was set to some other value. The other value might have a type (such
as a container) that could be false in a boolean context!
This is exactly the case: None is *not* a pointer (neither any other
kind of ctypes object), it's an object -a singleton, i.e., there
exist exactly a single one instance of None).
If you want to check if some kind of pointer is NULL or not, do *not*
check if it is None or not, check its Boolean value, again:
if ptr: xxx
if not ptr: xxx
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
--
http://mail.python.org/mailman/listinfo/python-list