1. Ranjith Kumar
  2. pypy

Commits

Amaury Forgeot d'Arc  committed 3b5978d

Translation fixes

  • Participants
  • Parent commits 7c3c751
  • Branches compile-from-stream

Comments (0)

Files changed (3)

File pypy/interpreter/pyparser/future.py

View file
     flags = 0
     pos = (-1, 0)
 
-    if not isinstance(tree, (ast.Module, ast.Interactive)):
+    if isinstance(tree, ast.Module):
+        stmts = tree.body
+    elif isinstance(tree, ast.Interactive):
+        stmts = tree.body
+    else:
         return flags, pos
 
-    if not tree.body:
+    if stmts is None:
         return flags, pos
 
     found_docstring = False
 
-    for elem in tree.body:
+    for elem in stmts:
         if isinstance(elem, ast.ImportFrom):
             if elem.module != '__future__':
                 break
             for alias in elem.names:
+                if not isinstance(alias, ast.alias):
+                    continue
                 name = alias.name
                 try:
                     flags |= future_flags.compiler_features[name]

File pypy/interpreter/pyparser/pyparse.py

View file
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.pyparser import parser, pytokenizer, pygram, error
 from pypy.interpreter.astcompiler import consts
+from pypy.interpreter.unicodehelper import PyUnicode_EncodeUTF8
+
 
 
 _recode_to_utf8 = gateway.applevel(r'''
             return self.stream.readline()
         else:
             w_line = self.space.call_function(self.w_readline)
-            return self.space.unicode_w(w_line).encode('utf-8')
+            return PyUnicode_EncodeUTF8(self.space,
+                                        self.space.unicode_w(w_line))
 
     def set_encoding(self, encoding):
         self.encoding = encoding
 
     def close(self):
         if self.w_file:
-            self.w_file.detach()
+            from pypy.module._file import interp_file
+            interp_file.detach_stream(self.space, self.w_file)
 
 class PythonParser(parser.Parser):
 
-    def __init__(self, space, grammar=pygram.python_grammar):
-        parser.Parser.__init__(self, grammar)
+    IMPORT_FROM = pygram.python_grammar.symbol_ids['import_from']
+
+    def __init__(self, space):
+        parser.Parser.__init__(self, pygram.python_grammar)
         self.space = space
 
     def _detect_encoding(self, text, lineno, compile_info):
         return self.build_tree(source_lines, compile_info)
 
     def parse_future_import(self, node):
-        if node.type != self.grammar.symbol_ids['import_from']:
+        if node.type != self.IMPORT_FROM:
             return
         children = node.children
         # from __future__ import ..., must have at least 4 children
 
         for i in range(0, len(child.children), 2):
             c = child.children[i]
-            if (len(c.children) >= 1 and
-                c.children[0].type == pygram.tokens.NAME):
-                name = c.children[0].value
+            if (len(c.children) == 0 or
+                c.children[0].type != pygram.tokens.NAME):
+                continue
+
+            name = c.children[0].value
 
             if name == 'print_function':
                 self.compile_info.flags |= consts.CO_FUTURE_PRINT_FUNCTION

File pypy/module/_file/interp_file.py

View file
     file.fdopenstream(stream, fd, mode)
     return space.wrap(file)
 
+def detach_stream(space, w_file):
+    file = space.interp_w(W_File, w_file)
+    return file.detach()
+
 def descr_file_closed(space, file):
     return space.wrap(file.stream is None)