1# Defines which version of the trace_dispatch we'll use.
2# Should give warning only here if cython is not available but supported.
3
4import os
5import sys
6from _pydevd_bundle.pydevd_constants import CYTHON_SUPPORTED
7
8
9use_cython = os.getenv('PYDEVD_USE_CYTHON', None)
10dirname = os.path.dirname(os.path.dirname(__file__))
11# Do not show incorrect warning for .egg files for Remote debugger
12if not CYTHON_SUPPORTED or dirname.endswith('.egg'):
13    # Do not try to import cython extensions if cython isn't supported
14    use_cython = 'NO'
15
16
17def delete_old_compiled_extensions():
18    pydev_dir = os.path.dirname(os.path.dirname(__file__))
19    _pydevd_bundle_dir = os.path.dirname(__file__)
20    _pydevd_frame_eval_dir = os.path.join(pydev_dir, '_pydevd_frame_eval')
21    try:
22        import shutil
23        for file in os.listdir(_pydevd_bundle_dir):
24            if file.startswith("pydevd") and file.endswith(".so"):
25                os.remove(os.path.join(_pydevd_bundle_dir, file))
26        for file in os.listdir(_pydevd_frame_eval_dir):
27            if file.startswith("pydevd") and file.endswith(".so"):
28                os.remove(os.path.join(_pydevd_frame_eval_dir, file))
29        build_dir = os.path.join(pydev_dir, "build")
30        if os.path.exists(build_dir):
31            shutil.rmtree(os.path.join(pydev_dir, "build"))
32    except OSError:
33        from _pydev_bundle.pydev_monkey import log_error_once
34        log_error_once("warning: failed to delete old cython speedups. Please delete all *.so files from the directories "
35                       "\"%s\" and \"%s\"" % (_pydevd_bundle_dir, _pydevd_frame_eval_dir))
36
37
38if use_cython == 'YES':
39    # We must import the cython version if forcing cython
40    from _pydevd_bundle.pydevd_cython_wrapper import trace_dispatch as _trace_dispatch, global_cache_skips, global_cache_frame_skips
41    def trace_dispatch(py_db, frame, event, arg):
42        return _trace_dispatch(py_db, frame, event, arg)
43
44elif use_cython == 'NO':
45    # Use the regular version if not forcing cython
46    from _pydevd_bundle.pydevd_trace_dispatch_regular import trace_dispatch, global_cache_skips, global_cache_frame_skips  # @UnusedImport
47
48elif use_cython is None:
49    # Regular: use fallback if not found and give message to user
50    try:
51        from _pydevd_bundle.pydevd_cython_wrapper import trace_dispatch as _trace_dispatch, global_cache_skips, global_cache_frame_skips
52        def trace_dispatch(py_db, frame, event, arg):
53            return _trace_dispatch(py_db, frame, event, arg)
54
55        # This version number is always available
56        from _pydevd_bundle.pydevd_additional_thread_info_regular import version as regular_version
57        # This version number from the already compiled cython extension
58        from _pydevd_bundle.pydevd_cython_wrapper import version as cython_version
59        if cython_version != regular_version:
60            delete_old_compiled_extensions()
61            raise ImportError()
62
63    except ImportError:
64        from _pydevd_bundle.pydevd_additional_thread_info_regular import PyDBAdditionalThreadInfo  # @UnusedImport
65        from _pydevd_bundle.pydevd_trace_dispatch_regular import trace_dispatch, global_cache_skips, global_cache_frame_skips  # @UnusedImport
66        from _pydev_bundle.pydev_monkey import log_error_once
67
68        log_error_once("warning: Debugger speedups using cython not found. Run '\"%s\" \"%s\" build_ext --inplace' to build." % (
69            sys.executable, os.path.join(dirname, 'setup_cython.py')))
70
71else:
72    raise RuntimeError('Unexpected value for PYDEVD_USE_CYTHON: %s (accepted: YES, NO)' % (use_cython,))
73
74
75