Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Lib/test/test_threading.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Very rudimentary test of threading module

import test.test_support
from test.test_support import verbose, cpython_only
from test.test_support import verbose, cpython_only, check_py3k_warnings
from test.script_helper import assert_python_ok

import random
Expand Down Expand Up @@ -480,6 +480,23 @@ def test_BoundedSemaphore_limit(self):
t.join()
self.assertRaises(ValueError, bs.release)

def test_py3k_thread_module(sellf):
expected = "In 3.x, the thread module is removed: use the threading module instead"
with check_py3k_warnings() as w:
from thread import get_ident

def test_threading_module_method_rename(self):
import threading
expected = "_get_ident is removed in 3.x: use get_ident instead"
with check_py3k_warnings() as w:
_get_indent()
expected = "_start_new_thread is removed in 3.x: use start_new_thread instead"
with check_py3k_warnings() as w:
_start_new_thread()
expected = "_allocate_lock is removed in 3.x: use allocate_lock instead"
with check_py3k_warnings() as w:
_allocate_lock()

class ThreadJoinOnShutdown(BaseTestCase):

# Between fork() and exec(), only async-safe functions are allowed (issues
Expand Down
17 changes: 14 additions & 3 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@
'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
'Timer', 'setprofile', 'settrace', 'local', 'stack_size']

_start_new_thread = thread.start_new_thread
_allocate_lock = thread.allocate_lock
_get_ident = thread.get_ident
def _get_ident():
warnings.warnpy3k_with_fix("_get_ident is removed in 3.x", "use get_ident instead",
stacklevel=2)
thread.get_ident

def _start_new_thread():
warnings.warnpy3k_with_fix("_start_new_thread is removed in 3.x", "use start_new_thread instead",
stacklevel=2)
thread.start_new_thread

def _allocate_lock():
warnings.warnpy3k_with_fix("_allocate_lock is removed in 3.x", "use allocate_lock instead",
stacklevel=2)
thread.allocate_lock
ThreadError = thread.error
del thread

Expand Down
4 changes: 4 additions & 0 deletions Modules/threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,10 @@ initthread(void)
{
PyObject *m, *d;

if (PyErr_WarnPy3k_WithFix("In 3.x, the thread module is removed",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the threading module compatible with thread? In other words, if I e.g. turn import thread.X into import thread.X will that work? I wonder if we need more fine-grained warnings?

Copy link
Collaborator Author

@nanjekyejoannah nanjekyejoannah Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good one, that is possible, we can warn both ways. For module, and the method where the warning is more fine grained e.g "thread.get_indent was removed in 3.x: use threading._ge_indent instead"

Also note even if you use the threading module, some methods were renamed to have an underscore e.g threading.get_indent is threading._get_indent there is no way to write code that works on both 2.x and 3.x for like 3 methods

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the threading module in Python 2 compatible with Python 3? I'm wondering if we have to do some warnings in pygrate2 and some others in pygrate3?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely, some methods are named with an underscore in 2.x and yet in 3.x they dont have the underscore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 methods are completely incompatible

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, do we need to forward port too? its as simple as aliasing the incompatible methods

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, if the user can change things in pygrate2, we shouldn't warn in pygrate3. But if they can't change in pygrate2, we should consider aliases & warnings in pygrate3.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK SGTM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do that in this PR? [I tend to think so, but I don't have a firm opinion.]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Onmy radar this morning, I want to handle as part of this PR

"use the threading module instead", 1))
return;

/* Initialize types: */
if (PyType_Ready(&localdummytype) < 0)
return;
Expand Down