[pypy-commit] pypy iterator-in-rpython: start limited iterators in RPython - tests
fijal
noreply at buildbot.pypy.org
Sun Jul 8 19:30:52 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: iterator-in-rpython
Changeset: r56001:01f79d0bf5a6
Date: 2012-07-08 19:09 +0200
http://bitbucket.org/pypy/pypy/changeset/01f79d0bf5a6/
Log: start limited iterators in RPython - tests
diff --git a/pypy/annotation/test/test_annrpython.py b/pypy/annotation/test/test_annrpython.py
--- a/pypy/annotation/test/test_annrpython.py
+++ b/pypy/annotation/test/test_annrpython.py
@@ -3793,7 +3793,18 @@
assert isinstance(s, annmodel.SomeString)
assert s.no_nul
-
+ def test_base_iter(self):
+ class A(object):
+ def __iter__(self):
+ return self
+
+ def fn():
+ return iter(A())
+
+ a = self.RPythonAnnotator()
+ s = a.build_types(fn, [])
+ assert isinstance(s, annmodel.SomeInstance)
+ assert s.classdef.name.endswith('.A')
def g(n):
return [0,1,2,n]
diff --git a/pypy/annotation/unaryop.py b/pypy/annotation/unaryop.py
--- a/pypy/annotation/unaryop.py
+++ b/pypy/annotation/unaryop.py
@@ -609,33 +609,36 @@
class __extend__(SomeInstance):
+ def _true_getattr(ins, attr):
+ if attr == '__class__':
+ return ins.classdef.read_attr__class__()
+ attrdef = ins.classdef.find_attribute(attr)
+ position = getbookkeeper().position_key
+ attrdef.read_locations[position] = True
+ s_result = attrdef.getvalue()
+ # hack: if s_result is a set of methods, discard the ones
+ # that can't possibly apply to an instance of ins.classdef.
+ # XXX do it more nicely
+ if isinstance(s_result, SomePBC):
+ s_result = ins.classdef.lookup_filter(s_result, attr,
+ ins.flags)
+ elif isinstance(s_result, SomeImpossibleValue):
+ ins.classdef.check_missing_attribute_update(attr)
+ # blocking is harmless if the attribute is explicitly listed
+ # in the class or a parent class.
+ for basedef in ins.classdef.getmro():
+ if basedef.classdesc.all_enforced_attrs is not None:
+ if attr in basedef.classdesc.all_enforced_attrs:
+ raise HarmlesslyBlocked("get enforced attr")
+ elif isinstance(s_result, SomeList):
+ s_result = ins.classdef.classdesc.maybe_return_immutable_list(
+ attr, s_result)
+ return s_result
+
def getattr(ins, s_attr):
if s_attr.is_constant() and isinstance(s_attr.const, str):
attr = s_attr.const
- if attr == '__class__':
- return ins.classdef.read_attr__class__()
- attrdef = ins.classdef.find_attribute(attr)
- position = getbookkeeper().position_key
- attrdef.read_locations[position] = True
- s_result = attrdef.getvalue()
- # hack: if s_result is a set of methods, discard the ones
- # that can't possibly apply to an instance of ins.classdef.
- # XXX do it more nicely
- if isinstance(s_result, SomePBC):
- s_result = ins.classdef.lookup_filter(s_result, attr,
- ins.flags)
- elif isinstance(s_result, SomeImpossibleValue):
- ins.classdef.check_missing_attribute_update(attr)
- # blocking is harmless if the attribute is explicitly listed
- # in the class or a parent class.
- for basedef in ins.classdef.getmro():
- if basedef.classdesc.all_enforced_attrs is not None:
- if attr in basedef.classdesc.all_enforced_attrs:
- raise HarmlesslyBlocked("get enforced attr")
- elif isinstance(s_result, SomeList):
- s_result = ins.classdef.classdesc.maybe_return_immutable_list(
- attr, s_result)
- return s_result
+ return ins._true_getattr(ins, s_attr)
return SomeObject()
getattr.can_only_throw = []
@@ -657,6 +660,9 @@
if not ins.can_be_None:
s.const = True
+ def iter(ins):
+ s_iterable = ins._true_getattr('__iter__')
+ return s_iterable.call(getbookkeeper().build_args("simple_call", []))
class __extend__(SomeBuiltin):
def _can_only_throw(bltn, *args):
diff --git a/pypy/rpython/test/test_rclass.py b/pypy/rpython/test/test_rclass.py
--- a/pypy/rpython/test/test_rclass.py
+++ b/pypy/rpython/test/test_rclass.py
@@ -958,6 +958,28 @@
found.append(op.args[1].value)
assert found == ['mutate_c']
+ def test_iter(self):
+ class Iterable(object):
+ def __init__(self):
+ self.counter = 0
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ if self.counter == 5:
+ raise StopIteration
+ self.counter += 1
+ return self.counter - 1
+
+ def f():
+ i = Iterable()
+ s = 0
+ for elem in i:
+ s += elem
+ return s
+
+ assert self.interpret(f, []) == f()
class TestLLtype(BaseTestRclass, LLRtypeMixin):
More information about the pypy-commit
mailing list