[PyKDE] SIP and unsigned int

Ulrich Berning ulrich.berning at denviso.de
Tue Jan 24 16:12:00 GMT 2006


Hi,

SIP (tested with snapshot-20060120) doesn't handle unsigned int
correctly. SIP treats unsigned int the same as signed int and I think,
this is wrong.
While 4294967295 (0xffffffff) is a legal unsigned int value (on machines
where the size of int is 32 bit of course), it is not a legal signed int
value. The maximum legal signed int value is 2147483647 (0x7fffffff).

How should I wrap a function/method, that takes/returns unsigned int's
in the full range?
I could replace every occurence of unsigned int with unsigned long, but
if you are wrapping a large number of functions/methods, this is
annoying. If you have functions/methods that take pointers to unsigned
int, you have lost. Changing unsigned int* to unsigned long* gives me
compiler errors.

See the attached code:

uinttest.h contains the definition of the class Test containing two
member functions.
uinttest.cpp contains the implementation
uinttest.sip is the SIP file (look at the comments there)
test.py is a test script

I think unsigned int should be handled like unsigned long, but I can't
say, if this would break existing code.

---
Ulli







-------------- next part --------------
A non-text attachment was scrubbed...
Name: uinttest.h
Type: text/x-chdr
Size: 140 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20060124/dbc26d57/uinttest.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: uinttest.cpp
Type: text/x-c++src
Size: 371 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20060124/dbc26d57/uinttest-0001.bin
-------------- next part --------------
%Module uinttest 0

class Test
{

%TypeHeaderCode
#include <uinttest.h>
%End

//--------------------------------------------------------------------//
// SIP handles unsigned int the same as signed int. This makes it     //
// impossible to use values greater than 0x7fffffff. If the arguments //
// are not pointers, changing unsigned int to unsigned long succeeds. //
// If the arguments are pointers, you have lost.                      //
//--------------------------------------------------------------------//
public:

    // This works for values up to 0x7fffffff.
    //unsigned int func1(unsigned int a, unsigned int b);

    // Changing unsigned int to unsigned long enables the
    // full range of unsigned int.
    unsigned long func1(unsigned long a, unsigned long b);

    // This works for values up to 0x7fffffff.
    unsigned int func2(unsigned int *a /In/, unsigned int *b /In/);
    
    // Changing unsigned int* to unsigned long* doesn't compile.
    //unsigned long func2(unsigned long *a /In/, unsigned long *b /In/);

};


-------------- next part --------------
#!/usr/bin/env python

import uinttest

# Create an instance
ox = uinttest.Test()

# This works in every case
x = ox.func1(2147483647, 2000000000)
print "x: %d,  type(x): %s" % (x, type(x))

# This works in every case
x = ox.func2(2147483647, 2000000000)
print "x: %d,  type(x): %s" % (x, type(x))

# This works only when we change unsigned int
# to unsigned long int in uinttest.sip
x = ox.func1(4294967295, 4000000000)
print "x: %d,  type(x): %s" % (x, type(x))

# This always raises TypeError
x = ox.func2(4294967295, 4000000000)
print "x: %d,  type(x): %s" % (x, type(x))





More information about the PyQt mailing list