Python CTypes translation of (pv != NULL)

p.lavarre at ieee.org p.lavarre at ieee.org
Wed Sep 27 12:35:51 EDT 2006


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

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

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!

///




More information about the Python-list mailing list