Author: Amaury Forgeot d'Arc <amaur...@gmail.com> Branch: more-rposix Changeset: r77019:0240be39ca29 Date: 2015-05-03 22:59 +0200 http://bitbucket.org/pypy/pypy/changeset/0240be39ca29/
Log: Remove all code related to "lazy registration" of functions, not used anymore. diff --git a/rpython/rtyper/extfunc.py b/rpython/rtyper/extfunc.py --- a/rpython/rtyper/extfunc.py +++ b/rpython/rtyper/extfunc.py @@ -1,123 +1,9 @@ -from rpython.rtyper import extregistry from rpython.rtyper.extregistry import ExtRegistryEntry from rpython.rtyper.lltypesystem.lltype import typeOf from rpython.annotator import model as annmodel from rpython.annotator.signature import annotation -import py, sys - -class extdef(object): - - def __init__(self, *args, **kwds): - self.def_args = args - self.def_kwds = kwds - -def lazy_register(func_or_list, register_func): - """ Lazily register external function. Will create a function, - which explodes when llinterpd/translated, but does not explode - earlier - """ - if isinstance(func_or_list, list): - funcs = func_or_list - else: - funcs = [func_or_list] - try: - val = register_func() - if isinstance(val, extdef): - assert len(funcs) == 1 - register_external(funcs[0], *val.def_args, **val.def_kwds) - return - return val - except (SystemExit, MemoryError, KeyboardInterrupt): - raise - except: - exc, exc_inst, tb = sys.exc_info() - for func in funcs: - # if the function has already been registered and we got - # an exception afterwards, the ExtRaisingEntry would create - # a double-registration and crash in an AssertionError that - # masks the original problem. In this case, just re-raise now. - if extregistry.is_registered(func): - raise exc, exc_inst, tb - class ExtRaisingEntry(ExtRegistryEntry): - _about_ = func - def __getattr__(self, attr): - if attr == '_about_' or attr == '__dict__': - return super(ExtRegistryEntry, self).__getattr__(attr) - raise exc, exc_inst, tb - -def registering(func, condition=True): - if not condition: - return lambda method: None - - def decorator(method): - method._registering_func = func - return method - return decorator - -def registering_if(ns, name, condition=True): - try: - func = getattr(ns, name) - except AttributeError: - condition = False - func = None - - return registering(func, condition=condition) - -class LazyRegisteringMeta(type): - def __new__(self, _name, _type, _vars): - RegisteringClass = type.__new__(self, _name, _type, _vars) - allfuncs = [] - for varname in _vars: - attr = getattr(RegisteringClass, varname) - f = getattr(attr, '_registering_func', None) - if f: - allfuncs.append(f) - registering_inst = lazy_register(allfuncs, RegisteringClass) - if registering_inst is not None: - for varname in _vars: - attr = getattr(registering_inst, varname) - f = getattr(attr, '_registering_func', None) - if f: - lazy_register(f, attr) - RegisteringClass.instance = registering_inst - # override __init__ to avoid confusion - def raising(self): - raise TypeError("Cannot call __init__ directly, use cls.instance to access singleton") - RegisteringClass.__init__ = raising - return RegisteringClass - -class BaseLazyRegistering(object): - __metaclass__ = LazyRegisteringMeta - compilation_info = None - - def configure(self, CConfig): - classes_seen = self.__dict__.setdefault('__classes_seen', {}) - if CConfig in classes_seen: - return - from rpython.rtyper.tool import rffi_platform as platform - # copy some stuff - if self.compilation_info is None: - self.compilation_info = CConfig._compilation_info_ - else: - self.compilation_info = self.compilation_info.merge( - CConfig._compilation_info_) - self.__dict__.update(platform.configure(CConfig)) - classes_seen[CConfig] = True - - def llexternal(self, *args, **kwds): - kwds = kwds.copy() - from rpython.rtyper.lltypesystem import rffi - - if 'compilation_info' in kwds: - kwds['compilation_info'] = self.compilation_info.merge( - kwds['compilation_info']) - else: - kwds['compilation_info'] = self.compilation_info - return rffi.llexternal(*args, **kwds) - - def _freeze_(self): - return True +import py class ExtFuncEntry(ExtRegistryEntry): safe_not_sandboxed = False @@ -252,8 +138,6 @@ else: FunEntry.__name__ = function.func_name -BaseLazyRegistering.register = staticmethod(register_external) - def is_external(func): if hasattr(func, 'value'): func = func.value diff --git a/rpython/rtyper/test/test_extfunc.py b/rpython/rtyper/test/test_extfunc.py --- a/rpython/rtyper/test/test_extfunc.py +++ b/rpython/rtyper/test/test_extfunc.py @@ -1,7 +1,7 @@ import py from rpython.rtyper.extfunc import ExtFuncEntry, register_external,\ - is_external, lazy_register + is_external from rpython.annotator import model as annmodel from rpython.annotator.annrpython import RPythonAnnotator from rpython.annotator.policy import AnnotatorPolicy diff --git a/rpython/rtyper/test/test_extfuncregister.py b/rpython/rtyper/test/test_extfuncregister.py deleted file mode 100644 --- a/rpython/rtyper/test/test_extfuncregister.py +++ /dev/null @@ -1,113 +0,0 @@ - -""" A small test suite for discovering whether lazy registration -of register_external functions work as intendet -""" - -import py -from rpython.rtyper.extfunc import lazy_register, BaseLazyRegistering, \ - registering, registering_if, extdef -from rpython.rtyper.test.test_llinterp import interpret - -def test_lazy_register(): - def f(): - return 3 - - def g(): - return f() - - def reg_func(): - 1/0 - - lazy_register(f, reg_func) - - py.test.raises(ZeroDivisionError, interpret, g, []) - -def test_lazy_register_class_raising(): - def f(): - return 3 - - def g(): - return 3 - - class LazyRegister(BaseLazyRegistering): - def __init__(self): - self.stuff = 8 - self.x = [] - - @registering(f) - def register_f(self): - self.x.append(1) - 1/0 - - @registering(g) - def register_g(self): - self.x.append(2) - self.register(g, [], int, llimpl=lambda : self.stuff) - - py.test.raises(TypeError, "LazyRegister()") - assert LazyRegister.instance.x == [1, 2] - py.test.raises(ZeroDivisionError, interpret, lambda : f(), []) - assert interpret(lambda : g(), []) == 8 - -def test_lazy_register_extdef(): - def g(): - return 3 - - x = [] - - def register_g(): - x.append('g') - return extdef([], int, llimpl=lambda : 21) - - nothing = lazy_register(g, register_g) - - assert x == ['g'] - assert nothing is None - assert interpret(lambda : g(), []) == 21 - -def test_lazy_register_raising_init(): - def f(): - return 3 - - def g(): - return 3 - - class LazyRegister(BaseLazyRegistering): - def __init__(self): - 1/0 - - @registering(f) - def register_f(self): - pass - - @registering(g) - def register_g(self): - pass - - py.test.raises(ZeroDivisionError, interpret, lambda : f(), []) - py.test.raises(ZeroDivisionError, interpret, lambda : g(), []) - -def test_registering_if(): - class A: - @staticmethod - def f(): - pass - - @registering_if(A, 'f') - def foo(): - pass - - assert foo._registering_func is A.f - - @registering_if(A, 'g') - def bar(): - pass - - assert bar is None - - @registering_if(A, 'f', False) - def baz(): - pass - - assert baz is None - _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit