Skip to content

Commit df29ae1

Browse files
correct linenums for external files
1 parent dfb44cf commit df29ae1

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

bpython/repl.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,23 @@ def reset_running_time(self):
104104
self.running_time = 0
105105

106106
def runsource(self, source, filename=None, symbol='single',
107-
encode=True):
107+
encode='auto'):
108108
"""Execute Python code.
109109
110110
source, filename and symbol are passed on to
111-
code.InteractiveInterpreter.runsource. If encode is True, the source
112-
will be encoded. On Python 3.X, encode will be ignored.
111+
code.InteractiveInterpreter.runsource. If encode is True,
112+
an encoding comment will be added to the source.
113+
On Python 3.X, encode will be ignored.
113114
114-
encode doesn't encode the source, it just adds an encoding comment
115-
that specifies the encoding of the source.
116115
encode should only be used for interactive interpreter input,
117-
files should always have an encoding comment or be ASCII.
116+
files should always already have an encoding comment or be ASCII.
117+
By default an encoding line will be added if no filename is given.
118118
119119
In Python 3, source must be a unicode string
120120
In Python 2, source may be latin-1 bytestring or unicode string,
121121
following the interface of code.InteractiveInterpreter"""
122+
if encode == 'auto':
123+
encode = filename is None
122124
if encode and not py3:
123125
if isinstance(source, str):
124126
# encoding only makes sense for bytestrings

bpython/test/test_args.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def test_exec_dunder_file(self):
3636

3737
self.assertEquals(stderr.strip(), f.name)
3838

39-
4039
def test_exec_nonascii_file(self):
4140
with tempfile.NamedTemporaryFile(mode="w") as f:
4241
f.write(dedent('''\
@@ -52,6 +51,23 @@ def test_exec_nonascii_file(self):
5251
except subprocess.CalledProcessError:
5352
self.fail('Error running module with nonascii characters')
5453

54+
def test_exec_nonascii_file_linenums(self):
55+
with tempfile.NamedTemporaryFile(mode="w") as f:
56+
f.write(dedent("""\
57+
#!/usr/bin/env python2
58+
# coding: utf-8
59+
1/0
60+
"""))
61+
f.flush()
62+
p = subprocess.Popen(
63+
[sys.executable, "-m", "bpython.curtsies",
64+
f.name],
65+
stderr=subprocess.PIPE,
66+
universal_newlines=True)
67+
(_, stderr) = p.communicate()
68+
69+
self.assertIn('line 3', stderr)
70+
5571

5672
class TestParse(TestCase):
5773

0 commit comments

Comments
 (0)