Skip to content

Commit eabdc9a

Browse files
make embed useful
1 parent 7f7aee1 commit eabdc9a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

bpython/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# THE SOFTWARE.
2222

2323
import os.path
24+
import sys
2425

2526
try:
2627
from bpython._version import __version__ as version
@@ -31,6 +32,21 @@
3132
package_dir = os.path.abspath(os.path.dirname(__file__))
3233

3334

34-
def embed(locals_=None, args=['-i', '-q'], banner=None):
35+
def embed(locals_=None, args=['-i', '-q'], banner=None, globals_=None):
36+
"""Run bpython in something like the surrounding environment
37+
38+
locals_ and globals_ are copied and merged to create a new
39+
locals_ dict so everything in scope at the call site will be
40+
in scope in the embedded bpython session.
41+
"""
3542
from bpython.curtsies import main
36-
return main(args, locals_, banner)
43+
if locals_ is None:
44+
f = sys._getframe(1)
45+
locals_ = f.f_locals
46+
if globals_ is None:
47+
f = sys._getframe(1)
48+
49+
globals_ = f.f_globals.copy()
50+
globals_.update(locals_)
51+
52+
return main(args, globals_, banner)

0 commit comments

Comments
 (0)