[Python-Dev] test_threaded_import

Tim Peters tim.one@home.com
Wed, 29 Aug 2001 15:03:12 -0400


[Jack Jansen]
> I spent the last couple of days hunting for the bug in MacPython
> threaded import, but it turned out the bug is in the test harness.
> (I did find another serious bug in the process, so the time wasn't
> wasted, but still)

Hmm!  You originally said this test hung "about 30% of the time", and didn't
respond when I asked how you ran it.  Given the way you are running it, it
should have hung 100% of the time.  So something here is still muddy.

> The problem is that test_threaded_import cannot run inside an
> "import". It knows this, and so does regrtest, so they run the test
> after the import by calling test_threaded_import.test_main(), which
> does the actual test.
>
> All good and fine if you run regrtest as a main script from the
> commandline,

Or if you run test_threaded_import directly (leaving regrtest out of it).

> but of course it breaks if you run autotest,

So it does!  I've never done that.

> or if you "import regrtest" in an interactive window,

Are you sure about that?  Works fine for me, and should work fine.

> because in these cases the import in the main thread
> already holds the lock.

It shouldn't hurt anything to import regrtest -- it's not until
regrtest.main() gets invoked that a problem can occur.

> I can simply add an "if not verbose", as in test_socketserver,
> but that isn't really the problem. Does anyone know a better
> solution?

I don't like that either -- test_socketserver almost never gets run on my
box simply because it's an oddball.  For the same reason I'm sure the
test_exceptions part of test_math never gets run on anyone's box.

Assuming you were suffering an illusion when saying "import regrest in an
interactive window" doesn't work, I'd be content to special-case the snot
out of autotest, like so:

Index: test_threaded_import.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_threaded_import.py,v
retrieving revision 1.2
diff -c -r1.2 test_threaded_import.py
*** test_threaded_import.py     2001/05/22 18:28:25     1.2
--- test_threaded_import.py     2001/08/29 18:53:38
***************
*** 6,12 ****
  # randrange, and then Python hangs.

  import thread
! from test_support import verbose

  critical_section = thread.allocate_lock()
  done = thread.allocate_lock()
--- 6,12 ----
  # randrange, and then Python hangs.

  import thread
! from test_support import verbose, TestSkipped

  critical_section = thread.allocate_lock()
  done = thread.allocate_lock()
***************
*** 32,37 ****
--- 32,41 ----

  def test_main():        # magic name!  see above
      global N, done
+     import sys
+     for modname in sys.modules:
+         if modname.find('autotest') >= 0:
+             raise TestSkipped("can't run from autotest")
      done.acquire()
      for N in (20, 50) * 3:
          if verbose: