ctypes.c_void_p(-1) might not be C return (void *) -1



Subject: Python CTypes translation of (pv != NULL)

And so then the next related Faq is:

Q: How should I test for ((void *) -1)?

A:

(pv == 0xffffFFFF) works often.

(ctypes.cast(pv, ctypes.c_void_p).value == 0xffffFFFF) works better.

((void *) -1) appears often in 32-bit Windows, behind the name
INVALID_HANDLE_VALUE of the type HANDLE that is def'ed as the type
PVOID that is def'ed as (void *). -1 is xffffFFFF in 32-bit two's
complement.

The more complex comparison works more often because specifying a
restype doesn't decide the __class__ of the result. For example, the
run below shows a <type 'long'> result value passed back from a C
routine that has a c_void_p restype:

$ gcc -o passlib.so -dynamiclib -Wall passlib.c
$ python results.py
True False c_void_p(4294967295L) <class 'ctypes.c_void_p'>
True True 4294967295 <type 'long'>
$
$ cat passlib.c
void * passback(int ii)
{
return (void *) ii;
}
$
$ cat results.py
from ctypes import *
passlib = cdll.LoadLibrary('./passlib.so')
passback = _fx = passlib.passback
_fx.restype = c_void_p
_fx.argtypes = [c_int]
def test(pv):
print cast(pv, c_void_p).value == 0xffffFFFF,
print pv == 0xffffFFFF,
print pv, pv.__class__
test(c_void_p(-1))
test(passlib.passback(-1))
$

All true? All bogus? Partly bogus? Curiously yours, Pat LaVarre

.



Relevant Pages

  • About the remoting event
    ... callback port in the client side, but told that these was an exception on the ... static void Main ... Cat cat = new Cat; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: */ in string confuses checkpatch.pl
    ... The below code confuses checkpatch.pl ver 0.21. ... void foo ... 1 errors, 0 warnings, 5 lines checked ... $ cat ../checkpatch/Z213.c ...
    (Linux-Kernel)
  • Re: About the remoting event
    ... define a callback interface that the client implements, ... static void Main ... Cat cat = new Cat; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About the remoting event
    ... define a callback interface that the client implements, ... static void Main ... BinaryServerFormatterSinkProvider ss = new ... Cat cat = new Cat; ...
    (microsoft.public.dotnet.languages.csharp)
  • more silly questions that are probably elementary to most but.....
    ... public static void main{ ... Wolf aWolf = new Wolf; ... Cat c = new Cat; ... public class Cat extends Feline { ...
    (comp.lang.java.help)