[pypy-svn] r27324 - in pypy/dist/pypy: annotation rpython rpython/ootypesystem rpython/ootypesystem/test rpython/test
antocuni at codespeak.net
antocuni at codespeak.net
Wed May 17 09:25:58 CEST 2006
Author: antocuni
Date: Wed May 17 09:25:53 2006
New Revision: 27324
Modified:
pypy/dist/pypy/annotation/builtin.py
pypy/dist/pypy/rpython/llinterp.py
pypy/dist/pypy/rpython/ootypesystem/ootype.py
pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py
pypy/dist/pypy/rpython/ootypesystem/rstr.py
pypy/dist/pypy/rpython/ootypesystem/test/test_ooann.py
pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
pypy/dist/pypy/rpython/test/test_rstr.py
Log:
Added support for string to integer conversion to ootypesystem.
Now ootypesystem string support should be complete! :-)
Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py (original)
+++ pypy/dist/pypy/annotation/builtin.py Wed May 17 09:25:53 2006
@@ -526,6 +526,11 @@
assert isinstance(base, SomeInteger)
return SomeOOInstance(ootype.String)
+def ooparse_int(s, base):
+ assert isinstance(s, SomeOOInstance) and s.ootype is ootype.String
+ assert isinstance(base, SomeInteger)
+ return SomeInteger()
+
BUILTIN_ANALYZERS[ootype.instanceof] = instanceof
BUILTIN_ANALYZERS[ootype.new] = new
BUILTIN_ANALYZERS[ootype.null] = null
@@ -534,6 +539,7 @@
BUILTIN_ANALYZERS[ootype.subclassof] = subclassof
BUILTIN_ANALYZERS[ootype.ooidentityhash] = ooidentityhash
BUILTIN_ANALYZERS[ootype.oostring] = oostring
+BUILTIN_ANALYZERS[ootype.ooparse_int] = ooparse_int
#________________________________
# non-gc objects
Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py (original)
+++ pypy/dist/pypy/rpython/llinterp.py Wed May 17 09:25:53 2006
@@ -1023,6 +1023,12 @@
def op_oostring(self, obj, base):
return ootype.oostring(obj, base)
+ def op_ooparse_int(self, s, base):
+ try:
+ return ootype.ooparse_int(s, base)
+ except ValueError:
+ self.make_llexception()
+
class Tracer(object):
Counter = 0
file = None
Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py Wed May 17 09:25:53 2006
@@ -1175,6 +1175,9 @@
obj = '<%s object>' % obj._inst._TYPE._name
return make_string(str(obj))
+def ooparse_int(s, base):
+ return int(s._str, base)
+
def setItemType(LIST, ITEMTYPE):
return LIST._set_itemtype(ITEMTYPE)
Modified: pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py Wed May 17 09:25:53 2006
@@ -65,6 +65,13 @@
assert isinstance(hop.args_s[1], annmodel.SomeInteger)
return hop.genop('oostring', hop.args_v, resulttype = ootype.String)
+def rtype_ooparse_int(hop):
+ assert isinstance(hop.args_s[0], annmodel.SomeOOInstance)\
+ and hop.args_s[0].ootype is ootype.String
+ assert isinstance(hop.args_s[1], annmodel.SomeInteger)
+ hop.has_implicit_exception(ValueError)
+ hop.exception_is_here()
+ return hop.genop('ooparse_int', hop.args_v, resulttype = ootype.Signed)
BUILTIN_TYPER = {}
BUILTIN_TYPER[ootype.new] = rtype_new
@@ -75,3 +82,4 @@
BUILTIN_TYPER[ootype.ooidentityhash] = rtype_ooidentityhash
BUILTIN_TYPER[isinstance] = rtype_builtin_isinstance
BUILTIN_TYPER[ootype.oostring] = rtype_oostring
+BUILTIN_TYPER[ootype.ooparse_int] = rtype_ooparse_int
Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py Wed May 17 09:25:53 2006
@@ -150,6 +150,9 @@
def ll_split_chr(LIST, s, c):
return s.ll_split_chr(c)
+ def ll_int(s, base):
+ return ootype.ooparse_int(s, base)
+
def do_stringformat(cls, hop, sourcevarsrepr):
InstanceRepr = hop.rtyper.type_system.rclass.InstanceRepr
string_repr = hop.rtyper.type_system.rstr.string_repr
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_ooann.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_ooann.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_ooann.py Wed May 17 09:25:53 2006
@@ -226,3 +226,11 @@
a = RPythonAnnotator()
s = a.build_types(oof, [])
assert isinstance(s, annmodel.SomeBuiltin)
+
+def test_ooparse_int():
+ def oof(n, b):
+ return ooparse_int(oostring(n, b), b)
+
+ a = RPythonAnnotator()
+ s = a.build_types(oof, [int, int])
+ assert isinstance(s, annmodel.SomeInteger)
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py Wed May 17 09:25:53 2006
@@ -198,8 +198,11 @@
assert interpret(oof, [s], type_system='ootype') == 3
def test_oostring():
+ def const(c):
+ return c
+
def oof(ch):
- return oostring(ch)
+ return oostring(ch, const(-1))
ch = 'a'
res = interpret(oof, [ch], type_system='ootype')
@@ -220,3 +223,11 @@
assert b
interpret(oof, [True], type_system='ootype')
+
+def test_ooparse_int():
+ def oof(n, b):
+ return ooparse_int(oostring(n, b), b)
+
+ for n in -42, 0, 42:
+ for b in 8, 10, 16:
+ assert interpret(oof, [n, b], type_system='ootype') == n
Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py (original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py Wed May 17 09:25:53 2006
@@ -519,49 +519,49 @@
s = s.replace('abb', 'c')
raises (TyperError, interpret, fn, ())
-def test_int():
- s1 = [ '42', '01001', 'abc', 'ABC', '4aBc', ' 12ef ', '+42', 'foo', '42foo', '42.1', '']
- def fn(i, base):
- s = s1[i]
- res = int(s, base)
- return res
- for j in (10, 16, 2, 1, 36, 42, -3):
- for i in range(len(s1)):
+ def test_int(self):
+ s1 = [ '42', '01001', 'abc', 'ABC', '4aBc', ' 12ef ', '+42', 'foo', '42foo', '42.1', '']
+ def fn(i, base):
+ s = s1[i]
+ res = int(s, base)
+ return res
+ for j in (10, 16, 2, 1, 36, 42, -3):
+ for i in range(len(s1)):
+ try:
+ expected = fn(i, j)
+ except ValueError:
+ self.interpret_raises(ValueError, fn, [i, j])
+ else:
+ res = self.interpret(fn, [i, j])
+ assert res == expected
+
+ def test_int_valueerror(self):
+ s1 = ['42g', '?']
+ def fn(i):
try:
- expected = fn(i, j)
+ return int(s1[i])
except ValueError:
- interpret_raises(ValueError, fn, [i, j])
- else:
- res = interpret(fn, [i, j])
- assert res == expected
+ return -654
+ res = self.interpret(fn, [0])
+ assert res == -654
+ res = self.interpret(fn, [1])
+ assert res == -654
-def test_int_valueerror():
- s1 = ['42g', '?']
- def fn(i):
- try:
- return int(s1[i])
- except ValueError:
- return -654
- res = interpret(fn, [0])
- assert res == -654
- res = interpret(fn, [1])
- assert res == -654
-
-def test_char_mul_n():
- def f(c, n):
- return c*n
- res = interpret(f, ['a', 4])
- assert ''.join(res.chars) == 'a'*4
- res = interpret(f, ['a', 0])
- assert ''.join(res.chars) == ""
-
-def test_n_mul_char():
- def f(c, n):
- return n*c
- res = interpret(f, ['a', 4])
- assert ''.join(res.chars) == 'a'*4
- res = interpret(f, ['a', 0])
- assert ''.join(res.chars) == ""
+ def test_char_mul_n(self):
+ def f(c, n):
+ return c*n
+ res = self.interpret(f, ['a', 4])
+ assert self.ll_to_string(res) == 'a'*4
+ res = self.interpret(f, ['a', 0])
+ assert self.ll_to_string(res) == ""
+
+ def test_n_mul_char(self):
+ def f(c, n):
+ return n*c
+ res = self.interpret(f, ['a', 4])
+ assert self.ll_to_string(res) == 'a'*4
+ res = self.interpret(f, ['a', 0])
+ assert self.ll_to_string(res) == ""
def FIXME_test_str_to_pystringobj():
def f(n):
More information about the Pypy-commit
mailing list