Author: Alex Gaynor <[email protected]>
Branch: 
Changeset: r63704:2523e1972966
Date: 2013-04-27 13:11 -0700
http://bitbucket.org/pypy/pypy/changeset/2523e1972966/

Log:    Added str.isalnum for strings (it already existed on chars)

diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -541,10 +541,13 @@
 
 
 class __extend__(SomeString):
-    def method_isdigit(chr):
+    def method_isdigit(str):
         return s_Bool
 
-    def method_isalpha(chr):
+    def method_isalpha(str):
+        return s_Bool
+
+    def method_isalnum(str):
         return s_Bool
 
     def method_upper(str):
diff --git a/rpython/rtyper/rstr.py b/rpython/rtyper/rstr.py
--- a/rpython/rtyper/rstr.py
+++ b/rpython/rtyper/rstr.py
@@ -259,6 +259,12 @@
         hop.exception_cannot_occur()
         return hop.gendirectcall(self.ll.ll_isalpha, v_str)
 
+    def rtype_method_isalnum(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_isalnum, 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."""
@@ -779,6 +785,17 @@
                 return False
         return True
 
+    def ll_isalnum(s):
+        from rpython.rtyper.annlowlevel import hlstr
+
+        s = hlstr(s)
+        if not s:
+            return False
+        for ch in s:
+            if not ch.isalnum():
+                return False
+        return True
+
     def ll_char_isspace(ch):
         c = ord(ch)
         return c == 32 or (9 <= c <= 13)   # c in (9, 10, 11, 12, 13, 32)
diff --git a/rpython/rtyper/test/test_rstr.py b/rpython/rtyper/test/test_rstr.py
--- a/rpython/rtyper/test/test_rstr.py
+++ b/rpython/rtyper/test/test_rstr.py
@@ -156,6 +156,15 @@
         for i in xrange(3):
             assert self.interpret(fn, [i]) == fn(i)
 
+    def test_str_isalnum(self):
+        const = self.const
+
+        def fn(i):
+            consts = [const(''), const('abc'), const('abc123'), 
const('abc123!')]
+            return consts[i].isalnum()
+        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

Reply via email to