Skip to content

Commit b1a20bf

Browse files
Merge python#16
16: Warn for specific thread module methods r=ltratt a=nanjekyejoannah Dont merge until python#13 and python#14 are merged, some helper code cuts across. This replaces python#15 Threading module Notes Python 2: ``` >>> from thread import get_ident >>> from threading import get_ident Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name get_ident >>> import threading >>> from threading import _get_ident >>> ``` Python 3: ``` >>> from threading import get_ident >>> from thread import get_ident Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'thread' > ``` **Note:** There is no neutral way of porting Co-authored-by: Joannah Nanjekye <jnanjekye@python.org>
2 parents fa27e80 + 96a8370 commit b1a20bf

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

Lib/test/test_thread.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import random
44
from test import support
5+
from test.test_support import check_py3k_warnings
56
thread = support.import_module('thread')
67
import time
78
import sys
@@ -157,6 +158,51 @@ def mywrite(self, *args):
157158
started.acquire()
158159
self.assertIn("Traceback", stderr.getvalue())
159160

161+
def test_py3k_thread_module(self):
162+
expected = "In 3.x, the thread module is removed: use the threading module instead"
163+
with check_py3k_warnings() as w:
164+
import thread
165+
166+
def test_py3k_thread_module_get_ident(self):
167+
expected = "thread.get_ident is removed in 3.x: use the threading.get_ident instead"
168+
with check_py3k_warnings() as w:
169+
thread.get_ident()
170+
171+
def test_py3k_thread_module_start_new_thread(self):
172+
expected = "thread.start_new_thread is removed in 3.x: use the threading._start_new_thread instead"
173+
with check_py3k_warnings() as w:
174+
def f():
175+
ident.append(threading.currentThread().ident)
176+
done.set()
177+
thread.start_new_thread((f), ())
178+
179+
def test_py3k_thread_module_allocate(self):
180+
expected = "thread.allocate_lock is removed in 3.x: use the threading._allocate_lock instead"
181+
with check_py3k_warnings() as w:
182+
thread.allocate_lock()
183+
184+
def test_py3k_thread_module_exit_thread(self):
185+
expected = "thread.exit is removed in 3.x: no equivalent method exists, raising SystemExit will exit a thread"
186+
with check_py3k_warnings() as w:
187+
with self.assertRaises(SystemExit):
188+
thread.exit_thread()
189+
190+
def test_py3k_thread_module_interrupt_main(self):
191+
expected = "thread.interrupt_main is removed in 3.x: no equivalent method exists, raising KeyboardInterrupt will interruot the main thread"
192+
with check_py3k_warnings() as w:
193+
with self.assertRaises(KeyboardInterrupt):
194+
thread.interrupt_main()
195+
196+
def test_py3k_thread_module_count(self):
197+
expected = "thread._count is removed in 3.x: use the threading.count instead"
198+
with check_py3k_warnings() as w:
199+
thread._count()
200+
201+
def test_py3k_thread_module_stack_size(self):
202+
expected = "thread.stack_size is removed in 3.x: use threading.stack_size instead"
203+
with check_py3k_warnings() as w:
204+
thread.stack_size()
205+
160206

161207
class Barrier:
162208
def __init__(self, num_threads):

Lib/test/test_threading.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Very rudimentary test of threading module
22

33
import test.test_support
4-
from test.test_support import verbose, cpython_only
4+
from test.test_support import verbose, cpython_only, check_py3k_warnings
55
from test.script_helper import assert_python_ok
66

77
import random
@@ -480,6 +480,21 @@ def test_BoundedSemaphore_limit(self):
480480
t.join()
481481
self.assertRaises(ValueError, bs.release)
482482

483+
def test_threading_module_method_rename(self):
484+
import threading
485+
expected = "_get_ident is removed in 3.x: use get_ident instead"
486+
with check_py3k_warnings() as w:
487+
threading. _get_ident()
488+
expected = "_start_new_thread is removed in 3.x: use start_new_thread instead"
489+
with check_py3k_warnings() as w:
490+
def f():
491+
ident.append(threading.currentThread().ident)
492+
done.set()
493+
threading._start_new_thread((f), ())
494+
expected = "_allocate_lock is removed in 3.x: use allocate_lock instead"
495+
with check_py3k_warnings() as w:
496+
threading._allocate_lock()
497+
483498
class ThreadJoinOnShutdown(BaseTestCase):
484499

