Skip to content

Commit 6a0264f

Browse files
automatically add import statements to generated code
--HG-- branch : scroll-frontend
1 parent 3c9c4ae commit 6a0264f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

bpython/scrollfrontend/repl.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,24 @@ def get_interpreter(config, locals_=None):
109109
if config.scroll_auto_import:
110110

111111
locals_ = locals_ if locals_ is not None else {}
112+
autoimported = []
112113

113114
class AutoImporter(dict):
114115
def __getitem__(self, item):
115116
if item in locals_:
116117
return locals_[item]
117118
else:
118119
try:
119-
return __import__(item)
120+
module = __import__(item)
121+
autoimported.append(item)
122+
return module
120123
except ImportError:
121124
raise KeyError(item)
122125
def __setitem__(self, item, value):
123126
locals_[item] = value
127+
@property
128+
def autoimported(self):
129+
return autoimported
124130

125131
ns = AutoImporter()
126132
return code.InteractiveInterpreter(locals=ns)
@@ -843,8 +849,9 @@ def reevaluate(self, insert_into_history=False):
843849

844850
def getstdout(self):
845851
lines = self.lines_for_display + [self.current_line_formatted]
846-
s = '\n'.join([x.s if isinstance(x, FmtStr) else x
847-
for x in lines]) if lines else ''
852+
s = '\n'.join(['%simport %s' % (self.ps1, name) for name in self.interp.locals.autoimported] if self.config.scroll_auto_import else [] +
853+
[x.s if isinstance(x, FmtStr) else x for x in lines]
854+
) if lines else ''
848855
return s
849856
def send_to_external_editor(self, filename=None):
850857
editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'vim'))

0 commit comments

Comments
 (0)