File: _debugger_case20.py

package info (click to toggle)
pydevd 3.3.0%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,892 kB
  • sloc: python: 77,508; cpp: 1,869; sh: 368; makefile: 50; ansic: 4
file content (99 lines) | stat: -rw-r--r-- 2,066 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pydevd
import threading
import sys

original = pydevd.PyDB.notify_thread_created

found = set()


def new_notify_thread_created(self, thread_id, thread, *args, **kwargs):
    found.add(thread)
    return original(self, thread_id, thread, *args, **kwargs)


pydevd.PyDB.notify_thread_created = new_notify_thread_created

ok = []


class MyThread(threading.Thread):
    def run(self):
        if self not in found:
            ok.append(False)
        else:
            ok.append(True)


class ManualCreatedThreadPy313:
    def __init__(self):
        self.ev = threading.Event()

    def run(self):
        try:
            if threading.current_thread() not in found:
                ok.append(False)
            else:
                ok.append(True)
        finally:
            self.ev.set()

    def start(self):
        import _thread

        _thread.start_joinable_thread(self.run)

    def join(self):
        self.ev.wait()


class ManualCreatedThreadFromThreadModule:
    def __init__(self):
        self.ev = threading.Event()

    def run(self):
        try:
            if threading.current_thread() not in found:
                ok.append(False)
            else:
                ok.append(True)
        finally:
            self.ev.set()

    def start(self):
        try:
            import thread
        except Exception:
            import _thread as thread

        thread.start_new_thread(self.run)

    def join(self):
        self.ev.wait()


if __name__ == "__main__":
    threads: list = []

    if sys.version_info[:2] >= (3, 13):
        t1 = ManualCreatedThreadPy313()
        t1.start()
        threads.append(t1)

    t2 = ManualCreatedThreadFromThreadModule()
    t2.start()
    threads.append(t2)

    for i in range(15):
        t = MyThread()
        t.start()
        threads.append(t)

    for t in threads:
        t.join()

    assert len(ok) == len(threads)
    assert all(ok), "Expected all threads to be notified of their creation before starting to run. Found: %s" % (ok,)

    found.clear()
    print("TEST SUCEEDED")