485500
# Between fork() and exec(), only async-safe functions are allowed (issues

Lib/threading.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232
'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
3333
'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
3434

35+
def _get_ident():
36+
warnings.warnpy3k_with_fix("_get_ident() is renamed in 3.x", "use get_ident() instead",
37+
stacklevel=2)
38+
thread.get_ident
39+
40+
def _start_new_thread():
41+
warnings.warnpy3k_with_fix("_start_new_thread() is removed in 3.x", "use start_new_thread() instead",
42+
stacklevel=2)
43+
thread.start_new_thread
44+
45+
def _allocate_lock():
46+
warnings.warnpy3k_with_fix("_allocate_lock() is removed in 3.x", "use allocate_lock instead",
47+
stacklevel=2)
48+
thread.allocate_lock
49+
3550
_start_new_thread = thread.start_new_thread
3651
_allocate_lock = thread.allocate_lock
3752
_get_ident = thread.get_ident

Modules/threadmodule.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,10 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
657657
struct bootstate *boot;
658658
long ident;
659659

660+
if (PyErr_WarnPy3k_WithFix("thread.start_new_thread is removed in 3.x",
661+
"use the threading._start_new_thread instead", 1))
662+
return NULL;
663+
660664
if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
661665
&func, &args, &keyw))
662666
return NULL;
@@ -718,6 +722,10 @@ printed unless the exception is SystemExit.\n");
718722
static PyObject *
719723
thread_PyThread_exit_thread(PyObject *self)
720724
{
725+
if (PyErr_WarnPy3k_WithFix("thread.exit is removed in 3.x",
726+
"no equivalent method exists, raising SystemExit will exit a thread", 1))
727+
return NULL;
728+
721729
PyErr_SetNone(PyExc_SystemExit);
722730
return NULL;
723731
}
@@ -732,6 +740,10 @@ thread to exit silently unless the exception is caught.");
732740
static PyObject *
733741
thread_PyThread_interrupt_main(PyObject * self)
734742
{
743+
if (PyErr_WarnPy3k_WithFix("thread.interrupt_main is removed in 3.x",
744+
"no equivalent method exists, raising KeyboardInterrupt will interruot the main thread", 1))
745+
return NULL;
746+
735747
PyErr_SetInterrupt();
736748
Py_INCREF(Py_None);
737749
return Py_None;
@@ -749,6 +761,10 @@ static lockobject *newlockobject(void);
749761
static PyObject *
750762
thread_PyThread_allocate_lock(PyObject *self)
751763
{
764+
if (PyErr_WarnPy3k_WithFix("thread.allocate_lock is removed in 3.x",
765+
"use the threading._allocate_lock instead", 1))
766+
return NULL;
767+
752768
return (PyObject *) newlockobject();
753769
}
754770

@@ -762,6 +778,10 @@ static PyObject *
762778
thread_get_ident(PyObject *self)
763779
{
764780
long ident;
781+
if (PyErr_WarnPy3k_WithFix("thread.get_ident is removed in 3.x",
782+
"use the threading.get_ident instead", 1))
783+
return NULL;
784+
765785
ident = PyThread_get_thread_ident();
766786
if (ident == -1) {
767787
PyErr_SetString(ThreadError, "no current thread ident");
@@ -784,6 +804,10 @@ A thread's identity may be reused for another thread after it exits.");
784804
static PyObject *
785805
thread__count(PyObject *self)
786806
{
807+
if (PyErr_WarnPy3k_WithFix("thread.count is removed in 3.x",
808+
"use the threading._count instead", 1))
809+
return NULL;
810+
787811
return PyInt_FromLong(nb_threads);
788812
}
789813

@@ -806,6 +830,10 @@ thread_stack_size(PyObject *self, PyObject *args)
806830
Py_ssize_t new_size = 0;
807831
int rc;
808832

833+
if (PyErr_WarnPy3k_WithFix("thread.stack_size is removed in 3.x",
834+
"use the threading.stack_size instead", 1))
835+
return NULL;
836+
809837
if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
810838
return NULL;
811839

@@ -904,6 +932,10 @@ initthread(void)
904932
{
905933
PyObject *m, *d;
906934

935+
if (PyErr_WarnPy3k_WithFix("In 3.x, the thread module is removed",
936+
"use the threading module instead", 1))
937+
return;
938+
907939
/* Initialize types: */
908940
if (PyType_Ready(&localdummytype) < 0)
909941
return;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy