[pypy-commit] pypy py3k: Python3 renamed thread to _thread

amauryfa noreply at buildbot.pypy.org
Sat Oct 22 23:03:16 CEST 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48346:26fa76635221
Date: 2011-10-22 01:33 +0200
http://bitbucket.org/pypy/pypy/changeset/26fa76635221/

Log:	Python3 renamed thread to _thread

diff --git a/pypy/module/thread/__init__.py b/pypy/module/thread/__init__.py
--- a/pypy/module/thread/__init__.py
+++ b/pypy/module/thread/__init__.py
@@ -3,6 +3,8 @@
 from pypy.interpreter.mixedmodule import MixedModule
 
 class Module(MixedModule):
+    applevel_name = '_thread'
+    
     appleveldefs = {
     }
 
diff --git a/pypy/module/thread/test/test_fork.py b/pypy/module/thread/test/test_fork.py
--- a/pypy/module/thread/test/test_fork.py
+++ b/pypy/module/thread/test/test_fork.py
@@ -7,7 +7,7 @@
         # XXX This test depends on a multicore machine, as busy_thread must
         # aquire the GIL the instant that the main thread releases it.
         # It will incorrectly pass if the GIL is not grabbed in time.
-        import thread
+        import _thread
         import os
         import time
 
@@ -22,7 +22,7 @@
             done.append(None)
 
         try:
-            thread.start_new(busy_thread, ())
+            _thread.start_new(busy_thread, ())
 
             pid = os.fork()
 
@@ -39,18 +39,18 @@
 
     def test_forked_can_thread(self):
         "Checks that a forked interpreter can start a thread"
-        import os, thread, time
+        import os, _thread, time
 
         if not hasattr(os, 'fork'):
             skip("No fork on this platform")
 
         # pre-allocate some locks
-        thread.start_new_thread(lambda: None, ())
+        _thread.start_new_thread(lambda: None, ())
 
         pid = os.fork()
         if pid == 0:
-            print 'in child'
-            thread.start_new_thread(lambda: None, ())
+            print('in child')
+            _thread.start_new_thread(lambda: None, ())
             os._exit(0)
         else:
             self.timeout_killer(pid, 5)
diff --git a/pypy/module/thread/test/test_import_lock.py b/pypy/module/thread/test/test_import_lock.py
--- a/pypy/module/thread/test/test_import_lock.py
+++ b/pypy/module/thread/test/test_import_lock.py
@@ -12,26 +12,26 @@
     def test_import_lock(self):
         # XXX XXX XXX this test fails if run together with all other tests
         # of this directory, but not when run alone
-        import thread, imp
+        import _thread, imp
         assert not imp.lock_held()
         done = []
         def f(i):
-            print '[ENTER %d]' % i
+            print('[ENTER %d]' % i)
             from imghdr import testall
-            print '[LEAVE %d]' % i
+            print('[LEAVE %d]' % i)
             done.append(1)
         for i in range(5):
-            print '[RUN %d]' % i
-            thread.start_new_thread(f, (i,))
+            print('[RUN %d]' % i)
+            _thread.start_new_thread(f, (i,))
         self.waitfor(lambda: len(done) == 5)
         assert len(done) == 5
 
     def test_with_many_dependencies(self):
-        import thread
+        import _thread
         import re      # -> causes nested imports
 
     def test_manual_locking(self):
-        import thread, os, imp, time, sys
+        import _thread, os, imp, time, sys
         f = open(os.path.join(self.tmpdir, 'foobaz2.py'), 'w')
         f.close()   # empty
         done = []
@@ -44,7 +44,7 @@
         assert not imp.lock_held()
         imp.acquire_lock()
         assert imp.lock_held()
-        thread.start_new_thread(f, ())
+        _thread.start_new_thread(f, ())
         time.sleep(0.9)
         assert not done
         assert imp.lock_held()
diff --git a/pypy/module/thread/test/test_local.py b/pypy/module/thread/test/test_local.py
--- a/pypy/module/thread/test/test_local.py
+++ b/pypy/module/thread/test/test_local.py
@@ -4,8 +4,8 @@
 class AppTestLocal(GenericTestThread):
 
     def test_local_1(self):
-        import thread
-        from thread import _local as tlsobject
+        import _thread
+        from _thread import _local as tlsobject
         freed = []
         class X:
             def __del__(self):
@@ -31,7 +31,7 @@
             finally:
                 ok.append(success)
         for i in range(20):
-            thread.start_new_thread(f, (i,))
+            _thread.start_new_thread(f, (i,))
         self.waitfor(lambda: len(ok) == 20, delay=3)
         assert ok == 20*[True] # see stdout/stderr for failures in the threads
 
@@ -47,14 +47,14 @@
 
 
     def test_local_init(self):
-        import thread
+        import _thread
         tags = [1, 2, 3, 4, 5, 54321]
         seen = []
 
-        raises(TypeError, thread._local, a=1)
-        raises(TypeError, thread._local, 1)
+        raises(TypeError, _thread._local, a=1)
+        raises(TypeError, _thread._local, 1)
 
