[pypy-svn] r5268 - in pypy/trunk/src/pypy: annotation translator/test
arigo at codespeak.net
arigo at codespeak.net
Thu Jun 24 16:44:49 CEST 2004
Author: arigo
Date: Thu Jun 24 16:44:48 2004
New Revision: 5268
Modified:
pypy/trunk/src/pypy/annotation/factory.py
pypy/trunk/src/pypy/annotation/model.py
pypy/trunk/src/pypy/annotation/unaryop.py
pypy/trunk/src/pypy/translator/test/snippet.py
pypy/trunk/src/pypy/translator/test/test_annrpython.py
Log:
Flow annotations inside __init__() constructors.
Modified: pypy/trunk/src/pypy/annotation/factory.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/factory.py (original)
+++ pypy/trunk/src/pypy/annotation/factory.py Thu Jun 24 16:44:48 2004
@@ -132,12 +132,19 @@
return self.bookkeeper.annotator.recursivecall(func, arglist, self)
-class InstanceFactory:
+class InstanceFactory(FuncCallFactory):
- def create(self, cls):
+ def create(self, cls, arglist):
classdef = self.bookkeeper.getclassdef(cls)
classdef.instancefactories[self] = True
- return SomeInstance(classdef)
+ s_instance = SomeInstance(classdef)
+ # flow into __init__() if the class has got one
+ init = getattr(cls, '__init__', None)
+ if init is not None and init != object.__init__:
+ self.pycall(init, [s_instance] + arglist)
+ else:
+ assert not arglist, "no __init__ found in %r" % (cls,)
+ return s_instance
class ClassDef:
Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py (original)
+++ pypy/trunk/src/pypy/annotation/model.py Thu Jun 24 16:44:48 2004
@@ -162,13 +162,13 @@
def valueoftype(t):
"The most precise SomeValue instance that contains all objects of type t."
- if isinstance(bool, type) and issubclass(t, bool):
+ if t is bool:
return SomeBool()
- elif issubclass(t, int):
+ elif t is int:
return SomeInteger()
- elif issubclass(t, str):
+ elif t is str:
return SomeString()
- elif issubclass(t, list):
+ elif t is list:
return SomeList(factories={})
else:
return SomeObject()
Modified: pypy/trunk/src/pypy/annotation/unaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/unaryop.py (original)
+++ pypy/trunk/src/pypy/annotation/unaryop.py Thu Jun 24 16:44:48 2004
@@ -130,9 +130,10 @@
class __extend__(SomeClass):
def call(cls, args, kwds):
- # XXX flow into __init__
+ arglist = decode_simple_call(args, kwds)
+ assert arglist is not None
factory = getbookkeeper().getfactory(InstanceFactory)
- return factory.create(cls.cls)
+ return factory.create(cls.cls, arglist)
class __extend__(SomeFunction):
Modified: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/snippet.py (original)
+++ pypy/trunk/src/pypy/translator/test/snippet.py Thu Jun 24 16:44:48 2004
@@ -324,11 +324,19 @@
def my_method(self):
return self.my_attribute
+class WithInit:
+ def __init__(self, n):
+ self.a = n
+
def simple_method(v=anytype):
z = Z()
z.my_attribute = v
return z.my_method()
+def with_init(v=int):
+ z = WithInit(v)
+ return z.a
+
def powerset(setsize=int):
"""Powerset
Modified: pypy/trunk/src/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_annrpython.py (original)
+++ pypy/trunk/src/pypy/translator/test/test_annrpython.py Thu Jun 24 16:44:48 2004
@@ -211,6 +211,12 @@
# result should be a built-in method
self.assert_(isinstance(s, annmodel.SomeBuiltin))
+ def test_with_init(self):
+ a = RPythonAnnotator()
+ s = a.build_types(snippet.with_init, [int])
+ # result should be an integer
+ self.assertEquals(s.knowntype, int)
+
def g(n):
return [0,1,2,n]
More information about the Pypy-commit
mailing list