##// END OF EJS Templates
Added tests and comments for: https://bugs.launchpad.net/bugs/266993...
Brian Granger -
Show More
@@ -679,21 +679,22 b' class Interpreter(object):'
679 # to exec will fail however. There seems to be some inconsistency in
679 # to exec will fail however. There seems to be some inconsistency in
680 # how trailing whitespace is handled, but this seems to work.
680 # how trailing whitespace is handled, but this seems to work.
681 python = python.strip()
681 python = python.strip()
682
682
683 # The compiler module does not like unicode. We need to convert
683 # The compiler module does not like unicode. We need to convert
684 # it encode it:
684 # it encode it:
685 if isinstance(python, unicode):
685 if isinstance(python, unicode):
686 # Use the utf-8-sig BOM so the compiler detects this a UTF-8
686 # Use the utf-8-sig BOM so the compiler detects this a UTF-8
687 # encode string.
687 # encode string.
688 python = '\xef\xbb\xbf' + python.encode('utf-8')
688 python = '\xef\xbb\xbf' + python.encode('utf-8')
689
689
690 # The compiler module will parse the code into an abstract syntax tree.
690 # The compiler module will parse the code into an abstract syntax tree.
691 # This has a bug with str("a\nb"), but not str("""a\nb""")!!!
691 ast = compiler.parse(python)
692 ast = compiler.parse(python)
692
693
693 # Uncomment to help debug the ast tree
694 # Uncomment to help debug the ast tree
694 # for n in ast.node:
695 # for n in ast.node:
695 # print n.lineno,'->',n
696 # print n.lineno,'->',n
696
697
697 # Each separate command is available by iterating over ast.node. The
698 # Each separate command is available by iterating over ast.node. The
698 # lineno attribute is the line number (1-indexed) beginning the commands
699 # lineno attribute is the line number (1-indexed) beginning the commands
699 # suite.
700 # suite.
@@ -703,20 +704,23 b' class Interpreter(object):'
703 # We might eventually discover other cases where lineno is None and have
704 # We might eventually discover other cases where lineno is None and have
704 # to put in a more sophisticated test.
705 # to put in a more sophisticated test.
705 linenos = [x.lineno-1 for x in ast.node if x.lineno is not None]
706 linenos = [x.lineno-1 for x in ast.node if x.lineno is not None]
706
707
707 # When we finally get the slices, we will need to slice all the way to
708 # When we finally get the slices, we will need to slice all the way to
708 # the end even though we don't have a line number for it. Fortunately,
709 # the end even though we don't have a line number for it. Fortunately,
709 # None does the job nicely.
710 # None does the job nicely.
710 linenos.append(None)
711 linenos.append(None)
712
713 # This is causing problems with commands that have a \n embedded in
714 # a string, such as str("""a\nb""")
711 lines = python.splitlines()
715 lines = python.splitlines()
712
716
713 # Create a list of atomic commands.
717 # Create a list of atomic commands.
714 cmds = []
718 cmds = []
715 for i, j in zip(linenos[:-1], linenos[1:]):
719 for i, j in zip(linenos[:-1], linenos[1:]):
716 cmd = lines[i:j]
720 cmd = lines[i:j]
717 if cmd:
721 if cmd:
718 cmds.append('\n'.join(cmd)+'\n')
722 cmds.append('\n'.join(cmd)+'\n')
719
723
720 return cmds
724 return cmds
721
725
722 def error(self, text):
726 def error(self, text):
@@ -15,12 +15,22 b' __docformat__ = "restructuredtext en"'
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 import unittest
18 from IPython.kernel.core.interpreter import Interpreter
19 from IPython.kernel.core.interpreter import Interpreter
19
20
20 def test_unicode():
21 class TestInterpreter(unittest.TestCase):
21 """ Test unicode handling with the interpreter.
22
22 """
23 def test_unicode(self):
23 i = Interpreter()
24 """ Test unicode handling with the interpreter.
24 i.execute_python(u'print "ù"')
25 """
25 i.execute_python('print "ù"')
26 i = Interpreter()
27 i.execute_python(u'print "ù"')
28 i.execute_python('print "ù"')
29
30 def test_ticket266993_1(self):
31 i = Interpreter()
32 i.execute('str("""a\nb""")')
26
33
34 def test_ticket266993_2(self):
35 i = Interpreter()
36 i.execute('str("a\nb")') No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now