Skip to content

Commit cf5f8d7

Browse files
committed
Run PYTHONSTARTUP using events
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 7474450 commit cf5f8d7

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

bpython/curtsies.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ def process_event(e):
134134
scrolled = window.render_to_terminal(array, cursor_pos)
135135
repl.scroll_offset += scrolled
136136

137+
# run startup file
138+
if interactive:
139+
process_event(bpythonevents.RunStartupFileEvent())
140+
141+
# handle paste
137142
if paste:
138143
process_event(paste)
139144

bpython/curtsiesfrontend/repl.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ def smarter_request_reload(files_modified=()):
292292
logger.debug("starting parent init")
293293
super(Repl, self).__init__(interp, config)
294294
#TODO bring together all interactive stuff - including current directory in path?
295-
if interactive:
296-
self.startup()
297295
self.formatter = BPythonFormatter(config.color_scheme)
298296
self.interact = self.status_bar # overwriting what bpython.Repl put there
299297
# interact is called to interact with the status bar,
@@ -401,22 +399,19 @@ def sigtstp_handler(self, signum, frame):
401399
self.after_suspend()
402400
self.__enter__()
403401

404-
def startup(self):
402+
def run_startup(self):
405403
"""
406404
Execute PYTHONSTARTUP file if it exits. Call this after front
407405
end-specific initialisation.
408406
"""
409407
filename = os.environ.get('PYTHONSTARTUP')
410408
if filename:
411-
if os.path.isfile(filename):
412-
with open(filename, 'r') as f:
413-
if py3:
414-
#TODO runsource has a new signature in PY3
415-
self.interp.runsource(f.read(), filename, 'exec')
416-
else:
417-
self.interp.runsource(f.read(), filename, 'exec')
418-
else:
419-
raise IOError("Python startup file (PYTHONSTARTUP) not found at %s" % filename)
409+
with open(filename, 'r') as f:
410+
if py3:
411+
#TODO runsource has a new signature in PY3
412+
self.interp.runsource(f.read(), filename, 'exec')
413+
else:
414+
self.interp.runsource(f.read(), filename, 'exec')
420415

421416
def clean_up_current_line_for_exit(self):
422417
"""Called when trying to exit to prep for final paint"""
@@ -468,6 +463,12 @@ def process_control_event(self, e):
468463
else:
469464
self.process_simple_keypress(ee)
470465

466+
elif isinstance(e, bpythonevents.RunStartupFileEvent):
467+
try:
468+
self.run_startup()
469+
except IOError as e:
470+
self.status_bar.message(_('Executing PYTHONSTARTUP failed: %s') % (str(e)))
471+
471472
elif self.stdin.has_focus:
472473
return self.stdin.process_event(e)
473474

0 commit comments

Comments
 (0)