Skip to content

Commit 6d8952c

Browse files
committed
Unify runsource and document its behavior
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 60c4c08 commit 6d8952c

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

bpython/repl.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,19 @@ def __init__(self, locals=None, encoding=None):
9696
def reset_running_time(self):
9797
self.running_time = 0
9898

99-
if py3:
100-
101-
def runsource(self, source, filename="<input>", symbol="single"):
102-
with self.timer:
103-
return code.InteractiveInterpreter.runsource(self, source,
104-
filename, symbol)
105-
106-
else:
107-
108-
def runsource(self, source, filename='<input>', symbol='single',
109-
encode=True):
110-
with self.timer:
111-
if encode:
112-
source = u'# coding: %s\n%s' % (self.encoding, source)
113-
source = source.encode(self.encoding)
114-
return code.InteractiveInterpreter.runsource(self, source,
115-
filename, symbol)
99+
def runsource(self, source, filename='<input>', symbol='single',
100+
encode=True):
101+
"""Execute Python code.
102+
103+
source, filename and symbol are passed on to
104+
code.InteractiveInterpreter.runsource. If encode is True, the source
105+
will be encoded. On Python 3.X, encode will be ignored."""
106+
if not py3 and encode:
107+
source = u'# coding: %s\n%s' % (self.encoding, source)
108+
source = source.encode(self.encoding)
109+
with self.timer:
110+
return code.InteractiveInterpreter.runsource(self, source,
111+
filename, symbol)
116112

117113
def showsyntaxerror(self, filename=None):
118114
"""Override the regular handler, the code's copied and pasted from
@@ -409,7 +405,11 @@ def startup(self):
409405
if filename:
410406
encoding = inspection.get_encoding_file(filename)
411407
with io.open(filename, 'rt', encoding=encoding) as f:
412-
self.interp.runsource(f.read(), filename, 'exec')
408+
source = f.read()
409+
if not py3:
410+
# Python 2.6 and early 2.7.X need bytes.
411+
source = source.encode(encoding)
412+
self.interp.runsource(source, filename, 'exec', encode=False)
413413

414414
def current_string(self, concatenate=False):
415415
"""If the line ends in a string get it, otherwise return ''"""

0 commit comments

Comments
 (0)