From: <pj...@us...> - 2009-04-07 05:49:03
|
Revision: 6185 http://jython.svn.sourceforge.net/jython/?rev=6185&view=rev Author: pjenvey Date: 2009-04-07 05:48:59 +0000 (Tue, 07 Apr 2009) Log Message: ----------- parser string input shouldn't go through universal newlines mode Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/Lib/test/test_codeop_jy.py trunk/jython/src/org/python/core/ParserFacade.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2009-04-07 02:44:38 UTC (rev 6184) +++ trunk/jython/Lib/test/test_builtin_jy.py 2009-04-07 05:48:59 UTC (rev 6185) @@ -141,23 +141,29 @@ def test_parse_str_eval(self): foo = 'föö' - for code in ("'%s'" % foo.decode('utf-8'), - "# coding: utf-8\n'%s'" % foo, - "%s'%s'" % (BOM_UTF8, foo)): - mod = compile(code, 'foo.py', 'eval') - bar = eval(mod) - self.assertEqual(foo, bar) - bar = eval(code) - self.assertEqual(foo, bar) + for code, expected in ( + ("'%s'" % foo.decode('utf-8'), foo), + ("# coding: utf-8\n'%s'" % foo, foo), + ("%s'%s'" % (BOM_UTF8, foo), foo), + ("'\rfoo\r'", '\rfoo\r') + ): + mod = compile(code, 'test.py', 'eval') + result = eval(mod) + self.assertEqual(result, expected) + result = eval(code) + self.assertEqual(result, expected) def test_parse_str_exec(self): foo = 'föö' - for code in ("a = '%s'" % foo.decode('utf-8'), - "# coding: utf-8\na = '%s'" % foo, - "%sa = '%s'" % (BOM_UTF8, foo)): + for code, expected in ( + ("bar = '%s'" % foo.decode('utf-8'), foo), + ("# coding: utf-8\nbar = '%s'" % foo, foo), + ("%sbar = '%s'" % (BOM_UTF8, foo), foo), + ("bar = '\rfoo\r'", '\rfoo\r') + ): ns = {} exec code in ns - self.assertEqual(foo, ns['a']) + self.assertEqual(ns['bar'], expected) def test_general_eval(self): # Tests that general mappings can be used for the locals argument Modified: trunk/jython/Lib/test/test_codeop_jy.py =================================================================== --- trunk/jython/Lib/test/test_codeop_jy.py 2009-04-07 02:44:38 UTC (rev 6184) +++ trunk/jython/Lib/test/test_codeop_jy.py 2009-04-07 05:48:59 UTC (rev 6185) @@ -1,6 +1,7 @@ """ test compile. derived from test_codeop """ +import codeop import unittest from test import test_support from test.test_support import run_unittest @@ -183,8 +184,17 @@ compile("a = 1\n", "def", 'single').co_filename) +class CodeopTests(unittest.TestCase): + + def test_no_universal_newlines(self): + # previously \r was translated due to universal newlines + code = codeop.compile_command("'\rfoo\r'", symbol='eval') + self.assertEqual(eval(code), '\rfoo\r') + + def test_main(): - run_unittest(CompileTests) + run_unittest(CompileTests, + CodeopTests) if __name__ == "__main__": Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2009-04-07 02:44:38 UTC (rev 6184) +++ trunk/jython/src/org/python/core/ParserFacade.java 2009-04-07 05:48:59 UTC (rev 6185) @@ -220,6 +220,15 @@ CompilerFlags cflags, String filename, boolean fromString) + throws IOException { + return prepBufReader(input, cflags, filename, fromString, true); + } + + private static ExpectedEncodingBufferedReader prepBufReader(InputStream input, + CompilerFlags cflags, + String filename, + boolean fromString, + boolean universalNewlines) throws IOException { input = new BufferedInputStream(input); boolean bom = adjustForBOM(input); @@ -240,12 +249,14 @@ } cflags.encoding = encoding; - // Enable universal newlines mode on the input - StreamIO rawIO = new StreamIO(input, true); - org.python.core.io.BufferedReader bufferedIO = - new org.python.core.io.BufferedReader(rawIO, 0); - UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); - input = new TextIOInputStream(textIO); + if (universalNewlines) { + // Enable universal newlines mode on the input + StreamIO rawIO = new StreamIO(input, true); + org.python.core.io.BufferedReader bufferedIO = + new org.python.core.io.BufferedReader(rawIO, 0); + UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO); + input = new TextIOInputStream(textIO); + } Charset cs; try { @@ -270,7 +281,8 @@ private static ExpectedEncodingBufferedReader prepBufReader(String string, CompilerFlags cflags, - String filename) throws IOException { + String filename) + throws IOException { byte[] stringBytes; if (cflags.source_is_utf8) { // Passed unicode, re-encode the String to raw bytes @@ -285,7 +297,7 @@ } else { stringBytes = StringUtil.toBytes(string); } - return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true); + return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true, false); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |