Author: Alex Gaynor <[email protected]>
Branch:
Changeset: r59431:ff3a90231b35
Date: 2012-12-14 20:15 -0800
http://bitbucket.org/pypy/pypy/changeset/ff3a90231b35/
Log: support isalpha on strings
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
@@ -255,13 +255,6 @@
assert getcdef(snippet.H).about_attribute('attr') == (
a.bookkeeper.immutablevalue(1))
- def DISABLED_test_knownkeysdict(self):
- # disabled, SomeDict() is now a general {s_key: s_value} dict
- a = self.RPythonAnnotator()
- s = a.build_types(snippet.knownkeysdict, [int])
- # result should be an integer
- assert s.knowntype == int
-
def test_generaldict(self):
a = self.RPythonAnnotator()
s = a.build_types(snippet.generaldict, [str, int, str, int])
@@ -483,6 +476,13 @@
s = a.build_types(f, [str])
assert isinstance(s, annmodel.SomeString)
+ def test_str_isalpha(self):
+ def f(s):
+ return s.isalpha()
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [str])
+ assert isinstance(s, annmodel.SomeBool)
+
def test_simple_slicing(self):
a = self.RPythonAnnotator()
s = a.build_types(snippet.simple_slice, [somelist(annmodel.s_Int)])
diff --git a/pypy/annotation/unaryop.py b/pypy/annotation/unaryop.py
--- a/pypy/annotation/unaryop.py
+++ b/pypy/annotation/unaryop.py
@@ -526,7 +526,11 @@
return SomeString()
method_encode.can_only_throw = [UnicodeEncodeError]
+
class __extend__(SomeString):
+ def method_isalpha(chr):
+ return s_Bool
+
def method_upper(str):
return SomeString()
diff --git a/pypy/rpython/rstr.py b/pypy/rpython/rstr.py
--- a/pypy/rpython/rstr.py
+++ b/pypy/rpython/rstr.py
@@ -249,6 +249,12 @@
hop.exception_cannot_occur()
return hop.gendirectcall(self.ll.ll_lower, v_str)
+ def rtype_method_isalpha(self, hop):
+ string_repr = hop.args_r[0].repr
+ [v_str] = hop.inputargs(string_repr)
+ hop.exception_cannot_occur()
+ return hop.gendirectcall(self.ll.ll_isalpha, v_str)
+
def _list_length_items(self, hop, v_lst, LIST):
"""Return two Variables containing the length and items of a
list. Need to be overriden because it is typesystem-specific."""
@@ -746,6 +752,17 @@
c = ord(ch)
return c <= 57 and c >= 48
+ def ll_isalpha(s):
+ from pypy.rpython.annlowlevel import hlstr
+
+ s = hlstr(s)
+ if not s:
+ return False
+ for ch in s:
+ if not ch.isalpha():
+ return False
+ return True
+
def ll_char_isalpha(ch):
c = ord(ch)
if c >= 97:
diff --git a/pypy/rpython/test/test_rstr.py b/pypy/rpython/test/test_rstr.py
--- a/pypy/rpython/test/test_rstr.py
+++ b/pypy/rpython/test/test_rstr.py
@@ -138,6 +138,15 @@
res = self.interpret(fn, [ch])
assert res == fn(ch)
+ def test_str_isalpha(self):
+ const = self.const
+
+ def fn(i):
+ consts = [const(''), const('anc'), const('abc123')]
+ return consts[i].isalpha()
+ for i in xrange(3):
+ assert self.interpret(fn, [i]) == fn(i)
+
def test_char_compare(self):
const = self.const
res = self.interpret(lambda c1, c2: c1 == c2, [const('a'),
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit