Message158803
| Author |
brechtm |
| Recipients |
brechtm |
| Date |
2012年04月20日.07:51:21 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1334908282.98.0.497797235147.issue14630@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
I have subclassed int to add an extra attribute:
class Integer(int):
def __new__(cls, value, base=10, indirect=False):
try:
obj = int.__new__(cls, value, base)
except TypeError:
obj = int.__new__(cls, value)
return obj
def __init__(self, value, base=10, indirect=False):
self.indirect = indirect
Using this class in my application, int(Integer(b'0')) sometimes returns a value of 48 (= ord('0')!) or 192, instead of the correct value 0. str(Integer(b'0')) always returns '0'. This seems to only occur for the value 0. First decoding b'0' to a string, or passing int(b'0') to Integer makes no difference. The problem lies with converting an Integer(0) to an int with int().
Furthermore, this occurs in a random way. Subsequent runs will produce 48 or 192 at different points in the application (a parser). Both Python 3.2.2 and 3.2.3 behave the same (32-bit, Windows XP). Apparently, the 64-bit Windows Python 3.2.3 does not show this behavior [2]. I haven't tested on other operating systems.
I cannot seem to reproduce this in a simple test program. The following produces no output:
for i in range(100000):
integer = int(Integer(b'0'))
if integer > 0:
print(integer)
Checking for the condition int(Integer()) > 0 in my application (when I know the argument to Integer is b'0') and conditionally printing int(Integer(b'0')) a number of times, the results 48 and 192 do show up now and then.
As I can't reproduce the problem in a short test program, I have attached the relevant code. It is basically a PDF parser. The output for this [2] PDF file is, for example:
b'0' 0 Integer(0) 192 0 b'0' 16853712
b'0' 0 Integer(0) 48 0 b'0' 16938088
b'0' 0 Integer(0) 192 0 b'0' 17421696
b'0' 0 Integer(0) 48 0 b'0' 23144888
b'0' 0 Integer(0) 48 0 b'0' 23185408
b'0' 0 Integer(0) 48 0 b'0' 23323272
Search for print function calls in the code to see what this represents.
[1] http://stackoverflow.com/questions/10230604/non-deterministic-behavior-of-int-subclass#comment13156508_10230604
[2] http://www.gust.org.pl/projects/e-foundry/math-support/vieth2008.pdf |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2012年04月20日 07:51:23 | brechtm | set | recipients:
+ brechtm |
| 2012年04月20日 07:51:22 | brechtm | set | messageid: <1334908282.98.0.497797235147.issue14630@psf.upfronthosting.co.za> |
| 2012年04月20日 07:51:22 | brechtm | link | issue14630 messages |
| 2012年04月20日 07:51:21 | brechtm | create |
|