Author: Ronan Lamy <ronan.l...@gmail.com> Branch: Changeset: r80957:b4515dee6ebf Date: 2015-11-25 16:31 +0000 http://bitbucket.org/pypy/pypy/changeset/b4515dee6ebf/
Log: clean up test_objectmodel.py diff --git a/rpython/rlib/test/test_objectmodel.py b/rpython/rlib/test/test_objectmodel.py --- a/rpython/rlib/test/test_objectmodel.py +++ b/rpython/rlib/test/test_objectmodel.py @@ -1,8 +1,12 @@ from collections import OrderedDict import py -from rpython.rlib.objectmodel import * -from rpython.rlib import types -from rpython.annotator import model +from rpython.rlib.objectmodel import ( + r_dict, UnboxedValue, Symbolic, compute_hash, compute_identity_hash, + compute_unique_id, current_object_addr_as_int, we_are_translated, + prepare_dict_update, reversed_dict, specialize, enforceargs, newlist_hint, + resizelist_hint, is_annotation_constant, always_inline, + iterkeys_with_hash, iteritems_with_hash, contains_with_hash, + setitem_with_hash, getitem_with_hash, delitem_with_hash, import_from_mixin) from rpython.translator.translator import TranslationContext, graphof from rpython.rtyper.test.tool import BaseRtypingTest from rpython.rtyper.test.test_llinterp import interpret @@ -72,9 +76,9 @@ rdic = r_dict(operator.eq, hash) rdic['x'] = rdic assert str(rdic) == "r_dict({'x': r_dict({...})})" - assert repr(rdic)== "r_dict({'x': r_dict({...})})" + assert repr(rdic) == "r_dict({'x': r_dict({...})})" -def test_r_dict(): +def func_r_dict(): # NB. this test function is also annotated/rtyped by the next tests d = r_dict(strange_key_eq, strange_key_hash) return play_with_r_dict(d) @@ -82,10 +86,11 @@ class Strange: def key_eq(strange, key1, key2): return key1[0] == key2[0] # only the 1st character is relevant + def key_hash(strange, key): return ord(key[0]) -def test_r_dict_bm(): +def func_r_dict_bm(): # NB. this test function is also annotated by the next tests strange = Strange() d = r_dict(strange.key_eq, strange.key_hash) @@ -94,7 +99,7 @@ def test_annotate_r_dict(): t = TranslationContext() a = t.buildannotator() - a.build_types(test_r_dict, []) + a.build_types(func_r_dict, []) #t.view() graph = graphof(t, strange_key_eq) assert a.binding(graph.getargs()[0]).knowntype == str @@ -105,7 +110,7 @@ def test_annotate_r_dict_bm(): t = TranslationContext() a = t.buildannotator() - a.build_types(test_r_dict_bm, []) + a.build_types(func_r_dict_bm, []) #t.view() strange_key_eq = Strange.key_eq.im_func strange_key_hash = Strange.key_hash.im_func @@ -124,6 +129,7 @@ def test_unboxed_value(): class Base(object): __slots__ = () + class C(Base, UnboxedValue): __slots__ = 'smallint' @@ -196,7 +202,7 @@ class TestObjectModel(BaseRtypingTest): def test_we_are_translated(self): - assert we_are_translated() == False + assert we_are_translated() is False def fn(): return we_are_translated() @@ -204,11 +210,11 @@ assert res is True def test_rtype_r_dict(self): - res = self.interpret(test_r_dict, []) + res = self.interpret(func_r_dict, []) assert res is True def test_rtype_r_dict_bm(self): - res = self.interpret(test_r_dict_bm, []) + res = self.interpret(func_r_dict_bm, []) assert res is True def test_rtype_constant_r_dicts(self): @@ -290,24 +296,27 @@ def test_access_in_try(self): h = lambda x: 1 - eq = lambda x,y: x==y + eq = lambda x, y: x == y + def f(d): try: return d[2] except ZeroDivisionError: return 42 return -1 + def g(n): d = r_dict(eq, h) d[1] = n - d[2] = 2*n + d[2] = 2 * n return f(d) + res = self.interpret(g, [3]) assert res == 6 def test_access_in_try_set(self): h = lambda x: 1 - eq = lambda x,y: x==y + eq = lambda x, y: x == y def f(d): try: d[2] = 77 @@ -339,7 +348,7 @@ assert res == 42 # "did not crash" def test_reversed_dict(self): - d1 = {2:3, 4:5, 6:7} + d1 = {2: 3, 4: 5, 6: 7} def g(): n1 = 0 for key in d1: @@ -358,7 +367,7 @@ pass def f(i): assert compute_hash(i) == compute_hash(42) - assert compute_hash(i+1.0) == compute_hash(43.0) + assert compute_hash(i + 1.0) == compute_hash(43.0) assert compute_hash("Hello" + str(i)) == compute_hash("Hello42") if i == 42: p = None @@ -373,13 +382,11 @@ assert compute_hash(INFINITY) == 314159 assert compute_hash(-INFINITY) == -271828 assert compute_hash(NAN) == 0 - return i*2 + return i * 2 res = self.interpret(f, [42]) assert res == 84 def test_isconstant(self): - from rpython.rlib.objectmodel import is_annotation_constant, specialize - @specialize.arg_or_var(0) def f(arg): if is_annotation_constant(arg): @@ -396,7 +403,7 @@ def f(): x = [1] y = ['b'] - objectmodel.keepalive_until_here(x,y) + objectmodel.keepalive_until_here(x, y) return 1 res = self.interpret(f, []) @@ -410,8 +417,8 @@ def f(i): assert compute_hash(None) == 0 assert compute_hash(i) == h_42 - assert compute_hash(i+1.0) == h_43_dot_0 - assert compute_hash((i+3)/6.0) == h_7_dot_5 + assert compute_hash(i + 1.0) == h_43_dot_0 + assert compute_hash((i + 3) / 6.0) == h_7_dot_5 assert compute_hash("Hello" + str(i)) == h_Hello42 if i == 42: p = None @@ -420,14 +427,14 @@ assert compute_hash(p) == h_None assert compute_hash(("world", None, i, 7.5)) == h_tuple assert compute_hash(q) == h_q - return i*2 - h_42 = compute_hash(42) + return i * 2 + h_42 = compute_hash(42) h_43_dot_0 = compute_hash(43.0) - h_7_dot_5 = compute_hash(7.5) - h_Hello42 = compute_hash("Hello42") - h_None = compute_hash(None) - h_tuple = compute_hash(("world", None, 42, 7.5)) - h_q = compute_hash(q) + h_7_dot_5 = compute_hash(7.5) + h_Hello42 = compute_hash("Hello42") + h_None = compute_hash(None) + h_tuple = compute_hash(("world", None, 42, 7.5)) + h_q = compute_hash(q) res = self.interpret(f, [42]) assert res == 84 @@ -466,18 +473,18 @@ @always_inline def f(a, b, c): return a, b, c - assert f._always_inline_ == True + assert f._always_inline_ is True def test_enforceargs_defaults(): @enforceargs(int, int) def f(a, b=40): - return a+b + return a + b assert f(2) == 42 def test_enforceargs_keywords(): @enforceargs(b=int) def f(a, b, c): - return a+b + return a + b assert f._annenforceargs_ == (None, int, None) def test_enforceargs_int_float_promotion(): @@ -511,7 +518,7 @@ def f(a, b, c): return a, b, c assert f._annenforceargs_ == (int, str, None) - assert f(1, 2, 3) == (1, 2, 3) # no typecheck + assert f(1, 2, 3) == (1, 2, 3) # no typecheck def test_enforceargs_translates(): from rpython.rtyper.lltypesystem import lltype @@ -539,39 +546,36 @@ def test_newlist(): - from rpython.annotator.model import SomeInteger def f(z): x = newlist_hint(sizehint=38) if z < 0: x.append(1) return len(x) - graph = getgraph(f, [SomeInteger()]) + graph = getgraph(f, [int]) for llop in graph.startblock.operations: if llop.opname == 'malloc_varsize': break assert llop.args[2].value == 38 def test_newlist_nonconst(): - from rpython.annotator.model import SomeInteger def f(z): x = newlist_hint(sizehint=z) return len(x) - graph = getgraph(f, [SomeInteger()]) + graph = getgraph(f, [int]) for llop in graph.startblock.operations: if llop.opname == 'malloc_varsize': break assert llop.args[2] is graph.startblock.inputargs[0] def test_resizelist_hint(): - from rpython.annotator.model import SomeInteger def f(z): x = [] resizelist_hint(x, 39) return len(x) - graph = getgraph(f, [SomeInteger()]) + graph = getgraph(f, [int]) for _, op in graph.iterblockops(): if op.opname == 'direct_call': break @@ -591,7 +595,7 @@ def test_iterkeys_with_hash(): def f(i): - d = {i+.0: 5, i+.5: 6} + d = {i + .0: 5, i + .5: 6} total = 0 for k, h in iterkeys_with_hash(d): total += k * h @@ -605,7 +609,7 @@ def test_iteritems_with_hash(): def f(i): - d = {i+.0: 5, i+.5: 6} + d = {i + .0: 5, i + .5: 6} total = 0 for k, v, h in iteritems_with_hash(d): total += k * h * v @@ -619,9 +623,9 @@ def test_contains_with_hash(): def f(i): - d = {i+.5: 5} - assert contains_with_hash(d, i+.5, compute_hash(i+.5)) - assert not contains_with_hash(d, i+.3, compute_hash(i+.3)) + d = {i + .5: 5} + assert contains_with_hash(d, i + .5, compute_hash(i + .5)) + assert not contains_with_hash(d, i + .3, compute_hash(i + .3)) return 0 f(29) @@ -630,9 +634,9 @@ def test_setitem_with_hash(): def f(i): d = {} - setitem_with_hash(d, i+.5, compute_hash(i+.5), 42) - setitem_with_hash(d, i+.6, compute_hash(i+.6), -612) - return d[i+.5] + setitem_with_hash(d, i + .5, compute_hash(i + .5), 42) + setitem_with_hash(d, i + .6, compute_hash(i + .6), -612) + return d[i + .5] assert f(29) == 42 res = interpret(f, [27]) @@ -640,8 +644,8 @@ def test_getitem_with_hash(): def f(i): - d = {i+.5: 42, i+.6: -612} - return getitem_with_hash(d, i+.5, compute_hash(i+.5)) + d = {i + .5: 42, i + .6: -612} + return getitem_with_hash(d, i + .5, compute_hash(i + .5)) assert f(29) == 42 res = interpret(f, [27]) @@ -649,10 +653,10 @@ def test_delitem_with_hash(): def f(i): - d = {i+.5: 42, i+.6: -612} - delitem_with_hash(d, i+.5, compute_hash(i+.5)) + d = {i + .5: 42, i + .6: -612} + delitem_with_hash(d, i + .5, compute_hash(i + .5)) try: - delitem_with_hash(d, i+.5, compute_hash(i+.5)) + delitem_with_hash(d, i + .5, compute_hash(i + .5)) except KeyError: pass else: @@ -682,39 +686,51 @@ def test_import_from_mixin(): class M: # old-style - def f(self): pass + def f(self): + pass class A: # old-style import_from_mixin(M) assert A.f.im_func is not M.f.im_func class M(object): - def f(self): pass + def f(self): + pass class A: # old-style import_from_mixin(M) assert A.f.im_func is not M.f.im_func class M: # old-style - def f(self): pass + def f(self): + pass class A(object): import_from_mixin(M) assert A.f.im_func is not M.f.im_func class M(object): - def f(self): pass + def f(self): + pass class A(object): import_from_mixin(M) assert A.f.im_func is not M.f.im_func class MBase(object): - a = 42; b = 43; c = 1000 - def f(self): return "hi" - def g(self): return self.c - 1 + a = 42 + b = 43 + c = 1000 + def f(self): + return "hi" + def g(self): + return self.c - 1 + class M(MBase): a = 84 - def f(self): return "there" + def f(self): + return "there" + class A(object): import_from_mixin(M) c = 88 + assert A.f.im_func is not M.f.im_func assert A.f.im_func is not MBase.f.im_func assert A.g.im_func is not MBase.g.im_func @@ -776,7 +792,6 @@ assert B._immutable_fields_ == ['b', 'a'] assert A._immutable_fields_ == ['a'] - class B(object): import_from_mixin(A) @@ -785,7 +800,6 @@ class C(A): _immutable_fields_ = ['c'] - class B(object): import_from_mixin(C) @@ -797,9 +811,9 @@ assert B._immutable_fields_ == ['b', 'c', 'a'] - class B(object): _immutable_fields_ = ['b'] + class BA(B): import_from_mixin(C) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit