Author: Christian Tismer <tis...@stackless.com> Branch: win64-stage1 Changeset: r50280:da31cc0f6eee Date: 2011-12-07 18:28 +0100 http://bitbucket.org/pypy/pypy/changeset/da31cc0f6eee/
Log: merge default diff --git a/pypy/bin/checkmodule.py b/pypy/bin/checkmodule.py --- a/pypy/bin/checkmodule.py +++ b/pypy/bin/checkmodule.py @@ -1,43 +1,45 @@ #! /usr/bin/env python """ -Usage: checkmodule.py [-b backend] <module-name> +Usage: checkmodule.py <module-name> -Compiles the PyPy extension module from pypy/module/<module-name>/ -into a fake program which does nothing. Useful for testing whether a -modules compiles without doing a full translation. Default backend is cli. - -WARNING: this is still incomplete: there are chances that the -compilation fails with strange errors not due to the module. If a -module is known to compile during a translation but don't pass -checkmodule.py, please report the bug (or, better, correct it :-). +Check annotation and rtyping of the PyPy extension module from +pypy/module/<module-name>/. Useful for testing whether a +modules compiles without doing a full translation. """ import autopath -import sys +import sys, os from pypy.objspace.fake.checkmodule import checkmodule def main(argv): - try: - assert len(argv) in (2, 4) - if len(argv) == 2: - backend = 'cli' - modname = argv[1] - if modname in ('-h', '--help'): - print >> sys.stderr, __doc__ - sys.exit(0) - if modname.startswith('-'): - print >> sys.stderr, "Bad command line" - print >> sys.stderr, __doc__ - sys.exit(1) - else: - _, b, backend, modname = argv - assert b == '-b' - except AssertionError: + if len(argv) != 2: print >> sys.stderr, __doc__ sys.exit(2) + modname = argv[1] + if modname in ('-h', '--help'): + print >> sys.stderr, __doc__ + sys.exit(0) + if modname.startswith('-'): + print >> sys.stderr, "Bad command line" + print >> sys.stderr, __doc__ + sys.exit(1) + if os.path.sep in modname: + if os.path.basename(modname) == '': + modname = os.path.dirname(modname) + if os.path.basename(os.path.dirname(modname)) != 'module': + print >> sys.stderr, "Must give '../module/xxx', or just 'xxx'." + sys.exit(1) + modname = os.path.basename(modname) + try: + checkmodule(modname) + except Exception, e: + import traceback, pdb + traceback.print_exc() + pdb.post_mortem(sys.exc_info()[2]) + return 1 else: - checkmodule(modname, backend, interactive=True) - print 'Module compiled succesfully' + print 'Passed.' + return 0 if __name__ == '__main__': - main(sys.argv) + sys.exit(main(sys.argv)) diff --git a/pypy/interpreter/astcompiler/optimize.py b/pypy/interpreter/astcompiler/optimize.py --- a/pypy/interpreter/astcompiler/optimize.py +++ b/pypy/interpreter/astcompiler/optimize.py @@ -1,6 +1,5 @@ """codegen helpers and AST constant folding.""" import sys -import itertools from pypy.interpreter.astcompiler import ast, consts, misc from pypy.tool import stdlib_opcode as ops @@ -146,8 +145,7 @@ } unrolling_unary_folders = unrolling_iterable(unary_folders.items()) -for folder in itertools.chain(binary_folders.itervalues(), - unary_folders.itervalues()): +for folder in binary_folders.values() + unary_folders.values(): folder._always_inline_ = True del folder diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py --- a/pypy/interpreter/baseobjspace.py +++ b/pypy/interpreter/baseobjspace.py @@ -1,4 +1,3 @@ -import itertools import pypy from pypy.interpreter.executioncontext import ExecutionContext, ActionFlag from pypy.interpreter.executioncontext import UserDelAction, FrameTraceAction @@ -519,8 +518,8 @@ exception_types_w = self.export_builtin_exceptions() # initialize with "bootstrap types" from objspace (e.g. w_None) - types_w = itertools.chain(self.get_builtin_types().iteritems(), - exception_types_w.iteritems()) + types_w = (self.get_builtin_types().items() + + exception_types_w.items()) for name, w_type in types_w: self.setitem(self.builtin.w_dict, self.wrap(name), w_type) @@ -1608,6 +1607,8 @@ 'UnicodeError', 'ValueError', 'ZeroDivisionError', + 'UnicodeEncodeError', + 'UnicodeDecodeError', ] ## Irregular part of the interface: diff --git a/pypy/module/itertools/interp_itertools.py b/pypy/module/itertools/interp_itertools.py --- a/pypy/module/itertools/interp_itertools.py +++ b/pypy/module/itertools/interp_itertools.py @@ -439,9 +439,6 @@ self.w_it = self.space.iter(self.space.next(self.w_iterables)) def next_w(self): - if not self.w_iterables: - # already stopped - raise OperationError(self.space.w_StopIteration, self.space.w_None) if not self.w_it: self._advance() try: diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py --- a/pypy/module/micronumpy/interp_boxes.py +++ b/pypy/module/micronumpy/interp_boxes.py @@ -4,7 +4,6 @@ from pypy.interpreter.typedef import TypeDef from pypy.objspace.std.floattype import float_typedef from pypy.objspace.std.inttype import int_typedef -from pypy.objspace.std.typeobject import W_TypeObject from pypy.rlib.rarithmetic import LONG_BIT from pypy.tool.sourcetools import func_with_new_name @@ -33,9 +32,8 @@ _attrs_ = () def descr__new__(space, w_subtype, __args__): - assert isinstance(w_subtype, W_TypeObject) raise operationerrfmt(space.w_TypeError, "cannot create '%s' instances", - w_subtype.get_module_type_name() + w_subtype.getname(space, '?') ) def descr_str(self, space): @@ -266,4 +264,4 @@ __module__ = "numpypy", __new__ = interp2app(W_Float64Box.descr__new__.im_func), -) \ No newline at end of file +) diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py --- a/pypy/module/micronumpy/interp_dtype.py +++ b/pypy/module/micronumpy/interp_dtype.py @@ -18,7 +18,7 @@ VOID_STORAGE = lltype.Array(lltype.Char, hints={'nolength': True, 'render_as_void': True}) class W_Dtype(Wrappable): - _immuable_fields_ = ["itemtype", "num", "kind"] + _immutable_fields_ = ["itemtype", "num", "kind"] def __init__(self, itemtype, num, kind, name, char, w_box_type, alternate_constructors=[]): self.signature = signature.BaseSignature() diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -852,7 +852,7 @@ if len(args_w) == 1: w_shape = args_w[0] else: - w_shape = space.newlist(args_w) + w_shape = space.newtuple(args_w) concrete = self.get_concrete() new_shape = get_shape_from_iterable(space, concrete.find_size(), w_shape) @@ -916,6 +916,15 @@ def descr_debug_repr(self, space): return space.wrap(self.debug_repr()) + def descr_array_iface(self, space): + concrete = self.get_concrete() + storage = concrete.get_storage(space) + addr = rffi.cast(lltype.Signed, storage) + w_d = space.newdict() + space.setitem_str(w_d, 'data', space.newtuple([space.wrap(addr), + space.w_False])) + return w_d + def convert_to_array(space, w_obj): if isinstance(w_obj, BaseArray): return w_obj @@ -976,6 +985,9 @@ # so in order to have a consistent API, let it go through. pass + def get_storage(self, space): + raise OperationError(space.w_TypeError, space.wrap("Cannot get array interface on scalars in pypy")) + class VirtualArray(BaseArray): """ Class for representing virtual arrays, such as binary ops or ufuncs @@ -1263,6 +1275,9 @@ a_iter = a_iter.next(len(array.shape)) return array + def get_storage(self, space): + return self.parent.get_storage(space) + class W_NDimArray(BaseArray): """ A class representing contiguous array. We know that each iteration by say ufunc will increase the data index by one @@ -1325,6 +1340,9 @@ def debug_repr(self): return 'Array' + def get_storage(self, space): + return self.storage + def __del__(self): lltype.free(self.storage, flavor='raw', track_allocation=False) @@ -1444,6 +1462,7 @@ __repr__ = interp2app(BaseArray.descr_repr), __str__ = interp2app(BaseArray.descr_str), __debug_repr__ = interp2app(BaseArray.descr_debug_repr), + __array_interface__ = GetSetProperty(BaseArray.descr_array_iface), dtype = GetSetProperty(BaseArray.descr_get_dtype), shape = GetSetProperty(BaseArray.descr_get_shape, diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py --- a/pypy/module/micronumpy/test/test_dtypes.py +++ b/pypy/module/micronumpy/test/test_dtypes.py @@ -174,7 +174,7 @@ raises(TypeError, numpy.number, 0) raises(TypeError, numpy.integer, 0) exc = raises(TypeError, numpy.signedinteger, 0) - assert str(exc.value) == "cannot create 'numpypy.signedinteger' instances" + assert str(exc.value) == "cannot create 'signedinteger' instances" raises(TypeError, numpy.floating, 0) raises(TypeError, numpy.inexact, 0) diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py --- a/pypy/module/micronumpy/test/test_numarray.py +++ b/pypy/module/micronumpy/test/test_numarray.py @@ -1114,6 +1114,16 @@ b = a[0].copy() assert (b == zeros(10)).all() + def test_array_interface(self): + from numpypy import array + a = array([1, 2, 3]) + i = a.__array_interface__ + assert isinstance(i['data'][0], int) + a = a[::2] + i = a.__array_interface__ + assert isinstance(i['data'][0], int) + raises(TypeError, getattr, array(3), '__array_interface__') + class AppTestSupport(BaseNumpyAppTest): def setup_class(cls): import struct diff --git a/pypy/objspace/fake/__init__.py b/pypy/objspace/fake/__init__.py --- a/pypy/objspace/fake/__init__.py +++ b/pypy/objspace/fake/__init__.py @@ -1,2 +0,0 @@ -from objspace import FakeObjSpace -Space = FakeObjSpace diff --git a/pypy/objspace/fake/checkmodule.py b/pypy/objspace/fake/checkmodule.py --- a/pypy/objspace/fake/checkmodule.py +++ b/pypy/objspace/fake/checkmodule.py @@ -1,108 +1,12 @@ -import re -from copy import copy -from pypy.tool.error import debug -from pypy.interpreter.argument import Arguments -from pypy.interpreter.gateway import interp2app -from pypy.rlib.nonconst import NonConstant +from pypy.objspace.fake.objspace import FakeObjSpace, W_Root -def my_import(name): - mod = __import__(name) - components = name.split('.') - for comp in components[1:]: - mod = getattr(mod, comp) - return mod -def find_gateways(modname, basepath, module): - identifier = r'[a-zA-Z0-9][a-zA-Z0-9_]*' - r_simplename = re.compile(r'(%s)[.](%s)$' % (identifier, identifier)) - res = [] - for name in module.interpleveldefs.values(): - match = r_simplename.match(name) - if match: - submod_name, obj_name = match.groups() - submod_name = '%s.%s.%s' % (basepath, modname, submod_name) - submod = my_import(submod_name) - obj = getattr(submod, obj_name) - res += find_gw_in_obj(obj) - return res - -def find_gw_in_obj(obj): - if hasattr(obj, 'typedef'): - typedef = obj.typedef - return [gw for gw in typedef.rawdict.values() - if isinstance(gw, interp2app)] - elif hasattr(obj, 'func_code'): - return [interp2app(obj)] - else: - assert False - -## Since the fake objspace is more a hack than a real object space, it -## happens that the annotator complains about operations that cannot -## succeed because it knows too much about the objects involved. For -## example, if it knows that a list is always empty, it will block -## each operations that tries to access that list. This is not what we -## want, because we know that with real objectspaces that operations -## will succeed. - -## As a workaround, we insert dummy rpython code (the function -## dummy_rpython) that manipulates the variables in order to give -## them a more sensible annotation. This is the preferred way to solve -## the problems so far. - -## If the solution above doesn't work, the alternative is to -## substitute the interpreter code with something that doesn't hurt -## the annotator. It's a very ugly hack, better solutions are welcome -## :-) - - -# dummy rpython code to give some variables more sensible annotations -def dummy_rpython(dummy_function): - # to make the annotator flow-in without executing the code - if NonConstant(False): - dummy_function.defs_w = [None] # else the annotator would see an always empty list - -def patch_pypy(): - from pypy.interpreter.baseobjspace import W_Root - - def descr_call_mismatch(self, space, opname, RequiredClass, args): - from pypy.interpreter.error import OperationError - msg = 'This message will never be displayed :-)' - raise OperationError(space.w_TypeError, space.wrap(msg)) - W_Root.descr_call_mismatch = descr_call_mismatch - - -def checkmodule(modname, backend, interactive=False, basepath='pypy.module'): - "Compile a fake PyPy module." - from pypy.objspace.fake.objspace import FakeObjSpace, W_Object - from pypy.translator.driver import TranslationDriver - +def checkmodule(modname): space = FakeObjSpace() - space.config.translating = True - ModuleClass = __import__(basepath + '.%s' % modname, - None, None, ['Module']).Module - module = ModuleClass(space, space.wrap(modname)) - w_moduledict = module.getdict(space) - - gateways = find_gateways(modname, basepath, module) - functions = [gw.__spacebind__(space) for gw in gateways] - arguments = Arguments.frompacked(space, W_Object(), W_Object()) - dummy_function = copy(functions[0]) - - def main(argv): # use the standalone mode not to allow SomeObject - dummy_rpython(dummy_function) - for func in functions: - func.call_args(arguments) - return 0 - - patch_pypy() - driver = TranslationDriver() - driver.setup(main, None) - try: - driver.proceed(['compile_' + backend]) - except SystemExit: - raise - except: - if not interactive: - raise - debug(driver) - raise SystemExit(1) + mod = __import__('pypy.module.%s' % modname, None, None, ['__doc__']) + # force computation and record what we wrap + module = mod.Module(space, W_Root()) + for name in module.loaders: + module._load_lazily(space, name) + # + space.translates() diff --git a/pypy/objspace/fake/objspace.py b/pypy/objspace/fake/objspace.py --- a/pypy/objspace/fake/objspace.py +++ b/pypy/objspace/fake/objspace.py @@ -1,147 +1,300 @@ -from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, W_Root -from pypy.interpreter.typedef import TypeDef +from pypy.interpreter.baseobjspace import W_Root, ObjSpace +from pypy.interpreter.baseobjspace import Wrappable, SpaceCache +from pypy.interpreter import argument, gateway +from pypy.interpreter.typedef import TypeDef, GetSetProperty +from pypy.annotation.model import SomeInstance, s_None +from pypy.rpython.extregistry import ExtRegistryEntry +from pypy.rpython.lltypesystem import lltype +from pypy.tool.sourcetools import compile2, func_with_new_name +from pypy.rlib.unroll import unrolling_iterable +from pypy.rlib.objectmodel import instantiate, we_are_translated from pypy.rlib.nonconst import NonConstant from pypy.rlib.rarithmetic import r_uint -from pypy.rlib.rbigint import rbigint +from pypy.translator.translator import TranslationContext -#class W_Type(W_Root): -# _attrs_ = () -class W_Object(W_Root): - _attrs_ = () -W_Object.typedef = TypeDef('foobar') +class W_MyObject(Wrappable): + typedef = None -def make_dummy(a=W_Object(), b=W_Object()): - def fn(*args): - if NonConstant(True): - return a - else: - return b - return fn + def getdict(self, space): + return w_obj_or_none() -int_dummy = make_dummy(42, 43) -float_dummy = make_dummy(42.0, 42.1) -uint_dummy = make_dummy(r_uint(42), r_uint(43)) -str_dummy = make_dummy('foo', 'bar') -bool_dummy = make_dummy(True, False) -unicode_dummy = make_dummy(u'abc', u'cde') -bigint_dummy = make_dummy(rbigint.fromint(0), rbigint.fromint(1)) + def getdictvalue(self, space, attr): + attr + "xx" # check that it's a string + return w_obj_or_none() + + def setdictvalue(self, space, attr, w_value): + attr + "xx" # check that it's a string + is_root(w_value) + return NonConstant(True) + + def deldictvalue(self, space, attr): + attr + "xx" # check that it's a string + return NonConstant(True) + + def setdict(self, space, w_dict): + is_root(w_dict) + + def setclass(self, space, w_subtype): + is_root(w_subtype) + + def str_w(self, space): + return NonConstant("foobar") + + def unicode_w(self, space): + return NonConstant(u"foobar") + + def int_w(self, space): + return NonConstant(-42) + + def uint_w(self, space): + return r_uint(NonConstant(42)) + + def bigint_w(self, space): + from pypy.rlib.rbigint import rbigint + return rbigint.fromint(NonConstant(42)) + + +def w_some_obj(): + if NonConstant(False): + return W_Root() + return W_MyObject() + +def w_obj_or_none(): + if NonConstant(False): + return None + return w_some_obj() + +def is_root(w_obj): + assert isinstance(w_obj, W_Root) +is_root.expecting = W_Root + +def is_arguments(arg): + assert isinstance(arg, argument.Arguments) +is_arguments.expecting = argument.Arguments + + +class Entry(ExtRegistryEntry): + _about_ = is_root, is_arguments + + def compute_result_annotation(self, s_w_obj): + cls = self.instance.expecting + s_inst = SomeInstance(self.bookkeeper.getuniqueclassdef(cls), + can_be_None=True) + assert s_inst.contains(s_w_obj) + return s_None + + def specialize_call(self, hop): + return hop.inputconst(lltype.Void, None) + +# ____________________________________________________________ + class FakeObjSpace(ObjSpace): - w_None = W_Object() - w_False = W_Object() - w_True = W_Object() - w_Ellipsis = W_Object() - w_NotImplemented = W_Object() - w_int = W_Object() - w_dict = W_Object() - w_float = W_Object() - w_long = W_Object() - w_tuple = W_Object() - w_str = W_Object() - w_basestring = W_Object() - w_unicode = W_Object() - w_type = W_Object() - w_instance = W_Object() - w_slice = W_Object() - w_hex = W_Object() - w_oct = W_Object() - - def initialize(self): - self.config.objspace.geninterp = False - self.config.objspace.disable_call_speedhacks = True - self.wrap_cache = {} - self.make_builtins() - def _freeze_(self): - return True + def __init__(self): + self._seen_extras = [] + ObjSpace.__init__(self) + + def float_w(self, w_obj): + is_root(w_obj) + return NonConstant(42.5) + + def is_true(self, w_obj): + is_root(w_obj) + return NonConstant(False) + + def unwrap(self, w_obj): + "NOT_RPYTHON" + raise NotImplementedError + + def newdict(self, module=False, instance=False, classofinstance=None, + strdict=False): + return w_some_obj() + + def newtuple(self, list_w): + for w_x in list_w: + is_root(w_x) + return w_some_obj() + + def newlist(self, list_w): + for w_x in list_w: + is_root(w_x) + return w_some_obj() + + def newslice(self, w_start, w_end, w_step): + is_root(w_start) + is_root(w_end) + is_root(w_step) + return w_some_obj() + + def newint(self, x): + return w_some_obj() + + def newfloat(self, x): + return w_some_obj() + + def marshal_w(self, w_obj): + "NOT_RPYTHON" + raise NotImplementedError def wrap(self, x): - if isinstance(x, Wrappable): - w_result = x.__spacebind__(self) - return w_result - return W_Object() + if not we_are_translated(): + if isinstance(x, gateway.interp2app): + self._see_interp2app(x) + if isinstance(x, GetSetProperty): + self._see_getsetproperty(x) + return w_some_obj() wrap._annspecialcase_ = "specialize:argtype(1)" - def unwrap(self, w_obj): - assert isinstance(w_obj, W_Object) - return None + def _see_interp2app(self, interp2app): + "NOT_RPYTHON" + activation = interp2app._code.activation + def check(): + scope_w = [w_some_obj()] * NonConstant(42) + w_result = activation._run(self, scope_w) + is_root(w_result) + check = func_with_new_name(check, 'check__' + interp2app.name) + self._seen_extras.append(check) - lookup = make_dummy() - allocate_instance = make_dummy() - getattr = make_dummy() - setattr = make_dummy() - getitem = make_dummy() - setitem = make_dummy() - delitem = make_dummy() - int_w = int_dummy - uint_w = uint_dummy - float_w = float_dummy - unicode_w = unicode_dummy - bigint_w = bigint_dummy - iter = make_dummy() - type = make_dummy() - str = make_dummy() - int = make_dummy() - float = make_dummy() - repr = make_dummy() - id = make_dummy() - len = make_dummy() - str_w = str_dummy - call_args = make_dummy() - new_interned_str = make_dummy() - newint = make_dummy() - newlong = make_dummy() - newfloat = make_dummy() - def newdict(self, module=False): - return self.newfloat() - newlist = make_dummy() - emptylist = make_dummy() - newtuple = make_dummy() - newslice = make_dummy() - lt = make_dummy() - le = make_dummy() - eq = make_dummy() - ne = make_dummy() - gt = make_dummy() - ge = make_dummy() - lt_w = bool_dummy - le_w = bool_dummy - eq_w = bool_dummy - ne_w = bool_dummy - gt_w = bool_dummy - ge_w = bool_dummy - is_w = bool_dummy - is_ = make_dummy() - next = make_dummy() - is_true = bool_dummy - nonzero = make_dummy() - issubtype = make_dummy() - ord = make_dummy() - hash = make_dummy() - delattr = make_dummy() # should return None? - contains = make_dummy() - hex = make_dummy() - oct = make_dummy() - pow = make_dummy() - inplace_pow = make_dummy() - cmp = make_dummy() + def _see_getsetproperty(self, getsetproperty): + "NOT_RPYTHON" + space = self + def checkprop(): + getsetproperty.fget(getsetproperty, space, w_some_obj()) + if getsetproperty.fset is not None: + getsetproperty.fset(getsetproperty, space, w_some_obj(), + w_some_obj()) + if getsetproperty.fdel is not None: + getsetproperty.fdel(getsetproperty, space, w_some_obj()) + if not getsetproperty.name.startswith('<'): + checkprop = func_with_new_name(checkprop, + 'checkprop__' + getsetproperty.name) + self._seen_extras.append(checkprop) - # XXsX missing operations - def coerce(self, *args): raise NotImplementedError("space.coerce()") - def get(self, *args): raise NotImplementedError("space.get()") - def set(self, *args): raise NotImplementedError("space.set()") - def delete(self, *args): raise NotImplementedError("space.delete()") - def userdel(self, *args): raise NotImplementedError("space.userdel()") - def marshal_w(self, *args):raise NotImplementedError("space.marshal_w()") + def call_obj_args(self, w_callable, w_obj, args): + is_root(w_callable) + is_root(w_obj) + is_arguments(args) + return w_some_obj() - gettypefor = make_dummy() - gettypeobject = make_dummy() - unpackiterable = make_dummy([W_Object()], [W_Object()]) + def call(self, w_callable, w_args, w_kwds=None): + is_root(w_callable) + is_root(w_args) + is_root(w_kwds) + return w_some_obj() + def call_function(self, w_func, *args_w): + is_root(w_func) + for w_arg in list(args_w): + is_root(w_arg) + return w_some_obj() -## Register all exceptions -import exceptions -for name in ObjSpace.ExceptionTable: - exc = getattr(exceptions, name) - setattr(FakeObjSpace, 'w_' + name, W_Object()) + def call_args(self, w_func, args): + is_root(w_func) + is_arguments(args) + return w_some_obj() + + def gettypefor(self, cls): + return self.gettypeobject(cls.typedef) + + def gettypeobject(self, typedef): + assert typedef is not None + return self.fromcache(TypeCache).getorbuild(typedef) + + def unpackiterable(self, w_iterable, expected_length=-1): + is_root(w_iterable) + if expected_length < 0: + expected_length = 3 + return [w_some_obj()] * expected_length + + def allocate_instance(self, cls, w_subtype): + is_root(w_subtype) + return instantiate(cls) + allocate_instance._annspecialcase_ = "specialize:arg(1)" + + def decode_index(self, w_index_or_slice, seqlength): + is_root(w_index_or_slice) + return (NonConstant(42), NonConstant(42), NonConstant(42)) + + def decode_index4(self, w_index_or_slice, seqlength): + is_root(w_index_or_slice) + return (NonConstant(42), NonConstant(42), + NonConstant(42), NonConstant(42)) + + def exec_(self, *args, **kwds): + pass + + # ---------- + + def translates(self, func=None, argtypes=None): + if func is not None: + if argtypes is None: + nb_args = func.func_code.co_argcount + argtypes = [W_Root] * nb_args + # + t = TranslationContext() + self.t = t # for debugging + ann = t.buildannotator() + if func is not None: + ann.build_types(func, argtypes, complete_now=False) + # + # annotate all _seen_extras, knowing that annotating some may + # grow the list + done = 0 + while done < len(self._seen_extras): + print self._seen_extras + ann.build_types(self._seen_extras[done], [], + complete_now=False) + done += 1 + ann.complete() + #t.viewcg() + t.buildrtyper().specialize() + t.checkgraphs() + + +def setup(): + for name in (ObjSpace.ConstantTable + + ObjSpace.ExceptionTable + + ['int', 'str', 'float', 'long', 'tuple', 'list', + 'dict', 'unicode', 'complex', 'slice', 'bool', + 'type', 'basestring']): + setattr(FakeObjSpace, 'w_' + name, w_some_obj()) + # + for (name, _, arity, _) in ObjSpace.MethodTable: + args = ['w_%d' % i for i in range(arity)] + d = {'is_root': is_root, + 'w_some_obj': w_some_obj} + exec compile2("""\ + def meth(self, %s): + %s + return w_some_obj() + """ % (', '.join(args), + '; '.join(['is_root(%s)' % arg for arg in args]))) in d + meth = func_with_new_name(d['meth'], name) + setattr(FakeObjSpace, name, meth) + # + for name in ObjSpace.IrregularOpTable: + assert hasattr(FakeObjSpace, name) # missing? + +setup() + +# ____________________________________________________________ + +class TypeCache(SpaceCache): + def build(cache, typedef): + assert isinstance(typedef, TypeDef) + for value in typedef.rawdict.values(): + cache.space.wrap(value) + return w_some_obj() + +class FakeCompiler(object): + pass +FakeObjSpace.default_compiler = FakeCompiler() + +class FakeModule(object): + def get(self, name): + name + "xx" # check that it's a string + return w_some_obj() +FakeObjSpace.sys = FakeModule() +FakeObjSpace.sys.filesystemencoding = 'foobar' diff --git a/pypy/objspace/fake/test/__init__.py b/pypy/objspace/fake/test/__init__.py deleted file mode 100644 diff --git a/pypy/objspace/fake/test/test_checkmodule.py b/pypy/objspace/fake/test/test_checkmodule.py --- a/pypy/objspace/fake/test/test_checkmodule.py +++ b/pypy/objspace/fake/test/test_checkmodule.py @@ -1,7 +1,63 @@ import py -from pypy.objspace.fake.checkmodule import checkmodule +from pypy.objspace.fake.objspace import FakeObjSpace, is_root +from pypy.interpreter.baseobjspace import Wrappable +from pypy.interpreter.typedef import TypeDef, GetSetProperty +from pypy.interpreter.gateway import interp2app, W_Root, ObjSpace -def test_dotnet(): - # the only module known to pass checkmodule is _dotnet so far - py.test.skip('fixme') - checkmodule('_dotnet', 'cli') + +def make_checker(): + check = [] + def see(): + check.append(True) + see._annspecialcase_ = 'specialize:memo' + return see, check + +def test_wrap_interp2app(): + see, check = make_checker() + space = FakeObjSpace() + assert len(space._seen_extras) == 0 + assert len(check) == 0 + space.wrap(interp2app(lambda space: see())) + assert len(space._seen_extras) == 1 + assert len(check) == 0 + space.translates() + assert len(check) == 1 + +def test_wrap_interp2app_int(): + see, check = make_checker() + def foobar(space, x, w_y, z): + is_root(w_y) + see() + return space.wrap(x - z) + space = FakeObjSpace() + space.wrap(interp2app(foobar, unwrap_spec=[ObjSpace, int, W_Root, int])) + space.translates() + assert check + +def test_wrap_GetSetProperty(): + see, check = make_checker() + def foobar(w_obj, space): + is_root(w_obj) + see() + return space.w_None + space = FakeObjSpace() + space.wrap(GetSetProperty(foobar)) + space.translates() + assert check + + +def test_gettypefor_untranslated(): + see, check = make_checker() + class W_Foo(Wrappable): + def do_it(self, space, w_x): + is_root(w_x) + see() + return W_Root() + W_Foo.typedef = TypeDef('foo', + __module__ = 'barmod', + do_it = interp2app(W_Foo.do_it)) + space = FakeObjSpace() + space.gettypefor(W_Foo) + assert not check + space.translates() + assert check diff --git a/pypy/objspace/fake/test/test_objspace.py b/pypy/objspace/fake/test/test_objspace.py new file mode 100644 --- /dev/null +++ b/pypy/objspace/fake/test/test_objspace.py @@ -0,0 +1,74 @@ +import py +from pypy.objspace.fake.objspace import FakeObjSpace, W_Root +from pypy.interpreter.argument import Arguments +from pypy.interpreter.typedef import TypeDef +from pypy.interpreter.baseobjspace import Wrappable +from pypy.rlib.unroll import unrolling_iterable + +def test_create(): + FakeObjSpace() + + +class TestTranslate: + def setup_method(self, meth): + self.space = FakeObjSpace() + + def test_simple(self): + space = self.space + space.translates(lambda w_x, w_y: space.add(w_x, w_y)) + + def test_methodtable(self): + space = self.space + for fixed_arity in [1, 2, 3, 4]: + # + methodtable = [name for (name, _, arity, _) in space.MethodTable + if arity == fixed_arity] + methodtable = unrolling_iterable(methodtable) + args_w = (W_Root(),) * fixed_arity + # + def f(): + for name in methodtable: + getattr(space, name)(*args_w) + # + space.translates(f) + + def test_newdict(self): + space = self.space + space.translates(lambda: (space.newdict(), + space.newdict(strdict=True))) + + def test_constants(self): + space = self.space + space.translates(lambda: (space.w_None, space.w_True, space.w_False, + space.w_int, space.w_str, + space.w_TypeError)) + + def test_wrap(self): + space = self.space + space.translates(lambda: (space.wrap(42), space.wrap(42.5), + space.wrap("foo"))) + + def test_call_args(self): + space = self.space + args = Arguments(space, [W_Root()]) + space.translates(lambda: space.call_args(W_Root(), args)) + + def test_gettypefor(self): + space = self.space + class W_Foo(Wrappable): + typedef = TypeDef("foo") + space.translates(lambda: space.gettypefor(W_Foo)) + + def test_is_true(self): + space = self.space + space.translates(lambda: space.is_true(W_Root())) + py.test.raises(AssertionError, + space.translates, lambda: space.is_true(42)) + + def test_unpackiterable(self): + space = self.space + space.translates(lambda: (space.unpackiterable(W_Root()), + space.unpackiterable(W_Root(), 42))) + + def test_newlist(self): + self.space.newlist([W_Root(), W_Root()]) diff --git a/pypy/objspace/fake/test/test_zmodules.py b/pypy/objspace/fake/test/test_zmodules.py new file mode 100644 --- /dev/null +++ b/pypy/objspace/fake/test/test_zmodules.py @@ -0,0 +1,17 @@ +from pypy.objspace.fake.checkmodule import checkmodule + + +def test__bisect(): + checkmodule('_bisect') + +def test__random(): + checkmodule('_random') + +def test_cStringIO(): + checkmodule('cStringIO') + +def test_itertools(): + checkmodule('itertools') + +def test_micronumpy(): + checkmodule('micronumpy') diff --git a/pypy/rlib/rmmap.py b/pypy/rlib/rmmap.py --- a/pypy/rlib/rmmap.py +++ b/pypy/rlib/rmmap.py @@ -421,7 +421,11 @@ low, high = _get_file_size(self.file_handle) if not high and low <= sys.maxint: return low + # not so sure if the signed/unsigned strictness is a good idea: + high = rffi.cast(lltype.Unsigned, high) + low = rffi.cast(lltype.Unsigned, low) size = (high << 32) + low + size = rffi.cast(lltype.Signed, size) elif _POSIX: st = os.fstat(self.fd) size = st[stat.ST_SIZE] diff --git a/pypy/translator/c/test/test_typed.py b/pypy/translator/c/test/test_typed.py --- a/pypy/translator/c/test/test_typed.py +++ b/pypy/translator/c/test/test_typed.py @@ -275,6 +275,14 @@ fn = self.getcompiled(f, [r_longlong]) assert fn(0) == 0 + def test_upcast_int(self): + from pypy.rpython.lltypesystem import rffi + def f(v): + v = rffi.cast(rffi.USHORT, v) + return intmask(v) + fn = self.getcompiled(f, [int]) + assert fn(0x1234CDEF) == 0xCDEF + def test_function_ptr(self): def f1(): return 1 diff --git a/pypy/translator/translator.py b/pypy/translator/translator.py --- a/pypy/translator/translator.py +++ b/pypy/translator/translator.py @@ -150,11 +150,11 @@ from pypy.translator.tool.graphpage import FlowGraphPage FlowGraphPage(self).display() - def viewcg(self, center_graph=None): + def viewcg(self, center_graph=None, huge=100): """Shows the whole call graph and the class hierarchy, based on the computed annotations.""" from pypy.translator.tool.graphpage import TranslatorPage - TranslatorPage(self, center_graph=center_graph).display() + TranslatorPage(self, center_graph=center_graph, huge=huge).display() _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit