Skip to content

Commit 7bcace4

Browse files
committed
merge in PYTHONSTARTUP-encoding-fix
2 parents acd887c + b9968b3 commit 7bcace4

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

bpython/args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
from __future__ import with_statement
55

6+
from __future__ import with_statement
67
import os
78
import sys
89
import code

bpython/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,8 @@ def main_curses(scr, args, config, interactive=True, locals_=None):
15451545
sys.stdout = repl
15461546
sys.stderr = repl
15471547

1548+
repl.startup()
1549+
15481550
if args:
15491551
bpython.args.exec_code(interpreter, args)
15501552
if not interactive:

bpython/gtk_.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ def main(args=None):
636636
sys.stderr = repl_widget
637637
sys.stdout = repl_widget
638638

639+
# repl.startup()
640+
639641
gobject.idle_add(init_import_completion)
640642

641643
if not options.socket_id:

bpython/repl.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ def __init__(self, locals=None, encoding=sys.getdefaultencoding()):
6767

6868
if not py3:
6969

70-
def runsource(self, source, filename='<input>', symbol='single'):
71-
source = '# coding: %s\n%s' % (self.encoding,
72-
source.encode(self.encoding))
70+
def runsource(self, source, filename='<input>', symbol='single',
71+
encode=True):
72+
if encode:
73+
source = '# coding: %s\n%s' % (self.encoding,
74+
source.encode(self.encoding))
7375
return code.InteractiveInterpreter.runsource(self, source,
7476
filename, symbol)
7577

@@ -111,10 +113,10 @@ def showtraceback(self):
111113
tblist = traceback.extract_tb(tb)
112114
del tblist[:1]
113115
# Set the right lineno (encoding header adds an extra line)
114-
lineno = tblist[0][1]
115116
if not py3:
116-
lineno -= 1
117-
tblist[0] = (tblist[0][0], lineno) + tblist[0][2:]
117+
for i, (filename, lineno, module, something) in enumerate(tblist):
118+
if filename == '<input>':
119+
tblist[i] = (filename, lineno - 1, module, something)
118120

119121
l = traceback.format_list(tblist)
120122
if l:
@@ -300,13 +302,15 @@ def __init__(self, interp, config, idle=None):
300302
if os.path.exists(pythonhist):
301303
self.rl_history.load(pythonhist, getpreferredencoding())
302304

303-
# This was a feature request to have the PYTHONSTARTUP
304-
# file executed on startup - I personally don't use this
305-
# feature so please notify me of any breakage.
305+
def startup(self):
306+
"""
307+
Execute PYTHONSTARTUP file if it exits. Call this after front
308+
end-specific initialisation.
309+
"""
306310
filename = os.environ.get('PYTHONSTARTUP')
307311
if filename and os.path.isfile(filename):
308312
with open(filename, 'r') as f:
309-
self.interp.runsource(f.read(), filename, 'exec')
313+
self.interp.runsource(f.read(), filename, 'exec', encode=False)
310314

311315
def attr_matches(self, text):
312316
"""Taken from rlcompleter.py and bent to my will."""

0 commit comments

Comments
 (0)