[pypy-svn] r75784 - in pypy/trunk/pypy: annotation module/marshal module/termios objspace/std rpython rpython/module rpython/test

arigo at codespeak.net arigo at codespeak.net
Fri Jul 2 19:01:08 CEST 2010


Author: arigo
Date: Fri Jul  2 19:01:06 2010
New Revision: 75784

Modified:
   pypy/trunk/pypy/annotation/model.py
   pypy/trunk/pypy/annotation/unaryop.py
   pypy/trunk/pypy/module/marshal/interp_marshal.py
   pypy/trunk/pypy/module/termios/interp_termios.py
   pypy/trunk/pypy/objspace/std/stringobject.py
   pypy/trunk/pypy/rpython/module/ll_termios.py
   pypy/trunk/pypy/rpython/rstr.py
   pypy/trunk/pypy/rpython/test/test_rstr.py
Log:
Remove the logic to support 'ord(string)' in RPython, only keeping 'ord(char)'.
The issue was that the implementation of the former was just 'ord(string[0])',
succeeding with strange results if the string is actually of length != 1 at
runtime.

Fix a few places in pypy that use this.


Modified: pypy/trunk/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/pypy/annotation/model.py	(original)
+++ pypy/trunk/pypy/annotation/model.py	Fri Jul  2 19:01:06 2010
@@ -229,11 +229,15 @@
 
 class SomeChar(SomeString):
     "Stands for an object known to be a string of length 1."
+    can_be_None = False
+    def __init__(self):    # no 'can_be_None' argument here
+        pass
 
 class SomeUnicodeCodePoint(SomeUnicodeString):
     "Stands for an object known to be a unicode codepoint."
-    def can_be_none(self):
-        return False
+    can_be_None = False
+    def __init__(self):    # no 'can_be_None' argument here
+        pass
 
 SomeString.basestringclass = SomeString
 SomeString.basecharclass = SomeChar

Modified: pypy/trunk/pypy/annotation/unaryop.py
==============================================================================
--- pypy/trunk/pypy/annotation/unaryop.py	(original)
+++ pypy/trunk/pypy/annotation/unaryop.py	Fri Jul  2 19:01:06 2010
@@ -5,7 +5,7 @@
 from types import MethodType
 from pypy.annotation.model import \
      SomeObject, SomeInteger, SomeBool, SomeString, SomeChar, SomeList, \
-     SomeDict, SomeTuple, SomeImpossibleValue, \
+     SomeDict, SomeTuple, SomeImpossibleValue, SomeUnicodeCodePoint, \
      SomeInstance, SomeBuiltin, SomeFloat, SomeIterator, SomePBC, \
      SomeExternalObject, SomeTypedAddressAccess, SomeAddress, \
      s_ImpossibleValue, s_Bool, s_None, \
@@ -494,9 +494,6 @@
     def getanyitem(str):
         return str.basecharclass()
 
-    def ord(str):
-        return SomeInteger(nonneg=True)
-
     def method_split(str, patt): # XXX
         getbookkeeper().count("str_split", str, patt)
         return getbookkeeper().newlist(str.basestringclass())
@@ -541,11 +538,16 @@
         return SomeUnicodeString()
     method_decode.can_only_throw = [UnicodeDecodeError]
 
-class __extend__(SomeChar):
+class __extend__(SomeChar, SomeUnicodeCodePoint):
 
     def len(chr):
         return immutablevalue(1)
 
+    def ord(str):
+        return SomeInteger(nonneg=True)
+
+class __extend__(SomeChar):
+
     def method_isspace(chr):
         return s_Bool
 

Modified: pypy/trunk/pypy/module/marshal/interp_marshal.py
==============================================================================
--- pypy/trunk/pypy/module/marshal/interp_marshal.py	(original)
+++ pypy/trunk/pypy/module/marshal/interp_marshal.py	Fri Jul  2 19:01:06 2010
@@ -365,8 +365,8 @@
         return self.reader.read(n)
 
     def get1(self):
-        # convince typer to use a char
-        return chr(ord(self.get(1)))
+        # the [0] is used to convince the annotator to return a char
+        return self.get(1)[0]
 
     def atom_str(self, typecode):
         self.start(typecode)

