Skip to content

Commit 0909f01

Browse files
authored
Use secure builtins standard module, instead of the __builtins__ (#109)
* Use secure builtins standard module, instead of the __builtins__ * use single quotes, and update the docs example * single quotes again, use hasattr()
1 parent 297e922 commit 0909f01

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

devtools/__main__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
import os
23
import sys
34
from pathlib import Path
@@ -7,12 +8,13 @@
78
# language=python
89
install_code = """
910
# add devtools `debug` function to builtins
11+
import builtins
1012
try:
1113
from devtools import debug
1214
except ImportError:
1315
pass
1416
else:
15-
__builtins__['debug'] = debug
17+
setattr(builtins, 'debug', debug)
1618
"""
1719

1820

@@ -24,7 +26,7 @@ def print_code() -> int:
2426
def install() -> int:
2527
print('[WARNING: this command is experimental, report issues at github.com/samuelcolvin/python-devtools]\n')
2628

27-
if 'debug' in __builtins__.__dict__:
29+
if hasattr(builtins, 'debug'):
2830
print('Looks like devtools is already installed.')
2931
return 0
3032

docs/usage.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ which is always imported.
114114

115115
```py
116116
# add devtools `debug` function to builtins
117+
import builtins
117118
try:
118119
from devtools import debug
119120
except ImportError:
120121
pass
121122
else:
122-
__builtins__['debug'] = debug
123+
setattr(builtins, 'debug', debug)
123124
```
124125

125126
The `ImportError` exception is important since you'll want python to run fine even if *devtools* isn't installed.

0 commit comments

Comments
 (0)