Possible BUG in Python2.x sre and threaded import locking

Joe Kelly joek at quantiva.com
Wed Aug 22 11:37:58 EDT 2001


There appears to be a bug in the way Python2.x does import locking on
sre.  This is illustrated by the following code fragments.

$ cat retest
#!/usr/local/bin/python
__import__("ret")

$ cat ret.py
#!/usr/local/bin/python

import time, thread, string
#import time, thread
import re
#import pre
#re=pre

def mythread():
        time.sleep(2)
        url = "hello there"
        url = re.sub(" ", '', url)
        print "hello from threadland", url

thread.start_new_thread(mythread, ())

url = "hello main there"
url = re.sub(" ", '', url)
print "main", url
time.sleep(5)
print "goodbye", url


If you run retest as shown above you get:
$ retest
main hellomainthere
goodbye hellomainthere

It never runs the code in the thread.

If you run python on ret.pyc directly you get the correct/expected
result:
$ python ret.pyc
main hellomainthere
hello from threadland hellothere
goodbye hellomainthere

If you replace sre with pre by commenting out line 5 and uncommenting
out line 6 and 7 it works correctly/as expected:
$ retest
main hellomainthere
hello from threadland hellothere
goodbye hellomainthere

I've also noticed some strange interaction with other imports, i.e. if
you go back to the original ret.py and comment out line 3 and
uncomment line 4 (just don't import the string module) you get the
following:
$ retest
main hellomainthere
goodbye hellomainthere
hello from threadland hellothere

That is the thread gets executed at the end or sometimes not at all:
$ retest
main hellomainthere
goodbye hellomainthere

Again, calling ret directly seems to work.
$ python ret.pyc
main hellomainthere
hello from threadland hellothere
goodbye hellomainthere

This has been tested on Solaris 8 running on Intel:
$ uname -a
SunOS machine 5.8 Generic_108529-07 i86pc i386 i86pc

The above output was generated by python 2.0 compiled with threading
on:
$ python
Python 2.0 (#1, May 13 2001, 00:55:08)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>>

It exhibits the same behavior under python 2.1.1:
$ python
Python 2.1.1 (#22, Aug 20 2001, 17:12:35)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.

Has anyone witness similar behavior? Any help would be greatly
appreciated.

Thanks,
Joe Kelly



More information about the Python-list mailing list