Skip to content

Commit 9bc21e5

Browse files
specialize source finding error messages
1 parent 4408f9d commit 9bc21e5

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

bpython/test/test_interpreter.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import unicode_literals
44

5+
import sys
6+
57
try:
68
import unittest2 as unittest
79
except ImportError:
@@ -13,6 +15,8 @@
1315
from bpython._py3compat import py3
1416
from bpython.test import mock
1517

18+
pypy = 'PyPy' in sys.version
19+
1620

1721
class TestInterpreter(unittest.TestCase):
1822
def test_syntaxerror(self):
@@ -25,9 +29,14 @@ def append_to_a(message):
2529
i.write = append_to_a
2630
i.runsource('1.1.1.1')
2731

28-
expected = ' File ' + green('"<input>"') + ', line ' + \
29-
bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + \
30-
bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n'
32+
if pypy:
33+
expected = ' File ' + green('"<input>"') + ', line ' + \
34+
bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + \
35+
bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n'
36+
else:
37+
expected = ' File ' + green('"<input>"') + ', line ' + \
38+
bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + \
39+
bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n'
3140

3241
self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
3342
self.assertEquals(plain('').join(a), expected)
@@ -49,10 +58,15 @@ def g():
4958

5059
i.runsource('g()')
5160

61+
if pypy:
62+
global_not_found = "global name 'g' is not defined"
63+
else:
64+
global_not_found = "name 'g' is not defined"
65+
5266
expected = 'Traceback (most recent call last):\n File ' + \
5367
green('"<input>"') + ', line ' + bold(magenta('1')) + ', in ' + \
5468
cyan('<module>') + '\n' + bold(red('NameError')) + ': ' + \
55-
cyan("name 'g' is not defined") + '\n'
69+
cyan(global_not_found) + '\n'
5670

5771
self.assertMultiLineEqual(str(plain('').join(a)), str(expected))
5872
self.assertEquals(plain('').join(a), expected)

bpython/test/test_repl.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import collections
21
from itertools import islice
2+
import collections
3+
import inspect
34
import os
45
import shutil
56
import socket
67
import tempfile
78
from six.moves import range
9+
import sys
810

911
try:
1012
import unittest2 as unittest
@@ -15,6 +17,7 @@
1517
from bpython import config, repl, cli, autocomplete
1618
from bpython.test import MagicIterMock, mock, FixLanguageTestCase as TestCase
1719

20+
pypy = 'PyPy' in sys.version
1821

1922
def setup_config(conf):
2023
config_struct = config.Struct()
@@ -230,21 +233,25 @@ def assert_get_source_error_for_current_function(self, func, msg):
230233

231234
def test_current_function(self):
232235
self.set_input_line('INPUTLINE')
233-
self.repl.current_func = collections.MutableSet.add
234-
self.assertIn("Add an element.",
236+
self.repl.current_func = inspect.getsource
237+
self.assertIn("text of the source code",
235238
self.repl.get_source_of_current_name())
236239

237240
self.assert_get_source_error_for_current_function(
238-
collections.defaultdict.copy, "No source code found for INPUTLINE")
241+
[], "No source code found for INPUTLINE")
239242

240243
self.assert_get_source_error_for_current_function(
241-
collections.defaultdict, "could not find class definition")
244+
list.pop, "No source code found for INPUTLINE")
242245

246+
@unittest.skipIf(pypy, 'different errors for PyPy')
247+
def test_current_function_cpython(self):
248+
self.set_input_line('INPUTLINE')
243249
self.assert_get_source_error_for_current_function(
244-
[], "No source code found for INPUTLINE")
245-
250+
collections.defaultdict.copy, "No source code found for INPUTLINE")
246251
self.assert_get_source_error_for_current_function(
247-
list.pop, "No source code found for INPUTLINE")
252+
collections.defaultdict, "could not find class definition")
253+
254+
248255

249256
def test_current_line(self):
250257
self.repl.interp.locals['a'] = socket.socket

0 commit comments

Comments
 (0)