473,574 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(429496 7295L) <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.LoadLibrar y('./passlib.so')
passback = _fx = passlib.passbac k
_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.pa ssback(-1))
$

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

Sep 28 '06 #1
3 4221
p.*******@ieee. org schrieb:
>Subject: Python CTypes translation of (pv != NULL)
I've started a wiki page where I already added an entry about the above question.
Do you think the entry would have answered your question?

http://starship.python.net/crew/thel...i/CodeSnippets

(My wiki should really be upgraded to a newer moinmoin version, however
it might be better to use another wiki for ctypes, maybe the python.org one, for this
stuff.)
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.
True, in principle. For me, on Windows, c_void_p(-1).value is the same
as c_void_p(0xFFFF FFFF).value, but it is -1 not 0xFFFFFFFF.

Worse, on 64-bit systems c_void_p(-1).value is 184467440737009 661615, which
is the same as 0xFFFFFFFFFFFFF FFF.

So, to be portable, INVALID_HANDLE_ VALUE should be defined like this:

INVALID_HANDLE_ VALUE = c_void_p(-1).value

and your test would become

(ctypes.cast(pv , ctypes.c_void_p ).value == INVALID_HANDLE_ VALUE

Thomas

PS: It is inconsistent that the .value of a c_void_p instance is signed on
32-bit systems, and unsigned on 64-bit systems, but it seems we have to live
with that. Fortunately, the c_void_p constructor accepts both signed and unsigned
Python ints and longs, so it should be possible to work around that fact.

Sep 28 '06 #2
I've started a wiki page where I already added an entry about the above question.
Do you think the entry would have answered your question?
http://starship.python.net/crew/thel...i/CodeSnippets
As concise as I now can imagine, yes thank you.

I see the main point leads: "The truth value of any NULL ctypes pointer
instance is False." That angle neatly ducks my ongoing newbie
confusion over a null constructed in Python via c_void_p(0) being
different from a null constructed in C via return (void *) 0.
...
And so then the next related Faq is:

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

For me, on Windows, c_void_p(-1).value is the same
as c_void_p(0xFFFF FFFF).value, but it is -1 not 0xFFFFFFFF.
Different results at my desk. Specifically, I see:
>>from ctypes import * ; c_void_p(-1).value
4294967295L
>>>
in Mac OS X 10.4.7 Python 2.5 r25:51918 and Win XpSp2 Python 2.5
r25:51908.
Worse, on 64-bit systems c_void_p(-1).value is 184467440737009 661615, which
is the same as 0xFFFFFFFFFFFFF FFF.
So, to be portable, INVALID_HANDLE_ VALUE should be defined like this:
INVALID_HANDLE_ VALUE = c_void_p(-1).value
Exactly what I should have written, yes, thank you.

And somewhat generalisable: c_void_p(0).val ue is None.

Maybe this is the thing that's been astonishing me: maybe C often does
return the .value of the pointer, rather than a new instance of the
POINTER class?
and your test would become
(ctypes.cast(pv , ctypes.c_void_p ).value == INVALID_HANDLE_ VALUE
Yes.

Mind you, typing out all that noise inspires me to ask, does (pv ==
INVALID_HANDLE_ VALUE) give the wrong answer for any pv other than
c_void_p(-1)? In other words, if by convention I get all my pointers
from C and never construct a pointer in Python except to take its
value, then can I safely write the more obvious translation of (pv ==
INVALID_HANDLE_ VALUE)?
PS: It is inconsistent that the .value of a c_void_p instance is signed on
32-bit systems, and unsigned on 64-bit systems, but it seems we have to live
with that.
I'm interested, if you can make the time to say more ...
Fortunately, the c_void_p constructor accepts both signed and unsigned
Python ints and longs, so it should be possible to work around that fact.
Good.
...
might be better to use another wiki for ctypes, maybe the python.org one, for this
stuff.) ...
Could be. Offline I heard we might have more pages coming soon.
Checking at random just now, I see http://wiki.python.org/moin/ is
"immutable" , so I guess I can't add "A Wiki page to collect the Faq's
highlit by Clp" to the "Starting points" "Asking for Help" >
"Wanted..." >

Sep 28 '06 #3
p.*******@ieee. org schrieb:
>I've started a wiki page where I already added an entry about the above question.
Do you think the entry would have answered your question?
http://starship.python.net/crew/thel...i/CodeSnippets

As concise as I now can imagine, yes thank you.

I see the main point leads: "The truth value of any NULL ctypes pointer
instance is False." That angle neatly ducks my ongoing newbie
confusion over a null constructed in Python via c_void_p(0) being
different from a null constructed in C via return (void *) 0.
>...
And so then the next related Faq is:

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

For me, on Windows, c_void_p(-1).value is the same
as c_void_p(0xFFFF FFFF).value, but it is -1 not 0xFFFFFFFF.

Different results at my desk. Specifically, I see:
>>>from ctypes import * ; c_void_p(-1).value
4294967295L
>>>>

in Mac OS X 10.4.7 Python 2.5 r25:51918 and Win XpSp2 Python 2.5
r25:51908.
You are right, I was using my own private build of ctypes which behaves
differently. With Python 2.5 on Windows, I get the same as you.
>Worse, on 64-bit systems c_void_p(-1).value is 184467440737009 661615, which
is the same as 0xFFFFFFFFFFFFF FFF.
So, to be portable, INVALID_HANDLE_ VALUE should be defined like this:
INVALID_HANDLE _VALUE = c_void_p(-1).value

Exactly what I should have written, yes, thank you.

And somewhat generalisable: c_void_p(0).val ue is None.

Maybe this is the thing that's been astonishing me: maybe C often does
return the .value of the pointer, rather than a new instance of the
POINTER class?
>and your test would become
(ctypes.cast(p v, ctypes.c_void_p ).value == INVALID_HANDLE_ VALUE

Yes.

Mind you, typing out all that noise inspires me to ask, does (pv ==
INVALID_HANDLE_ VALUE) give the wrong answer for any pv other than
c_void_p(-1)? In other words, if by convention I get all my pointers
from C and never construct a pointer in Python except to take its
value, then can I safely write the more obvious translation of (pv ==
INVALID_HANDLE_ VALUE)?
Well, c_void_p(-1).value is the same as c_void_p(0xFFFF F...) for a sufficient
number of F's, so to speak. The integer or long value you pass to c_void_p
is masked with a bitmask haing all bits set to '1' as the platform's (void *)
type has.
>PS: It is inconsistent that the .value of a c_void_p instance is signed on
32-bit systems, and unsigned on 64-bit systems, but it seems we have to live
with that.
Obviously, as you discovered, and I confirmed, this is no longer true. In the Python2.5
ctypes, the value of c_void_p is always unsigned. I just meant that we have to live
for quite some time with the behaviour of ctypes as implemented, because only bug
fixes can be made to Python 2.5.

Thomas (I'll probably leave for a few days now)

Sep 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
8815
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty programmer that you pretend to be, why don't you just write some? (And oh, void main is still not allow by the C++ standard.) Seeming as how you...
14
15637
by: deanbrown3d | last post by:
Hi there! Suppose I have a function void ShowMessage(string s); (that has a void return type.) In my other function F, also with a void return type, can I do this?
5
3506
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can...
2
5352
by: Judah | last post by:
With generics in .NET 2, there are certain scenarios in which using System.Void would be useful: TReturnType MyFunction<TReturnType>() { return default(TReturnType); } Above is a simple function that would allow me to make a generic method that would return any type I instanciate the method with. But what if I wanted to
7
3061
by: getridofthespam | last post by:
Hi all, Why doesn't _tzset return an error when the TZ environment variable does not contain a valid timezone? Other question: how to check the timezone is a valid one? Thanks for all answers.
2
2318
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId : the class Id of objects owning attributes to
28
3654
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
0
1187
by: p.lavarre | last post by:
Now at http://pyfaq.infogami.com/suggest we have: FAQ: How do I declare that CTypes function returns void? A: c_void = None is not doc'ed, but is suggested by: <class 'ctypes.c_void_p'> Remembering c_void = c_int from K&R C often works, but if you say a restype is c_int, then doctest's of that call will print an int, yuck. If you say...
1
4717
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out how to set values to a variable inside the library. This is Python 2.5 on Linux. Here's what I have so far--if you comment out the memmove call (3 lines) it works as expected: # START...
0
7735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8065
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8246
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7823
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8102
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2250
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.