Modified: pypy/trunk/pypy/module/termios/interp_termios.py
==============================================================================
--- pypy/trunk/pypy/module/termios/interp_termios.py	(original)
+++ pypy/trunk/pypy/module/termios/interp_termios.py	Fri Jul  2 19:01:06 2010
@@ -60,8 +60,8 @@
     # last one need to be chosen carefully
     cc_w = [space.wrap(i) for i in cc]
     if lflag & termios.ICANON:
-        cc_w[termios.VMIN] = space.wrap(ord(cc[termios.VMIN]))
-        cc_w[termios.VTIME] = space.wrap(ord(cc[termios.VTIME]))
+        cc_w[termios.VMIN] = space.wrap(ord(cc[termios.VMIN][0]))
+        cc_w[termios.VTIME] = space.wrap(ord(cc[termios.VTIME][0]))
     w_cc = space.newlist(cc_w)
     l_w.append(w_cc)
     return space.newlist(l_w)

Modified: pypy/trunk/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/stringobject.py	Fri Jul  2 19:01:06 2010
@@ -863,7 +863,7 @@
             space.w_TypeError,
             "ord() expected a character, but string "
             "of length %d found", len(u_str))
-    return space.wrap(ord(u_str))
+    return space.wrap(ord(u_str[0]))
 
 def getnewargs__String(space, w_str):
     return space.newtuple([wrapstr(space, w_str._value)])

Modified: pypy/trunk/pypy/rpython/module/ll_termios.py
==============================================================================
--- pypy/trunk/pypy/rpython/module/ll_termios.py	(original)
+++ pypy/trunk/pypy/rpython/module/ll_termios.py	Fri Jul  2 19:01:06 2010
@@ -85,7 +85,7 @@
     c_struct.c_c_lflag, ispeed, ospeed, cc = attributes
     try:
         for i in range(NCCS):
-            c_struct.c_c_cc[i] = rffi.r_uchar(ord(cc[i]))
+            c_struct.c_c_cc[i] = rffi.r_uchar(ord(cc[i][0]))
         error = c_cfsetispeed(c_struct, ispeed)
         if error == -1:
             raise termios.error(error, 'tcsetattr failed')

Modified: pypy/trunk/pypy/rpython/rstr.py
==============================================================================
--- pypy/trunk/pypy/rpython/rstr.py	(original)
+++ pypy/trunk/pypy/rpython/rstr.py	Fri Jul  2 19:01:06 2010
@@ -80,17 +80,6 @@
             # defaults to checking the length
             return super(AbstractStringRepr, self).rtype_is_true(hop)
 
-    def rtype_ord(self, hop):
-        string_repr = hop.args_r[0].repr
-        v_str, = hop.inputargs(string_repr)
-        c_zero = inputconst(Signed, 0)
-        v_chr = hop.gendirectcall(self.ll.ll_stritem_nonneg, v_str, c_zero)
-        if string_repr is hop.rtyper.type_system.rstr.string_repr:
-            return hop.genop('cast_char_to_int', [v_chr], resulttype=Signed)
-        else:
-            assert string_repr is hop.rtyper.type_system.rstr.unicode_repr
-            return hop.genop('cast_unichar_to_int', [v_chr], resulttype=Signed)
-
     def rtype_method_startswith(self, hop):
         str1_repr, str2_repr = self._str_reprs(hop)
         v_str, v_value = hop.inputargs(str1_repr, str2_repr)

Modified: pypy/trunk/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/trunk/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/trunk/pypy/rpython/test/test_rstr.py	Fri Jul  2 19:01:06 2010
@@ -131,7 +131,7 @@
             s = c * mul
             res = 0
             for i in range(len(s)):
-                res = res*10 + ord(const(s[i])) - ord(const('0'))
+                res = res*10 + ord(const(s[i])[0]) - ord(const('0')[0])
             c2 = c
             c2 *= mul
             res = 10 * res + (c2 == s)
@@ -577,7 +577,7 @@
             sum = 0
             for num in l:
                  if len(num):
-                     sum += ord(num) - ord(const('0'))
+                     sum += ord(num[0]) - ord(const('0')[0])
             return sum + len(l) * 100
         for i in range(5):
             res = self.interpret(fn, [i])



More information about the Pypy-commit mailing list