473,795 Members | 2,391 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 4234
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
9028
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 tried to quote the standard in an attempt to pretend you're right, I'll quote the standard to once...
14
15666
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
3526
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 store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
2
5361
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
3082
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
2332
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
3698
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
0
1196
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 the restype is None, then those doctest's work: they print
1
4728
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 #!/usr/local/bin/python2.5
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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

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.