[pypy-svn] r18418 - in pypy/branch/hl-backend/pypy/rpython/ootype: . test
bert at codespeak.net
bert at codespeak.net
Tue Oct 11 17:38:06 CEST 2005
Author: bert
Date: Tue Oct 11 17:38:06 2005
New Revision: 18418
Modified:
pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py
pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ootype.py
Log:
Refactor to share code with lltype.
(Samuele, Bert, Boria, Aurelien, Arre)
Modified: pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py (original)
+++ pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py Tue Oct 11 17:38:06 2005
@@ -4,130 +4,11 @@
from pypy.tool.uid import Hashable
from pypy.tool.tls import tlsobject
from types import NoneType
+from pypy.rpython.lltype import LowLevelType, Signed, Unsigned, Float, Char
+from pypy.rpython.lltype import Bool, Void, UniChar, typeOf
-TLS = tlsobject()
-
-def saferecursive(func, defl):
- def safe(*args):
- try:
- seeing = TLS.seeing
- except AttributeError:
- seeing = TLS.seeing = {}
- seeingkey = tuple([func] + [id(arg) for arg in args])
- if seeingkey in seeing:
- return defl
- seeing[seeingkey] = True
- try:
- return func(*args)
- finally:
- del seeing[seeingkey]
- return safe
-
-#safe_equal = saferecursive(operator.eq, True)
-def safe_equal(x, y):
- # a specialized version for performance
- try:
- seeing = TLS.seeing_eq
- except AttributeError:
- seeing = TLS.seeing_eq = {}
- seeingkey = (id(x), id(y))
- if seeingkey in seeing:
- return True
- seeing[seeingkey] = True
- try:
- return x == y
- finally:
- del seeing[seeingkey]
-
-
-class frozendict(dict):
-
- def __hash__(self):
- items = self.items()
- items.sort()
- return hash(tuple(items))
-
-
-class OOType(object):
- # the following line prevents '__cached_hash' to be in the __dict__ of
- # the instance, which is needed for __eq__() and __hash__() to work.
- __slots__ = ['__dict__', '__cached_hash']
-
- def __eq__(self, other):
- return self.__class__ is other.__class__ and (
- self is other or safe_equal(self.__dict__, other.__dict__))
-
- def __ne__(self, other):
- return not (self == other)
-
- def __hash__(self):
- # cannot use saferecursive() -- see test_lltype.test_hash().
- # NB. the __cached_hash should neither be used nor updated
- # if we enter with hash_level > 0, because the computed
- # __hash__ can be different in this situation.
- hash_level = 0
- try:
- hash_level = TLS.nested_hash_level
- if hash_level == 0:
- return self.__cached_hash
- except AttributeError:
- pass
- if hash_level >= 3:
- return 0
- items = self.__dict__.items()
- items.sort()
- TLS.nested_hash_level = hash_level + 1
- try:
- result = hash((self.__class__,) + tuple(items))
- finally:
- TLS.nested_hash_level = hash_level
- if hash_level == 0:
- self.__cached_hash = result
- return result
-
- # due to this dynamic hash value, we should forbid
- # pickling, until we have an algorithm for that.
- # but we just provide a tag for external help.
- __hash_is_not_constant__ = True
-
- def __repr__(self):
- return '<%s>' % (self,)
-
- def __str__(self):
- return self.__class__.__name__
-
- def _short_name(self):
- return str(self)
-
- def _defl(self, parent=None, parentindex=None):
- raise NotImplementedError
-
- def _freeze_(self):
- return True
-
-class Primitive(OOType):
- def __init__(self, name, default):
- self._name = self.__name__ = name
- self._default = default
-
- def __str__(self):
- return self._name
-
- def _defl(self, parent=None, parentindex=None):
- return self._default
-
- def _is_atomic(self):
- return True
-
- _example = _defl
-
-Signed = Primitive("Signed", 0)
-Unsigned = Primitive("Unsigned", r_uint(0))
-Float = Primitive("Float", 0.0)
-Char = Primitive("Char", '\x00')
-Bool = Primitive("Bool", False)
-Void = Primitive("Void", None)
-UniChar = Primitive("UniChar", u'\x00')
+class OOType(LowLevelType):
+ pass
class Class(OOType):
@@ -205,16 +86,16 @@
return meth
-class Func(OOType):
+class StaticMethod(OOType):
def __init__(self, args, result):
self.ARGS = tuple(args)
self.RESULT = result
-class Meth(Func):
+class Meth(StaticMethod):
def __init__(self, args, result):
- Func.__init__(self, args, result)
+ StaticMethod.__init__(self, args, result)
# ____________________________________________________________
class _instance(object):
@@ -282,11 +163,11 @@
raise RuntimeError,"calling undefined function"
return callb
-class _func(_callable):
+class _static_meth(_callable):
- def __init__(self, FUNCTION, **attrs):
- assert isinstance(FUNCTION, Func)
- _callable.__init__(self, FUNCTION, **attrs)
+ def __init__(self, STATICMETHOD, **attrs):
+ assert isinstance(STATICMETHOD, StaticMethod)
+ _callable.__init__(self, STATICMETHOD, **attrs)
def __call__(self, *args):
return self._checkargs(args)(*args)
@@ -312,8 +193,8 @@
def new(CLASS):
return _instance(CLASS)
-def func(FUNCTION, **attrs):
- return _func(FUNCTION, **attrs)
+def static_meth(FUNCTION, **attrs):
+ return _static_meth(FUNCTION, **attrs)
def meth(METHOD, **attrs):
return _meth(METHOD, **attrs)
@@ -336,30 +217,3 @@
return False
-def typeOf(val):
- try:
- return val._TYPE
- except AttributeError:
- tp = type(val)
- if tp is NoneType:
- return Void # maybe
- if tp is int:
- return Signed
- if tp is bool:
- return Bool
- if tp is r_uint:
- return Unsigned
- if tp is float:
- return Float
- if tp is str:
- assert len(val) == 1
- return Char
- if tp is unicode:
- assert len(val) == 1
- return UniChar
- raise TypeError("typeOf(%r object)" % (tp.__name__,))
-
-class InvalidCast(TypeError):
- pass
-
-
Modified: pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ootype.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ootype.py (original)
+++ pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ootype.py Tue Oct 11 17:38:06 2005
@@ -62,22 +62,22 @@
py.test.raises(TypeError, """D = Class("test2", C, {"a": (Signed, 3)})""")
-def test_simple_function():
- F = Func([Signed, Signed], Signed)
+def test_simple_static_method():
+ F = StaticMethod([Signed, Signed], Signed)
def f_(a, b):
return a+b
- f = func(F, _name="f", _callable=f_)
+ f = static_meth(F, _name="f", _callable=f_)
assert typeOf(f) == F
result = f(2, 3)
assert typeOf(result) == Signed
assert result == 5
-def test_function_args():
- F = Func([Signed, Signed], Signed)
+def test_static_method_args():
+ F = StaticMethod([Signed, Signed], Signed)
def f_(a, b):
return a+b
- f = func(F, _name="f", _callable=f_)
+ f = static_meth(F, _name="f", _callable=f_)
py.test.raises(TypeError, "f(2.0, 3.0)")
py.test.raises(TypeError, "f()")
More information about the Pypy-commit
mailing list