-        class X(thread._local):
+        class X(_thread._local):
             def __init__(self, n):
                 assert n == 42
                 self.tag = tags.pop()
@@ -64,7 +64,7 @@
         def f():
             seen.append(x.tag)
         for i in range(5):
-            thread.start_new_thread(f, ())
+            _thread.start_new_thread(f, ())
         self.waitfor(lambda: len(seen) == 5, delay=2)
         seen1 = seen[:]
         seen1.sort()
@@ -72,8 +72,8 @@
         assert tags == []
 
     def test_local_setdict(self):
-        import thread
-        x = thread._local()
+        import _thread
+        x = _thread._local()
         # XXX: On Cpython these are AttributeErrors
         raises(TypeError, "x.__dict__ = 42")
         raises(TypeError, "x.__dict__ = {}")
@@ -84,6 +84,6 @@
             assert x.__dict__["spam"] == n
             done.append(1)
         for i in range(5):
-            thread.start_new_thread(f, (i,))
+            _thread.start_new_thread(f, (i,))
         self.waitfor(lambda: len(done) == 5, delay=2)
         assert len(done) == 5
diff --git a/pypy/module/thread/test/test_lock.py b/pypy/module/thread/test/test_lock.py
--- a/pypy/module/thread/test/test_lock.py
+++ b/pypy/module/thread/test/test_lock.py
@@ -6,17 +6,17 @@
 class AppTestLock(GenericTestThread):
 
     def test_lock(self):
-        import thread
-        lock = thread.allocate_lock()
-        assert type(lock) is thread.LockType
+        import _thread
+        lock = _thread.allocate_lock()
+        assert type(lock) is _thread.LockType
         assert lock.locked() is False
-        raises(thread.error, lock.release)
+        raises(_thread.error, lock.release)
         assert lock.locked() is False
         lock.acquire()
         assert lock.locked() is True
         lock.release()
         assert lock.locked() is False
-        raises(thread.error, lock.release)
+        raises(_thread.error, lock.release)
         assert lock.locked() is False
         feedback = []
         lock.acquire()
@@ -25,14 +25,14 @@
             feedback.append(42)
             lock.release()
         assert lock.locked() is True
-        thread.start_new_thread(f, ())
+        _thread.start_new_thread(f, ())
         lock.acquire()
         assert lock.locked() is True
         assert feedback == [42]
 
     def test_lock_in_with(self):
-        import thread
-        lock = thread.allocate_lock()
+        import _thread
+        lock = _thread.allocate_lock()
         feedback = []
         lock.acquire()
         def f():
@@ -40,7 +40,7 @@
             feedback.append(42)
             lock.release()
         assert lock.locked() is True
-        thread.start_new_thread(f, ())
+        _thread.start_new_thread(f, ())
         with lock:
             assert lock.locked() is True
             assert feedback == [42]
diff --git a/pypy/module/thread/test/test_thread.py b/pypy/module/thread/test/test_thread.py
--- a/pypy/module/thread/test/test_thread.py
+++ b/pypy/module/thread/test/test_thread.py
@@ -27,34 +27,34 @@
             cls.w_can_start_many_threads = space.wrap(True)
 
     def test_start_new_thread(self):
-        import thread
+        import _thread
         feedback = []
         please_start = []
         def f(x, y, z):
             self.waitfor(lambda: please_start)
             feedback.append(42)
-        thread.start_new_thread(f, (1, 2), {'z': 3})
+        _thread.start_new_thread(f, (1, 2), {'z': 3})
         assert feedback == []   # still empty
         please_start.append(1)  # trigger
         self.waitfor(lambda: feedback)
         assert feedback == [42]
 
     def test_thread_count(self):
-        import thread, time
+        import _thread, time
         feedback = []
         please_start = []
         def f():
             feedback.append(42)
             self.waitfor(lambda: please_start)
-        assert thread._count() == 0
-        thread.start_new_thread(f, ())
+        assert _thread._count() == 0
+        _thread.start_new_thread(f, ())
         self.waitfor(lambda: feedback)
-        assert thread._count() == 1
+        assert _thread._count() == 1
         please_start.append(1)  # trigger
         # XXX joining a thread seems difficult at applevel.
 
     def test_start_new_thread_args(self):
-        import thread
+        import _thread
         def f():
             pass
         test_args = [
@@ -64,27 +64,27 @@
         ]
         for args in test_args:
             try:
-                thread.start_new_thread(*args)
+                _thread.start_new_thread(*args)
                 assert False
             except TypeError:
                 pass
 
     def test_get_ident(self):
-        import thread
-        ident = thread.get_ident()
+        import _thread
+        ident = _thread.get_ident()
         feedback = []
         def f():
-            feedback.append(thread.get_ident())
-        ident2 = thread.start_new_thread(f, ())
+            feedback.append(_thread.get_ident())
+        ident2 = _thread.start_new_thread(f, ())
         assert ident2 != ident
-        assert ident == thread.get_ident()
+        assert ident == _thread.get_ident()
         self.waitfor(lambda: feedback)
         assert feedback == [ident2]
 
     def test_sys_getframe(self):
         # this checks that each thread gets its own ExecutionContext.
         def main():
