[pypy-svn] r45073 - in pypy/dist/pypy/interpreter: . pyparser test
justas at codespeak.net
justas at codespeak.net
Sat Jul 14 14:43:58 CEST 2007
Author: justas
Date: Sat Jul 14 14:43:58 2007
New Revision: 45073
Modified:
pypy/dist/pypy/interpreter/baseobjspace.py
pypy/dist/pypy/interpreter/pycompiler.py
pypy/dist/pypy/interpreter/pyparser/astbuilder.py
pypy/dist/pypy/interpreter/pyparser/pythonparse.py
pypy/dist/pypy/interpreter/test/test_compiler.py
Log:
(Arlo, Justas)
A test that compiling a file with import from __future__ does not affect compilation of other files
Yes, more whitespace removed (blame Justas)
Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py (original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py Sat Jul 14 14:43:58 2007
@@ -38,7 +38,7 @@
space.set_str_keyed_item(w_dict, w_attr, w_value, shadows_type)
return True
return False
-
+
def deldictvalue(self, space, w_name):
w_dict = self.getdict()
if w_dict is not None:
@@ -153,7 +153,7 @@
self.space.leave_cache_building_mode(val)
def ready(self, result):
pass
-
+
class UnpackValueError(ValueError):
def __init__(self, msg):
self.msg = msg
@@ -166,10 +166,10 @@
class ObjSpace(object):
"""Base class for the interpreter-level implementations of object spaces.
http://codespeak.net/pypy/dist/pypy/doc/objspace.html"""
-
+
full_exceptions = True # full support for exceptions (normalization & more)
- def __init__(self, config=None, **kw):
+ def __init__(self, config=None, **kw):
"NOT_RPYTHON: Basic initialization of objects."
self.fromcache = InternalSpaceCache(self).getorbuild
self.threadlocals = ThreadLocals()
@@ -189,7 +189,7 @@
self.pending_actions = []
self.setoptions(**kw)
-# if self.config.objspace.logbytecodes:
+# if self.config.objspace.logbytecodes:
# self.bytecodecounts = {}
self.initialize()
@@ -228,14 +228,14 @@
if self.config.objspace.std.logspaceoptypes:
for s in self.FrameClass._space_op_types:
print s
-
+
def reportbytecodecounts(self):
os.write(2, "Starting bytecode report.\n")
fd = os.open('bytecode.txt', os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0644)
for opcode, count in self.bytecodecounts.items():
os.write(fd, str(opcode) + ", " + str(count) + "\n")
os.close(fd)
- os.write(2, "Reporting done.\n")
+ os.write(2, "Reporting done.\n")
def __repr__(self):
try:
@@ -247,19 +247,19 @@
"""NOT_RPYTHON. load a lazy pypy/module and put it into sys.modules"""
import sys
- fullname = "pypy.module.%s" % importname
+ fullname = "pypy.module.%s" % importname
- Module = __import__(fullname,
+ Module = __import__(fullname,
None, None, ["Module"]).Module
if Module.applevel_name is not None:
name = Module.applevel_name
else:
name = importname
- w_name = self.wrap(name)
- w_mod = self.wrap(Module(self, w_name))
+ w_name = self.wrap(name)
+ w_mod = self.wrap(Module(self, w_name))
w_modules = self.sys.get('modules')
- self.setitem(w_modules, w_name, w_mod)
+ self.setitem(w_modules, w_name, w_mod)
return name
def getbuiltinmodule(self, name):
@@ -291,7 +291,7 @@
for modname in self.ALL_BUILTIN_MODULES:
if not (os.path.exists(
os.path.join(os.path.dirname(pypy.__file__),
- 'lib', modname+'.py'))):
+ 'lib', modname+'.py'))):
modules.append('faked+'+modname)
self._builtinmodule_list = modules
@@ -308,25 +308,25 @@
def make_builtins(self):
"NOT_RPYTHON: only for initializing the space."
- from pypy.module.sys import Module
+ from pypy.module.sys import Module
w_name = self.wrap('sys')
- self.sys = Module(self, w_name)
+ self.sys = Module(self, w_name)
w_modules = self.sys.get('modules')
self.setitem(w_modules, w_name, self.wrap(self.sys))
- from pypy.module.__builtin__ import Module
+ from pypy.module.__builtin__ import Module
w_name = self.wrap('__builtin__')
- self.builtin = Module(self, w_name)
+ self.builtin = Module(self, w_name)
w_builtin = self.wrap(self.builtin)
- self.setitem(w_modules, w_name, w_builtin)
- self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin)
+ self.setitem(w_modules, w_name, w_builtin)
+ self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin)
bootstrap_modules = ['sys', '__builtin__', 'exceptions']
installed_builtin_modules = bootstrap_modules[:]
# initialize with "bootstrap types" from objspace (e.g. w_None)
for name, value in self.__dict__.items():
- if name.startswith('w_') and not name.endswith('Type'):
+ if name.startswith('w_') and not name.endswith('Type'):
name = name[2:]
#print "setitem: space instance %-20s into builtins" % name
self.setitem(self.builtin.w_dict, self.wrap(name), value)
@@ -350,7 +350,7 @@
w_builtin_module_names)
def install_mixedmodule(self, mixedname, installed_builtin_modules):
- """NOT_RPYTHON"""
+ """NOT_RPYTHON"""
modname = self.setbuiltinmodule(mixedname)
if modname:
assert modname not in installed_builtin_modules, (
@@ -407,7 +407,7 @@
# for reasons related to the specialization of the framestack attribute
# so we make sure there is no executioncontext at freeze-time
self.threadlocals.setvalue(None)
- return True
+ return True
def createexecutioncontext(self):
"Factory function for execution contexts."
@@ -461,7 +461,7 @@
def set_str_keyed_item(self, w_obj, w_key, w_value, shadows_type=True):
return self.setitem(w_obj, w_key, w_value)
-
+
def finditem(self, w_obj, w_key):
try:
return self.getitem(w_obj, w_key)
@@ -529,7 +529,7 @@
raise DescrMismatch()
return obj
descr_self_interp_w._annspecialcase_ = 'specialize:arg(1)'
-
+
def interp_w(self, RequiredClass, w_obj, can_be_None=False):
"""
Unwrap w_obj, checking that it is an instance of the required internal
@@ -698,7 +698,7 @@
def recursive_issubclass(self, w_obj, w_cls):
if self.is_w(w_obj, w_cls):
return self.w_True
- for w_base in self.unpackiterable(self.getattr(w_obj,
+ for w_base in self.unpackiterable(self.getattr(w_obj,
self.wrap('__bases__'))):
if self.is_true(self.recursive_issubclass(w_base, w_cls)):
return self.w_True
@@ -763,7 +763,7 @@
self.setitem(w_globals, w_key, self.wrap(self.builtin))
return statement.exec_code(self, w_globals, w_locals)
- def appexec(self, posargs_w, source):
+ def appexec(self, posargs_w, source):
""" return value from executing given source at applevel.
EXPERIMENTAL. The source must look like
'''(x, y):
@@ -842,7 +842,7 @@
def build(cache, source):
""" NOT_RPYTHON """
space = cache.space
- # XXX will change once we have our own compiler
+ # XXX will change once we have our own compiler
import py
source = source.lstrip()
assert source.startswith('('), "incorrect header in:\n%s" % (source,)
Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py (original)
+++ pypy/dist/pypy/interpreter/pycompiler.py Sat Jul 14 14:43:58 2007
@@ -5,6 +5,8 @@
from codeop import PyCF_DONT_IMPLY_DEDENT
from pypy.interpreter.error import OperationError
+ENABLE_GRAMMAR_VERSION = "2.4"
+
class AbstractCompiler:
"""Abstract base class for a bytecode compiler."""
@@ -199,11 +201,12 @@
of incomplete inputs (e.g. we shouldn't re-compile from sracth
the whole source after having only added a new '\n')
"""
- def __init__(self, space):
+ def __init__(self, space, grammar_version=ENABLE_GRAMMAR_VERSION):
from pyparser.pythonparse import PYTHON_PARSER
PyCodeCompiler.__init__(self, space)
self.parser = PYTHON_PARSER
self.additional_rules = {}
+ self.grammar_version = grammar_version
def compile(self, source, filename, mode, flags):
@@ -220,7 +223,8 @@
flags |= stdlib___future__.generators.compiler_flag # always on (2.2 compat)
space = self.space
try:
- builder = AstBuilder(self.parser, space=space)
+ builder = AstBuilder(self.parser, space=space,
+ grammar_version=self.grammar_version)
for rulename, buildfunc in self.additional_rules.iteritems():
assert isinstance(buildfunc, Function)
builder.user_build_rules[rulename] = buildfunc
Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py Sat Jul 14 14:43:58 2007
@@ -11,7 +11,7 @@
#import pypy.interpreter.pyparser.pytoken as tok
from pypy.interpreter.pyparser.error import SyntaxError
from pypy.interpreter.pyparser.parsestring import parsestr
-from pypy.interpreter.pyparser.pythonparse import ENABLE_GRAMMAR_VERSION
+from pypy.interpreter.pycompiler import ENABLE_GRAMMAR_VERSION
from pypy.interpreter.gateway import interp2app
from asthelper import *
Modified: pypy/dist/pypy/interpreter/pyparser/pythonparse.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/pythonparse.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/pythonparse.py Sat Jul 14 14:43:58 2007
@@ -9,6 +9,7 @@
import os
from pypy.interpreter.error import OperationError, debug_print
from pypy.interpreter import gateway
+from pypy.interpreter.pycompiler import ENABLE_GRAMMAR_VERSION
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
@@ -28,8 +29,6 @@
from codeop import PyCF_DONT_IMPLY_DEDENT
-ENABLE_GRAMMAR_VERSION = "2.4"
-
## files encoding management ############################################
_recode_to_utf8 = gateway.applevel(r'''
Modified: pypy/dist/pypy/interpreter/test/test_compiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_compiler.py (original)
+++ pypy/dist/pypy/interpreter/test/test_compiler.py Sat Jul 14 14:43:58 2007
@@ -567,11 +567,30 @@
py.test.skip("syntax not supported by the CPython 2.4 compiler")
test_continue_in_nested_finally = skip_on_2_4
+
class TestPythonAstCompiler(BaseTestCompiler):
def setup_method(self, method):
- self.compiler = PythonAstCompiler(self.space)
+ self.compiler = PythonAstCompiler(self.space, grammar_version="2.4")
+
+class TestPythonAstCompiler_25_grammar:
+ def setup_method(self, method):
+ self.compiler = PythonAstCompiler(self.space, grammar_version="2.5a")
+ def test_from_future_import(self):
+ source = """from __future__ import with_statement
+with somtehing as stuff:
+ pass
+ """
+ code = self.compiler.compile(source, '<filename>', 'exec', 0)
+ assert isinstance(code, PyCode)
+ assert code.co_filename == '<filename>'
+
+ source2 = "with = 3"
+
+ code = self.compiler.compile(source, '<filename2>', 'exec', 0)
+ assert isinstance(code, PyCode)
+ assert code.co_filename == '<filename2>'
class AppTestOptimizer:
More information about the Pypy-commit
mailing list