Skip to content

Commit d5c355c

Browse files
author
Victor Stinner
committed
Issue #11223: Replace threading._info() by sys.thread_info
1 parent fab6c70 commit d5c355c

13 files changed

Lines changed: 119 additions & 108 deletions

File tree

Doc/library/sys.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,35 @@ always available.
961961
to a console and Python apps started with :program:`pythonw`.
962962

963963

964+
.. data:: thread_info
965+
966+
A :term:`struct sequence` holding information about the thread
967+
implementation.
968+
969+
+------------------+---------------------------------------------------------+
970+
| Attribute | Explanation |
971+
+==================+=========================================================+
972+
| :const:`name` | Name of the thread implementation: |
973+
| | |
974+
| | * ``'nt'``: Windows threads |
975+
| | * ``'os2'``: OS/2 threads |
976+
| | * ``'pthread'``: POSIX threads |
977+
| | * ``'solaris'``: Solaris threads |
978+
+------------------+---------------------------------------------------------+
979+
| :const:`lock` | Name of the lock implementation: |
980+
| | |
981+
| | * ``'semaphore'``: a lock uses a semaphore |
982+
| | * ``'mutex+cond'``: a lock uses a mutex |
983+
| | and a condition variable |
984+
| | * ``None`` if this information is unknown |
985+
+------------------+---------------------------------------------------------+
986+
| :const:`version` | Name and version of the thread library. It is a string, |
987+
| | or ``None`` if these informations are unknown. |
988+
+------------------+---------------------------------------------------------+
989+
990+
.. versionadded:: 3.3
991+
992+
964993
.. data:: tracebacklimit
965994

966995
When this variable is set to an integer value, it determines the maximum number

Doc/library/threading.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,30 +175,6 @@ This module defines the following functions and objects:
175175
Availability: Windows, systems with POSIX threads.
176176

177177

178-
.. function:: _info()
179-
180-
Return a dictionary with informations about the thread implementation.
181-
The ``'name'`` key gives the name of the thread implementation (string):
182-
183-
* ``'nt'``: Windows threads
184-
* ``'os2'``: OS/2 threads
185-
* ``'pthread'``: POSIX threads
186-
* ``'solaris'``: Solaris threads
187-
188-
POSIX threads have two more keys:
189-
190-
* ``'lock_implementation'`` (string): name of the lock
191-
implementation
192-
193-
* ``'semaphore'``: a lock uses a semaphore
194-
* ``'mutex+cond'``: a lock uses a mutex and a condition variable
195-
196-
* ``'pthread_version'`` (string, optional): name and version of the pthread
197-
library
198-
199-
.. versionadded:: 3.3
200-
201-
202178
This module also defines the following constant:
203179

204180
.. data:: TIMEOUT_MAX

Doc/whatsnew/3.3.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ connection when done::
112112

113113
(Contributed by Giampaolo Rodolà in :issue:`9795`)
114114

115-
threading
116-
---------
115+
sys
116+
---
117117

118-
* The :mod:`threading` module has a new :func:`~threading._info` function which
119-
provides informations about the thread implementation.
118+
* The :mod:`sys` module has a new :func:`~sys.thread_info` :term:`struct
119+
sequence` holding informations about the thread implementation.
120120

121121
(:issue:`11223`)
122122

Include/pythread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PyAPI_FUNC(void) PyThread_release_lock(PyThread_type_lock);
7474
PyAPI_FUNC(size_t) PyThread_get_stacksize(void);
7575
PyAPI_FUNC(int) PyThread_set_stacksize(size_t);
7676

77-
PyAPI_FUNC(PyObject*) _PyThread_Info(void);
77+
PyAPI_FUNC(PyObject*) PyThread_GetInfo(void);
7878

7979
/* Thread Local Storage (TLS) API */
8080
PyAPI_FUNC(int) PyThread_create_key(void);

