COM call : OverflowError: Python int too large to convert to C long

1

I'm trying to call a COM Method using win32com Python (v3.3) library. This method accepts an address and a data to write to that address.

SWDIOW(IN addr, IN data)

Problem is: accepted size for data is 32bits. When I call this method with such parameters there is no problem:

swdiow(0x400c0000, 0x00000012)

But when data is too big (actually greater than 2^(-31)-1), such as;

swdiow(0x400c0000, 0xF3804770)

This happens:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\WinPython\python-3.3.2.amd64\lib\site-packages\win32com\gen_py\97FF1B53-6477-4BD1-8A29-ADDB58E862E5x0x16x0.py", line 1123, in swdiow
    , data)
OverflowError: Python int too large to convert to C long

I think this problem happens because python takes 0xF3804770 as a signed integer. This value requires 32 bits to be represented as unsigned. To make it signed, Python adds one more sign bit, and size of 0xF3804770 becomes 33 bits. Because of that sign bit, it cannot convert to C long type which I believe the size is 32 bits.

I've found a workaround to this that will convert number to a negative integer when it's too big.

>> int.from_bytes((0xF3804770).to_bytes(4,'big',signed=False),'big', signed=True)
-209696912

Maybe this is the best thing I could do, but I wonder if there is a more elegant solution to this?

By the way, source of my 'data' is a text file that contains 4 bytes HEX values such as this;

D0010880
D1FD1E40
F3EF4770
431103C2
...
python
com
win32com
asked on Stack Overflow Dec 12, 2013 by HeyYO

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0