[pypy-commit] pypy string-NUL: (arigo, amaury) pass no_nul to SomeString constructor instead of changing

amauryfa noreply at buildbot.pypy.org
Mon Jan 30 22:58:29 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: string-NUL
Changeset: r51983:8fcec5fcbb57
Date: 2012-01-30 22:56 +0100
http://bitbucket.org/pypy/pypy/changeset/8fcec5fcbb57/

Log:	(arigo, amaury) pass no_nul to SomeString constructor instead of
	changing the property afterwards. + s_list_of_string is now a list
	of strings without NULs.

diff --git a/pypy/annotation/binaryop.py b/pypy/annotation/binaryop.py
--- a/pypy/annotation/binaryop.py
+++ b/pypy/annotation/binaryop.py
@@ -434,18 +434,15 @@
 class __extend__(pairtype(SomeString, SomeString)):
 
     def union((str1, str2)):
-        result = SomeString(can_be_None=str1.can_be_None or str2.can_be_None)
-        if str1.no_nul and str2.no_nul:
-            result.no_nul = True
-        return result
+        can_be_None = str1.can_be_None or str2.can_be_None
+        no_nul = str1.no_nul and str2.no_nul
+        return SomeString(can_be_None=can_be_None, no_nul=no_nul)
 
     def add((str1, str2)):
         # propagate const-ness to help getattr(obj, 'prefix' + const_name)
-        result = SomeString()
+        result = SomeString(no_nul=str1.no_nul and str2.no_nul)
         if str1.is_immutable_constant() and str2.is_immutable_constant():
             result.const = str1.const + str2.const
-        if str1.no_nul and str2.no_nul:
-            result.no_nul = True
         return result
 
 class __extend__(pairtype(SomeChar, SomeChar)):
@@ -480,7 +477,6 @@
                 raise NotImplementedError(
                     "string formatting mixing strings and unicode not supported")
         getbookkeeper().count('strformat', str, s_tuple)
-        result = SomeString()
         no_nul = str.no_nul
         for s_item in s_tuple.items:
             if isinstance(s_item, SomeFloat):
@@ -490,9 +486,7 @@
             else:
                 no_nul = False
                 break
-        if no_nul:
-            result.no_nul = True
-        return result
+        return SomeString(no_nul=no_nul)
 
 
 class __extend__(pairtype(SomeString, SomeObject)):
diff --git a/pypy/annotation/bookkeeper.py b/pypy/annotation/bookkeeper.py
--- a/pypy/annotation/bookkeeper.py
+++ b/pypy/annotation/bookkeeper.py
@@ -342,14 +342,11 @@
             else:
                 raise Exception("seeing a prebuilt long (value %s)" % hex(x))
         elif issubclass(tp, str): # py.lib uses annotated str subclasses
+            no_nul = not '\x00' in x
             if len(x) == 1:
-                result = SomeChar()
-                if not '\x00' in x:
-                    result.no_nul = True
+                result = SomeChar(no_nul=no_nul)
             else:
-                result = SomeString()
-                if not '\x00' in x:
-                    result.no_nul = True
+                result = SomeString(no_nul=no_nul)
         elif tp is unicode:
             if len(x) == 1:
                 result = SomeUnicodeCodePoint()
diff --git a/pypy/annotation/listdef.py b/pypy/annotation/listdef.py
--- a/pypy/annotation/listdef.py
+++ b/pypy/annotation/listdef.py
@@ -223,4 +223,5 @@
 
 MOST_GENERAL_LISTDEF = ListDef(None, SomeObject())
 
-s_list_of_strings = SomeList(ListDef(None, SomeString(), resized = True))
+s_list_of_strings = SomeList(ListDef(None, SomeString(no_nul=True),
+                                     resized = True))
diff --git a/pypy/annotation/model.py b/pypy/annotation/model.py
--- a/pypy/annotation/model.py
+++ b/pypy/annotation/model.py
@@ -232,26 +232,30 @@
     can_be_None=False
     no_nul = False  # No NUL character in the string.
 
-    def __init__(self, can_be_None=False):
+    def __init__(self, can_be_None=False, no_nul=False):
         if can_be_None:
             self.can_be_None = True
+        if no_nul:
+            self.no_nul = True
 
     def can_be_none(self):
         return self.can_be_None
 
     def nonnoneify(self):
-        result = SomeString(can_be_None=False)
-        if self.no_nul:
-            result.no_nul = True
-        return result
+        return SomeString(can_be_None=False, no_nul=self.no_nul)
 
 class SomeUnicodeString(SomeObject):
     "Stands for an object which is known to be an unicode string"
     knowntype = unicode
     immutable = True
+    can_be_None=False
     no_nul = False
