~ubuntu-branches/ubuntu/utopic/python3.3/utopic

« back to all changes in this revision

Viewing changes to Lib/test/test_threading.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-01-27 12:13:57 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20140127121357-60ennw934dn1qirz
Tags: 3.3.4~rc1-1
Python 3.3.4 release candidate 1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import os
18
18
from test.script_helper import assert_python_ok, assert_python_failure
19
19
import subprocess
 
20
try:
 
21
    import _testcapi
 
22
except ImportError:
 
23
    _testcapi = None
20
24
 
21
25
from test import lock_tests
22
26
 
769
773
        for t in threads:
770
774
            t.join()
771
775
 
 
776
    @unittest.skipIf(_testcapi is None, "need _testcapi module")
 
777
    def test_frame_tstate_tracing(self):
 
778
        # Issue #14432: Crash when a generator is created in a C thread that is
 
779
        # destroyed while the generator is still used. The issue was that a
 
780
        # generator contains a frame, and the frame kept a reference to the
 
781
        # Python state of the destroyed C thread. The crash occurs when a trace
 
782
        # function is setup.
 
783
 
 
784
        def noop_trace(frame, event, arg):
 
785
            # no operation
 
786
            return noop_trace
 
787
 
 
788
        def generator():
 
789
            while 1:
 
790
                yield "genereator"
 
791
 
 
792
        def callback():
 
793
            if callback.gen is None:
 
794
                callback.gen = generator()
 
795
            return next(callback.gen)
 
796
        callback.gen = None
 
797
 
 
798
        old_trace = sys.gettrace()
 
799
        sys.settrace(noop_trace)
 
800
        try:
 
801
            # Install a trace function
 
802
            threading.settrace(noop_trace)
 
803
 
 
804
            # Create a generator in a C thread which exits after the call
 
805
            _testcapi.call_in_temporary_c_thread(callback)
 
806
 
 
807
            # Call the generator in a different Python thread, check that the
 
808
            # generator didn't keep a reference to the destroyed thread state
 
809
            for test in range(3):
 
810
                # The trace function is still called here
 
811
                callback()
 
812
        finally:
 
813
            sys.settrace(old_trace)
 
814
 
772
815
 
773
816
class ThreadingExceptionTests(BaseTestCase):
774
817
    # A RuntimeError should be raised if Thread.start() is called