[Matrix-SIG] Re: Bug in generating .pyc files!!!

Guido van Rossum guido@CNRI.Reston.VA.US
Mon, 01 Nov 1999 14:51:09 -0500


> On Mon, 1 Nov 1999, Pearu Peterson wrote: [in the MATRIX-SIG, although i
> don't know why!]
> 
> > I was able to track down where is the bug and I will demonstrate it below.
> > Let main.py be:
> 
> #!/usr/bin/env python
> import sub
> sub.sub()
> # EOF main.py
> 
> (here's DA's minimal version of Pearu's code):
> 
> def sub():
>     l = ' '
>     print l[0] is l
> 
> Here are the results of repeated calls of main.py:
> 
> > python main.py
> 1
> > python main.py
> 0
> > python main.py
> 0
> 
> > So, it seems that 'is' is interpreted differently when .pyc file is used.
> > I think this is a serious problem and should be fixed.
> 
> I agree.  FWIW, IMO the right thing to do is to have the test return false
> all the time, but I agree that there should not be a dependence on
> byte-compilation.
> 
> Guido?

The result of the 'is' operator on immutable objects (like strings)
not well-defined.  The Python VM is allowed but not required to cache
objects with certain values and reuse them as the outcome of certain
expressions (such as l[0] above) is it sees fit to optimize speed.

The solution is not to use 'is' here but always to use '==' when
comparing strings.

This is definitely not a bug and definitely does not need to be fixed.

--Guido van Rossum (home page: http://www.python.org/~guido/)