Skip to content

Commit fb6d70e

Browse files
committed
install debug into DebugProxy
1 parent f080d39 commit fb6d70e

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

devtools/__main__.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,27 @@
77
# language=python
88
install_code = """
99
# add devtools `debug` function to builtins
10-
import sys
11-
# we don't install here for pytest as it breaks pytest, it is
12-
# installed later by a pytest fixture
13-
if not sys.argv[0].endswith('pytest'):
14-
import builtins
15-
try:
16-
from devtools import debug
17-
except ImportError:
18-
pass
19-
else:
20-
setattr(builtins, 'debug', debug)
10+
# we don't want to import devtools until it's required
11+
# since it breaks pytest, hence this proxy
12+
class DebugProxy:
13+
def __init__(self):
14+
self._debug = None
15+
16+
def _import_debug(self):
17+
if self._debug is None:
18+
from devtools import debug
19+
self._debug = debug
20+
21+
def __call__(self, *args, **kwargs):
22+
self._import_debug()
23+
return self._debug(*args, **kwargs)
24+
25+
def __getattr__(self, item):
26+
self._import_debug()
27+
return getattr(self._debug, item)
28+
29+
import builtins
30+
setattr(builtins, 'debug', DebugProxy())
2131
"""
2232

2333

@@ -27,12 +37,6 @@ def print_code() -> int:
2737

2838

2939
def install() -> int:
30-
print('[WARNING: this command is experimental, report issues at github.com/samuelcolvin/python-devtools]\n')
31-
32-
if hasattr(builtins, 'debug'):
33-
print('Looks like devtools is already installed.')
34-
return 0
35-
3640
try:
3741
import sitecustomize # type: ignore
3842
except ImportError:
@@ -48,7 +52,11 @@ def install() -> int:
4852
else:
4953
install_path = Path(sitecustomize.__file__)
5054

51-
print(f'Found path "{install_path}" to install devtools into __builtins__')
55+
if hasattr(builtins, 'debug'):
56+
print(f'Looks like devtools is already installed at `{install_path}`.')
57+
return 0
58+
59+
print(f'Found path `{install_path}` to install devtools into __builtins__')
5260
print('To install devtools, run the following command:\n')
5361
print(f' python -m devtools print-code >> {install_path}\n')
5462
if not install_path.is_relative_to(Path.home()):

0 commit comments

Comments
 (0)