2.3 - > 2.4:long int太大而无法转换为int [英] 2.3 -> 2.4: long int too large to convert to int

查看:91
本文介绍了2.3 - > 2.4:long int太大而无法转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放弃了,如何在2.4下使其失败?

fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck(" HBB,0x1c,0x00,0x00))

我得到一个OverflowError:long int太大而无法转换为int

ioctl()期望一个32位整数值,并且0xc0047a80具有

高位设置。我假设Python认为它是一个

签名值。我如何告诉Python 0xc0047a80是一个

无符号32位值?


-

Grant Edwards grante哇!我要求IMPUNITY!



visi.com

解决方案



Grant Edwards < GR **** @ visi.com>在留言中写道

新闻:11 ************* @ corp.supernews.com ...

我放弃了,怎么办我认为这不会失败2.4?

fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck(" HBB",0x1c,0x00,0x00))
我得到一个OverflowError:long int太大而无法转换为int

ioctl()期望一个32位整数值,而0xc0047a80具有
高位组。我假设Python认为它是一个
签名值。我怎么告诉Python 0xc0047a80是一个无符号的32位值?




在2.3和之前,你得到这个:

0xc0047a80



-1073448320


在2.4中,正十六进制文字被视为正数,这就是你的问题:你的文字大于最大的int,因此得到

存储为长整数。我会尝试-1073448320作为arg。


Terry J. Reedy


Grant Edwards写道:

我放弃了,如何在2.4下使这不失败?

fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck(" HBB" ;,0x1c,0x00,0x00))

我得到一个OverflowError:long int太大而无法转换为int

ioctl()期望一个32位整数值,并且0xc0047a80具有高位设置。我假设Python认为它是一个
签名值。我怎么告诉Python 0xc0047a80是一个无符号的32位值?




你可以这样伪装它,
< br $>
def unsigned(val):

返回struct.unpack(''我',struct.pack(''我',val))[0]


fcntl.ioctl(self.dev.fileno(),unsigned(0xc0047a80),...)


但写好文档字符串解释好运为什么一个函数叫做

" unsigned"取正数并返回负数...;)

克里斯珀金斯


2005-09-15, Terry Reedy< tj ***** @ udel.edu>写道:

我放弃了,我如何使这不能在2.4下失败?

fcntl.ioctl(自我。 dev.fileno(),0xc0047a80,struct.pa ck(" HBB",0x1c,0x00,0x00))

我得到一个OverflowError:long int太大而无法转换为int

ioctl()期望一个32位整数值,0xc0047a80具有高位设置。我假设Python认为它是一个
签名值。我怎么告诉Python 0xc0047a80是一个无符号的32位值?
在2.3及之前,你得到这个:

0xc0047a80


-1073448320




我并不特别关心Python如何打印价值 - 我只是

想要传递给我正在调用的函数的值。

在2.4中,正十六进制文字被视为正数,这就是你的问题:你的文字更大比最大的int因此得到
存储为long int。


我知道,我只是想出一个很好的方法来解决它。

我会尝试-1073448320作为arg。




这应该有效,但它有点蹩脚(没有冒犯)。


ioctl值总是,总是用十六进制写的。一块

ioctl值通常分配给特定的驱动程序,例如高阶N(是4个5?)十六进制数字对于

那个司机。用十进制写的值是

完全混淆任何看代码的人。


我更喜欢写一个函数的另一个建议

接受0x< whatever>并返回相应的整数值。


另一张海报建议使用struct的解决方案。这是我的

解决方案(假设python整数用2'的

恭维二进制表示):


def ioctlValue(i):

if i& 0x80000000:

i = - ((i ^ 0xffffffff)+1)

返回i


-

格兰特爱德华兹格兰特哇!在Tenafly的某个地方,

在新泽西州,一名脊椎按摩师

visi.com正在查看留下它来b / b
eaver!


I give up, how do I make this not fail under 2.4?

fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck("HBB",0x1c,0x00,0x00))

I get an OverflowError: long int too large to convert to int

ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has
the high-order bit set. I''m assuming Python thinks it''s a
signed value. How do I tell Python that 0xc0047a80 is an
unsigned 32-bit value?

--
Grant Edwards grante Yow! I demand IMPUNITY!
at
visi.com

解决方案


"Grant Edwards" <gr****@visi.com> wrote in message
news:11*************@corp.supernews.com...

I give up, how do I make this not fail under 2.4?
fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck("HBB",0x1c,0x00,0x00))

I get an OverflowError: long int too large to convert to int

ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has
the high-order bit set. I''m assuming Python thinks it''s a
signed value. How do I tell Python that 0xc0047a80 is an
unsigned 32-bit value?



In 2.3 and before, you get this:

0xc0047a80


-1073448320

In 2.4, positive hex literals are treated as positive numbers, and that is
your problem: your literal is greater than the largest int and hence gets
stored as long int. I would try -1073448320 as the arg.

Terry J. Reedy


Grant Edwards wrote:

I give up, how do I make this not fail under 2.4?

fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck("HBB",0x1c,0x00,0x00))

I get an OverflowError: long int too large to convert to int

ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has
the high-order bit set. I''m assuming Python thinks it''s a
signed value. How do I tell Python that 0xc0047a80 is an
unsigned 32-bit value?



You could sort-of fake it like this,

def unsigned(val):
return struct.unpack(''i'', struct.pack(''I'', val))[0]

fcntl.ioctl(self.dev.fileno(), unsigned(0xc0047a80), ...)

but good luck writing a docstring explaining why a function called
"unsigned" takes a positive long and returns a negative int... ;)
Chris Perkins


On 2005-09-15, Terry Reedy <tj*****@udel.edu> wrote:

I give up, how do I make this not fail under 2.4?

fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck("HBB",0x1c,0x00,0x00))

I get an OverflowError: long int too large to convert to int

ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has
the high-order bit set. I''m assuming Python thinks it''s a
signed value. How do I tell Python that 0xc0047a80 is an
unsigned 32-bit value?
In 2.3 and before, you get this:

0xc0047a80


-1073448320



I don''t particular care how Python prints the value -- I just
want that value passed to the function I''m calling.
In 2.4, positive hex literals are treated as positive numbers, and that is
your problem: your literal is greater than the largest int and hence gets
stored as long int.
I knew that, I just couldn''t come up with a good way to fix it.
I would try -1073448320 as the arg.



That should work, but it''s kind of lame (no offense).

ioctl values are always, always written in hex. A block of
ioctl values is generally assigned to a particular driver such
that the high order N (is it 4 oe 5?) hex digits are unique to
that driver. Writing the value in decimal is going to
completely confuse anybody looking at the code.

I rather like the other suggestion of writing a function that
accepts 0x<whatever> and returns the appropriate integer value.

Another poster suggested a solution using struct. Here''s my
solution (which assume python integers are represented in 2''s
compliment binary):

def ioctlValue(i):
if i & 0x80000000:
i = -((i^0xffffffff)+1)
return i

--
Grant Edwards grante Yow! Somewhere in Tenafly,
at New Jersey, a chiropractor
visi.com is viewing "Leave it to
Beaver"!


这篇关于2.3 - &gt; 2.4:long int太大而无法转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