[pypy-svn] r18578 - in pypy/dist/pypy: objspace/flow rpython rpython/test translator/c/test
afa at codespeak.net
afa at codespeak.net
Fri Oct 14 18:40:34 CEST 2005
Author: afa
Date: Fri Oct 14 18:40:31 2005
New Revision: 18578
Modified:
pypy/dist/pypy/objspace/flow/objspace.py
pypy/dist/pypy/rpython/rint.py
pypy/dist/pypy/rpython/test/test_rbuiltin.py
pypy/dist/pypy/translator/c/test/test_annotated.py
Log:
valentino, afa: chr(256) should raise an error.
pypy silently wrapped around values.
Modified: pypy/dist/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/objspace.py (original)
+++ pypy/dist/pypy/objspace/flow/objspace.py Fri Oct 14 18:40:31 2005
@@ -436,6 +436,7 @@
implicit_exceptions = {
int: [ValueError], # built-ins that can always raise exceptions
+ chr: [ValueError],
}
def _add_exceptions(names, exc):
Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py (original)
+++ pypy/dist/pypy/rpython/rint.py Fri Oct 14 18:40:31 2005
@@ -213,6 +213,9 @@
def rtype_chr(_, hop):
vlist = hop.inputargs(Signed)
+ if hop.has_implicit_exception(ValueError):
+ hop.exception_is_here()
+ hop.gendirectcall(ll_check_chr, vlist[0])
return hop.genop('cast_int_to_char', vlist, resulttype=Char)
def rtype_unichr(_, hop):
@@ -405,6 +408,12 @@
def ll_hash_int(n):
return n
+def ll_check_chr(n):
+ if 0 <= n <= 255:
+ return
+ else:
+ raise ValueError
+
#
# _________________________ Conversions _________________________
Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py (original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py Fri Oct 14 18:40:31 2005
@@ -276,3 +276,16 @@
return x
res = interpret(f, [0])
assert res == 3
+
+def test_chr():
+ def f(x=int):
+ try:
+ return chr(x)
+ except ValueError:
+ return '?'
+ res = interpret(f, [65])
+ assert res == 'A'
+ res = interpret(f, [256])
+ assert res == '?'
+ res = interpret(f, [-1])
+ assert res == '?'
Modified: pypy/dist/pypy/translator/c/test/test_annotated.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_annotated.py (original)
+++ pypy/dist/pypy/translator/c/test/test_annotated.py Fri Oct 14 18:40:31 2005
@@ -177,3 +177,14 @@
assert fn(2) == 0
py.test.raises(MemoryError, fn, sys.maxint//2+1)
py.test.raises(MemoryError, fn, sys.maxint)
+
+ def test_chr(self):
+ def f(x=int):
+ try:
+ return 'Yes ' + chr(x)
+ except ValueError:
+ return 'No'
+ fn = self.getcompiled(f)
+ assert fn(65) == 'Yes A'
+ assert fn(256) == 'No'
+ assert fn(-1) == 'No'
More information about the Pypy-commit
mailing list