-    def __init__(self, can_be_None=False):
-        self.can_be_None = can_be_None
+
+    def __init__(self, can_be_None=False, no_nul=False):
+        if can_be_None:
+            self.can_be_None = True
+        if no_nul:
+            self.no_nul = True
 
     def can_be_none(self):
         return self.can_be_None
@@ -262,14 +266,16 @@
 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
+    def __init__(self, no_nul=False):    # no 'can_be_None' argument here
+        if no_nul:
+            self.no_nul = True
 
 class SomeUnicodeCodePoint(SomeUnicodeString):
     "Stands for an object known to be a unicode codepoint."
     can_be_None = False
-    def __init__(self):    # no 'can_be_None' argument here
-        pass
+    def __init__(self, no_nul=False):    # no 'can_be_None' argument here
+        if no_nul:
+            self.no_nul = True
 
 SomeString.basestringclass = SomeString
 SomeString.basecharclass = SomeChar
@@ -510,8 +516,7 @@
 s_None = SomePBC([], can_be_None=True)
 s_Bool = SomeBool()
 s_ImpossibleValue = SomeImpossibleValue()
-s_Str0 = SomeString()
-s_Str0.no_nul = True
+s_Str0 = SomeString(no_nul=True)
 
 # ____________________________________________________________
 # weakrefs
diff --git a/pypy/annotation/unaryop.py b/pypy/annotation/unaryop.py
--- a/pypy/annotation/unaryop.py
+++ b/pypy/annotation/unaryop.py
@@ -491,10 +491,8 @@
             if isinstance(str, SomeUnicodeString):
                 return immutablevalue(u"")
             return immutablevalue("")
-        result = str.basestringclass()
-        if str.no_nul and s_item.no_nul:
-            result.no_nul = True
-        return result
+        no_nul = str.no_nul and s_item.no_nul
+        return str.basestringclass(no_nul=no_nul)
 
     def iter(str):
         return SomeIterator(str)
@@ -505,27 +503,20 @@
 
     def method_split(str, patt, max=-1):
         getbookkeeper().count("str_split", str, patt)
-        s_item = str.basestringclass()
-        if str.no_nul:
-            s_item.no_nul = True
-        result = getbookkeeper().newlist(s_item)
-        return result
+        s_item = str.basestringclass(no_nul=str.no_nul)
+        return getbookkeeper().newlist(s_item)
 
     def method_rsplit(str, patt, max=-1):
         getbookkeeper().count("str_rsplit", str, patt)
-        result = getbookkeeper().newlist(str.basestringclass())
-        if str.no_nul:
-            result.no_nul = True
-        return result
+        s_item = str.basestringclass(no_nul=str.no_nul)
+        return getbookkeeper().newlist(s_item)
 
     def method_replace(str, s1, s2):
         return str.basestringclass()
 
     def getslice(str, s_start, s_stop):
         check_negative_slice(s_start, s_stop)
-        result = str.basestringclass()
-        if str.no_nul:
-            result.no_nul = True
+        result = str.basestringclass(no_nul=str.no_nul)
         return result
 
 class __extend__(SomeUnicodeString):
diff --git a/pypy/rpython/module/ll_os.py b/pypy/rpython/module/ll_os.py
--- a/pypy/rpython/module/ll_os.py
+++ b/pypy/rpython/module/ll_os.py
@@ -31,11 +31,9 @@
 from pypy.rlib import rgc
 from pypy.rlib.objectmodel import specialize
 
-str0 = SomeString()
-str0.no_nul = True
+str0 = SomeString(no_nul=True)
+unicode0 = SomeUnicodeString(no_nul=True)
 
-unicode0 = SomeUnicodeString()
-unicode0.no_nul = True
 
 def monkeypatch_rposix(posixfunc, unicodefunc, signature):
     func_name = posixfunc.__name__
diff --git a/pypy/translator/cli/test/runtest.py b/pypy/translator/cli/test/runtest.py
--- a/pypy/translator/cli/test/runtest.py
+++ b/pypy/translator/cli/test/runtest.py
@@ -276,10 +276,7 @@
 
 def get_annotation(x):
     if isinstance(x, basestring) and len(x) > 1:
-        result = SomeString()
-        if '\x00' not in x:
-            result.no_nul = True
-        return result
+        return SomeString(no_nul='\x00' not in x)
     else:
         return lltype_to_annotation(typeOf(x))
 
diff --git a/pypy/translator/driver.py b/pypy/translator/driver.py
--- a/pypy/translator/driver.py
+++ b/pypy/translator/driver.py
@@ -185,7 +185,7 @@
 
         if standalone:
             # the 'argv' parameter
-            inputtypes = [[annmodel.s_Str0]]
+            inputtypes = [s_list_of_strings]
         self.inputtypes = inputtypes
 
         if policy is None:


More information about the pypy-commit mailing list