[pypy-svn] r61191 - in pypy/trunk/pypy/interpreter: pyparser test

antocuni at codespeak.net antocuni at codespeak.net
Wed Jan 21 15:34:01 CET 2009


Author: antocuni
Date: Wed Jan 21 15:33:58 2009
New Revision: 61191

Modified:
   pypy/trunk/pypy/interpreter/pyparser/pythonparse.py
   pypy/trunk/pypy/interpreter/test/test_syntax.py
Log:
raise SyntaxError in case the encoding declaration contains a bad codec



Modified: pypy/trunk/pypy/interpreter/pyparser/pythonparse.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/pythonparse.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/pythonparse.py	Wed Jan 21 15:33:58 2009
@@ -5,6 +5,7 @@
 using file_input, single_input and eval_input targets
 """
 from pypy.interpreter import gateway
+from pypy.interpreter.error import OperationError
 from pypy.interpreter.pyparser.error import SyntaxError
 from pypy.interpreter.pyparser.pythonlexer import Source, match_encoding_declaration
 from pypy.interpreter.astcompiler.consts import CO_FUTURE_WITH_STATEMENT
@@ -107,7 +108,16 @@
         else:
             enc = _normalize_encoding(_check_for_encoding(textsrc))
             if enc is not None and enc not in ('utf-8', 'iso-8859-1'):
-                textsrc = recode_to_utf8(builder.space, textsrc, enc)
+                try:
+                    textsrc = recode_to_utf8(builder.space, textsrc, enc)
+                except OperationError, e:
+                    # if the codec is not found, LookupError is raised.  we
+                    # check using 'is_w' not to mask potential IndexError or
+                    # KeyError
+                    space = builder.space
+                    if space.is_w(e.w_type, space.w_LookupError):
+                        raise SyntaxError("Unknown encoding: %s" % enc)
+                    raise                    
 
         lines = [line + '\n' for line in textsrc.split('\n')]
         builder.source_encoding = enc

Modified: pypy/trunk/pypy/interpreter/test/test_syntax.py
==============================================================================
--- pypy/trunk/pypy/interpreter/test/test_syntax.py	(original)
+++ pypy/trunk/pypy/interpreter/test/test_syntax.py	Wed Jan 21 15:33:58 2009
@@ -621,6 +621,13 @@
         else:
             raise Exception("no SyntaxError??")
 
+    def test_bad_encoding(self):
+        program = """
+# -*- coding: uft-8 -*-
+pass
+"""
+        raises(SyntaxError, "exec program")
+
 
 if __name__ == '__main__':
     # only to check on top of CPython (you need 2.4)



More information about the Pypy-commit mailing list