[New-bugs-announce] [issue14630] non-deterministic behavior of int subclass
Brecht Machiels
report at bugs.python.org
Fri Apr 20 09:51:22 CEST 2012
New submission from Brecht Machiels <brecht at mos6581.org>:
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
----------
components: None
files: pdf.py
messages: 158803
nosy: brechtm
priority: normal
severity: normal
status: open
title: non-deterministic behavior of int subclass
type: behavior
versions: Python 3.2
Added file: http://bugs.python.org/file25286/pdf.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14630>
_______________________________________
More information about the New-bugs-announce
mailing list