Skip to content

Commit b6a2a5f

Browse files
test behavior requiring encoding in runsource
1 parent 8bdb42e commit b6a2a5f

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

bpython/test/test_interpreter.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
except ImportError:
66
import unittest
77

8-
from bpython.curtsiesfrontend import interpreter
98
from curtsies.fmtfuncs import bold, green, magenta, cyan, red, plain
109

10+
from bpython.curtsiesfrontend import interpreter
11+
from bpython._py3compat import py3
12+
from bpython.test import mock
13+
1114

1215
class TestInterpreter(unittest.TestCase):
1316
def test_syntaxerror(self):
@@ -51,3 +54,42 @@ def g():
5154

5255
self.assertEquals(str(plain('').join(a)), str(expected))
5356
self.assertEquals(plain('').join(a), expected)
57+
58+
@unittest.skipIf(py3, "runsource() accepts only unicode in Python 3")
59+
def test_runsource_bytes(self):
60+
i = interpreter.Interp()
61+
i.encoding = 'latin-1'
62+
63+
i.runsource(b"a = b'\xfe'")
64+
self.assertIsInstance(i.locals['a'], str)
65+
self.assertEqual(i.locals['a'], b"\xfe")
66+
67+
i.runsource(b"b = u'\xfe'")
68+
self.assertIsInstance(i.locals['b'], unicode)
69+
self.assertEqual(i.locals['b'], u"\xfe")
70+
71+
@unittest.skipUnless(py3, "Only a syntax error in Python 3")
72+
@mock.patch.object(interpreter.Interp, 'showsyntaxerror')
73+
def test_runsource_bytes_over_128_syntax_error(self):
74+
i = interpreter.Interp()
75+
i.encoding = 'latin-1'
76+
77+
i.runsource(u"a = b'\xfe'")
78+
i.showsyntaxerror.assert_called_with()
79+
80+
@unittest.skipIf(py3, "only ASCII allowed in bytestrings in Python 3")
81+
def test_runsource_bytes_over_128_syntax_error(self):
82+
i = interpreter.Interp()
83+
i.encoding = 'latin-1'
84+
85+
i.runsource(u"a = b'\xfe'")
86+
self.assertIsInstance(i.locals['a'], type(b''))
87+
self.assertEqual(i.locals['a'], b"\xfe")
88+
89+
def test_runsource_unicode(self):
90+
i = interpreter.Interp()
91+
i.encoding = 'latin-1'
92+
93+
i.runsource(u"a = u'\xfe'")
94+
self.assertIsInstance(i.locals['a'], type(u''))
95+
self.assertEqual(i.locals['a'], u"\xfe")

0 commit comments

Comments
 (0)