-            import thread, sys
+            import _thread, sys
             def dump_frames(feedback):
                 f = sys._getframe()
                 for i in range(3):
@@ -103,7 +103,7 @@
             feedbacks = []
             for i in range(3):
                 feedback = []
-                thread.start_new_thread(dummyfn, (feedback,))
+                _thread.start_new_thread(dummyfn, (feedback,))
                 feedbacks.append(feedback)
             expected = 3*[['dump_frames', 'dummyfn', None]]   # without 'main'
             self.waitfor(lambda: feedbacks == expected)
@@ -111,23 +111,23 @@
         main()
 
     def test_thread_exit(self):
-        import thread, sys, StringIO
+        import _thread, sys, io
         def fn1():
-            thread.exit()
+            _thread.exit()
         def fn2():
             raise SystemExit
         def fn3():
             raise ValueError("hello world")
         prev = sys.stderr
         try:
-            sys.stderr = StringIO.StringIO()
-            thread.start_new_thread(fn1, ())
-            thread.start_new_thread(fn2, ())
+            sys.stderr = io.StringIO()
+            _thread.start_new_thread(fn1, ())
+            _thread.start_new_thread(fn2, ())
             self.busywait(0.2)   # time for the threads to finish
             assert sys.stderr.getvalue() == ''
 
-            sys.stderr = StringIO.StringIO()
-            thread.start_new_thread(fn3, ())
+            sys.stderr = io.StringIO()
+            _thread.start_new_thread(fn3, ())
             self.waitfor(lambda: "ValueError" in sys.stderr.getvalue())
             result = sys.stderr.getvalue()
             assert "ValueError" in result
@@ -137,7 +137,7 @@
             sys.stderr = prev
 
     def test_perthread_excinfo(self):
-        import thread, sys
+        import _thread, sys
         done = []
         def fn1(n):
             success = False
@@ -160,12 +160,12 @@
             finally:
                 done.append(success)
         for i in range(20):
-            thread.start_new_thread(fn1, (i,))
+            _thread.start_new_thread(fn1, (i,))
         self.waitfor(lambda: len(done) == 20)
         assert done == 20*[True]  # see stderr for failures in the threads
 
     def test_no_corruption(self):
-        import thread
+        import _thread
         lst = []
         done_marker = []
         def f(x, done):
@@ -174,18 +174,18 @@
             done.append(True)
         for i in range(0, 120, 40):
             done = []
-            thread.start_new_thread(f, (i, done))
+            _thread.start_new_thread(f, (i, done))
             done_marker.append(done)
         for done in done_marker:
             self.waitfor(lambda: done, delay=3)
             assert done    # see stderr for failures in threads
-        assert sorted(lst) == range(120)
+        assert sorted(lst) == list(range(120))
 
     def test_many_threads(self):
-        import thread, time
+        import _thread, time
         if self.can_start_many_threads:
             skip("this OS supports too many threads to check (> 1000)")
-        lock = thread.allocate_lock()
+        lock = _thread.allocate_lock()
         lock.acquire()
         def f():
             lock.acquire()
@@ -193,43 +193,43 @@
         try:
             try:
                 for i in range(1000):
-                    thread.start_new_thread(f, ())
+                    _thread.start_new_thread(f, ())
             finally:
                 lock.release()
                 # wait a bit to allow most threads to finish now
                 self.busywait(2.0)
-        except (thread.error, MemoryError):
+        except (_thread.error, MemoryError):
             pass
         else:
             raise Exception("could unexpectedly start 1000 threads")
 
     def test_stack_size(self):
-        import thread
-        thread.stack_size(0)
-        res = thread.stack_size(0)
+        import _thread
+        _thread.stack_size(0)
+        res = _thread.stack_size(0)
         assert res == 0
-        res = thread.stack_size(1024*1024)
+        res = _thread.stack_size(1024*1024)
         assert res == 0
-        res = thread.stack_size(2*1024*1024)
+        res = _thread.stack_size(2*1024*1024)
         assert res == 1024*1024
-        res = thread.stack_size(0)
+        res = _thread.stack_size(0)
         assert res == 2*1024*1024
 
     def test_interrupt_main(self):
-        import thread, time
+        import _thread, time
         import signal
 
         def f():
             time.sleep(0.5)
-            thread.interrupt_main()
+            _thread.interrupt_main()
 
         def busy_wait():
             for x in range(1000):
-                print 'tick...', x  # <-force the GIL to be released, as
-                time.sleep(0.01)    #   time.sleep doesn't do non-translated
+                print('tick...', x)  # <-force the GIL to be released, as
+                time.sleep(0.01)     #   time.sleep doesn't do non-translated
 
         # This is normally called by app_main.py
         signal.signal(signal.SIGINT, signal.default_int_handler)
 
-        thread.start_new_thread(f, ())
+        _thread.start_new_thread(f, ())
         raises(KeyboardInterrupt, busy_wait)


More information about the pypy-commit mailing list