def test_is_main_thread(): from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread if not is_current_thread_main_thread(): error_msg = 'Current thread does not seem to be a main thread. Details:\n' current_thread = threading.current_thread() error_msg += 'Current thread: %s\n' % (current_thread, ) if hasattr(threading, 'main_thread'): error_msg += 'Main thread found: %s\n' % ( threading.main_thread(), ) else: error_msg += 'Current main thread not instance of: %s (%s)' % ( threading._MainThread, current_thread.__class__.__mro__, ) raise AssertionError(error_msg) class NonMainThread(threading.Thread): def run(self): self.is_main_thread = is_current_thread_main_thread() non_main_thread = NonMainThread() non_main_thread.start() non_main_thread.join() assert not non_main_thread.is_main_thread
def test_is_main_thread(): from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread from _pydevd_bundle.pydevd_utils import dump_threads if not is_current_thread_main_thread(): error_msg = 'Current thread does not seem to be a main thread. Details:\n' current_thread = threading.current_thread() error_msg += 'Current thread: %s\n' % (current_thread, ) if hasattr(threading, 'main_thread'): error_msg += 'Main thread found: %s\n' % ( threading.main_thread(), ) else: error_msg += 'Current main thread not instance of: %s (%s)' % ( threading._MainThread, current_thread.__class__.__mro__, ) try: from StringIO import StringIO except: from io import StringIO stream = StringIO() dump_threads(stream=stream) error_msg += '\n\n' + stream.getvalue() raise AssertionError(error_msg) class NonMainThread(threading.Thread): def run(self): self.is_main_thread = is_current_thread_main_thread() non_main_thread = NonMainThread() non_main_thread.start() non_main_thread.join() assert not non_main_thread.is_main_thread
def test_is_main_thread(): from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread assert is_current_thread_main_thread() class NonMainThread(threading.Thread): def run(self): self.is_main_thread = is_current_thread_main_thread() non_main_thread = NonMainThread() non_main_thread.start() non_main_thread.join() assert not non_main_thread.is_main_thread
def create_interrupt_this_thread_callback(): ''' The idea here is returning a callback that when called will generate a KeyboardInterrupt in the thread that called this function. If this is the main thread, this means that it'll emulate a Ctrl+C (which may stop I/O and sleep operations). For other threads, this will call PyThreadState_SetAsyncExc to raise a KeyboardInterrupt before the next instruction (so, it won't really interrupt I/O or sleep operations). :return callable: Returns a callback that will interrupt the current thread (this may be called from an auxiliary thread). ''' tid = thread_get_ident() if is_current_thread_main_thread(): main_thread = threading.current_thread() def raise_on_this_thread(): pydev_log.debug('Callback to interrupt main thread.') pydevd_utils.interrupt_main_thread(main_thread) else: # Note: this works in the sense that it can stop some cpu-intensive slow operation, # but we can't really interrupt the thread out of some sleep or I/O operation # (this will only be raised when Python is about to execute the next instruction). def raise_on_this_thread(): if IS_CPYTHON: pydev_log.debug('Interrupt thread: %s', tid) ctypes.pythonapi.PyThreadState_SetAsyncExc( ctypes.c_long(tid), ctypes.py_object(KeyboardInterrupt)) else: pydev_log.debug( 'It is only possible to interrupt non-main threads in CPython.' ) return raise_on_this_thread
def test_is_main_thread(): from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread if not is_current_thread_main_thread(): error_msg = 'Current thread does not seem to be a main thread. Details:\n' current_thread = threading.current_thread() error_msg += 'Current thread: %s\n' % (current_thread,) if hasattr(threading, 'main_thread'): error_msg += 'Main thread found: %s\n' % (threading.main_thread(),) else: error_msg += 'Current main thread not instance of: %s (%s)' % ( threading._MainThread, current_thread.__class__.__mro__,) raise AssertionError(error_msg) class NonMainThread(threading.Thread): def run(self): self.is_main_thread = is_current_thread_main_thread() non_main_thread = NonMainThread() non_main_thread.start() non_main_thread.join() assert not non_main_thread.is_main_thread
def run(self): self.is_main_thread = is_current_thread_main_thread()
def test_is_main_thread(): ''' This is now skipped due to it failing sometimes (only on Windows). I (fabioz) am not 100% sure on why this happens, but when this happens the initial thread for the tests seems to be a non main thread. i.e.: With an autouse fixture with a scope='session' with the code and error message below, it's possible to see that at even at the `conftest` import (where indent_at_import is assigned) the current thread is already not the main thread. As far as I know this seems to be an issue in how pytest-xdist is running the tests (i.e.: I couldn't reproduce this without running with `python -m pytest -n 0 ...`). -------- Code to check error / error output ---------- from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread import threading indent_at_import = threading.get_ident() @pytest.yield_fixture(autouse=True, scope='session') def check_main_thread_session(request): if not is_current_thread_main_thread(): error_msg = 'Current thread does not seem to be a main thread at the start of the session. Details:\n' current_thread = threading.current_thread() error_msg += 'Current thread: %s\n' % (current_thread,) error_msg += 'Current thread ident: %s\n' % (current_thread.ident,) error_msg += 'ident at import: %s\n' % (indent_at_import,) error_msg += 'curr ident: %s\n' % (threading.get_ident(),) if hasattr(threading, 'main_thread'): error_msg += 'Main thread found: %s\n' % (threading.main_thread(),) error_msg += 'Main thread id: %s\n' % (threading.main_thread().ident,) else: error_msg += 'Current main thread not instance of: %s (%s)\n' % ( threading._MainThread, current_thread.__class__.__mro__,) > raise AssertionError(error_msg) E AssertionError: Current thread does not seem to be a main thread at the start of the session. Details: E Current thread: <_DummyThread(Dummy-2, started daemon 7072)> E Current thread ident: 7072 E ident at import: 7072 E curr ident: 7072 E Main thread found: <_MainThread(MainThread, started 5924)> E Main thread id: 5924 conftest.py:67: AssertionError ''' from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread from _pydevd_bundle.pydevd_utils import dump_threads if not is_current_thread_main_thread(): error_msg = 'Current thread does not seem to be a main thread. Details:\n' current_thread = threading.current_thread() error_msg += 'Current thread: %s\n' % (current_thread, ) if hasattr(threading, 'main_thread'): error_msg += 'Main thread found: %s\n' % ( threading.main_thread(), ) else: error_msg += 'Current main thread not instance of: %s (%s)' % ( threading._MainThread, current_thread.__class__.__mro__, ) try: from StringIO import StringIO except: from io import StringIO stream = StringIO() dump_threads(stream=stream) error_msg += '\n\n' + stream.getvalue() raise AssertionError(error_msg) class NonMainThread(threading.Thread): def run(self): self.is_main_thread = is_current_thread_main_thread() non_main_thread = NonMainThread() non_main_thread.start() non_main_thread.join() assert not non_main_thread.is_main_thread