Lib/_dummy_thread.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,3 @@ def interrupt_main():
149149
else:
150150
global _interrupt
151151
_interrupt = True
152-
153-
def info():
154-
return {'name': 'dummy'}

Lib/test/test_os.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,10 @@
2727
# and unmaintained) linuxthreads threading library. There's an issue
2828
# when combining linuxthreads with a failed execv call: see
2929
# http://bugs.python.org/issue4970.
30-
USING_LINUXTHREADS = False
31-
if threading:
32-
info = threading._info()
33-
try:
34-
pthread_version = info['pthread_version']
35-
except KeyError:
36-
pass
37-
else:
38-
USING_LINUXTHREADS = pthread_version.startswith("linuxthreads")
30+
if hasattr(sys, 'thread_info') and sys.thread_info.version:
31+
USING_LINUXTHREADS = sys.thread_info.version.startswith("linuxthreads")
32+
else:
33+
USING_LINUXTHREADS = False
3934

4035
# Tests creating TESTFN
4136
class FileTests(unittest.TestCase):

Lib/test/test_sys.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,14 @@ def test_attributes(self):
474474
if not sys.platform.startswith('win'):
475475
self.assertIsInstance(sys.abiflags, str)
476476

477+
@unittest.skipUnless(hasattr(sys, 'thread_info'),
478+
'Threading required for this test.')
479+
def test_thread_info(self):
480+
info = sys.thread_info
481+
self.assertTrue(len(info), 3)
482+
self.assertIn(info.name, ('nt', 'os2', 'pthread', 'solaris', None))
483+
self.assertIn(info.lock, ('semaphore', 'mutex+cond', None))
484+
477485
def test_43581(self):
478486
# Can't use sys.stdout, as this is a StringIO object when
479487
# the test runs under regrtest.

Lib/test/test_threading.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -719,24 +719,14 @@ class BarrierTests(lock_tests.BarrierTests):
719719
barriertype = staticmethod(threading.Barrier)
720720

721721

722-
class MiscTests(unittest.TestCase):
723-
def test_info(self):
724-
info = threading._info()
725-
self.assertIn(info['name'],
726-
'nt os2 pthread solaris'.split())
727-
if info['name'] == 'pthread':
728-
self.assertIn(info['lock_implementation'],
729-
('semaphore', 'mutex+cond'))
730-
731-
732722
def test_main():
733723
test.support.run_unittest(LockTests, PyRLockTests, CRLockTests, EventTests,
734724
ConditionAsRLockTests, ConditionTests,
735725
SemaphoreTests, BoundedSemaphoreTests,
736726
ThreadTests,
737727
ThreadJoinOnShutdown,
738728
ThreadingExceptionTests,
739-
BarrierTests, MiscTests,
729+
BarrierTests,
740730
)
741731

742732
if __name__ == "__main__":

Lib/test/test_threadsignals.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
process_pid = os.getpid()
1515
signalled_all=thread.allocate_lock()
1616

17-
info = thread.info()
18-
USING_PTHREAD_COND = (info['name'] == 'pthread'
19-
and info['lock_implementation'] == 'mutex+cond')
17+
USING_PTHREAD_COND = (sys.thread_info.name == 'pthread'
18+
and sys.thread_info.lock == 'mutex+cond')
2019

2120
def registerSignals(for_usr1, for_usr2, for_alrm):
2221
usr1 = signal.signal(signal.SIGUSR1, for_usr1)

Lib/threading.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
2121
'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
22-
'Timer', 'setprofile', 'settrace', 'local', 'stack_size', '_info']
22+
'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
2323

2424
# Rename some stuff so "from threading import *" is safe
2525
_start_new_thread = _thread.start_new_thread
@@ -31,7 +31,6 @@
3131
except AttributeError:
3232
_CRLock = None
3333
TIMEOUT_MAX = _thread.TIMEOUT_MAX
34-
_info = _thread.info
3534
del _thread
3635

3736

0 commit comments

Comments
 (0)