Author: Richard Plangger <planri...@gmail.com>
Branch: strbuf-as-buffer
Changeset: r89202:392161cec298
Date: 2016-12-20 16:54 +0100
http://bitbucket.org/pypy/pypy/changeset/392161cec298/

Log:    extended test and made some simplifications

diff --git a/rpython/rlib/rgc.py b/rpython/rlib/rgc.py
--- a/rpython/rlib/rgc.py
+++ b/rpython/rlib/rgc.py
@@ -543,17 +543,8 @@
 
         NOTE: Only use for immutable objects!
     """
-    pass
-
-class MoveOutOfNurseryEntry(ExtRegistryEntry):
-    _about_ = move_out_of_nursery
-
-    def compute_result_annotation(self, s_obj):
-        return s_obj
-
-    def specialize_call(self, hop):
-        hop.exception_cannot_occur()
-        return hop.genop('gc_move_out_of_nursery', hop.args_v, 
resulttype=hop.r_result)
+    from rpython.rtyper.lltypesystem.lloperation import llop
+    return llop.gc_move_out_of_nursery(lltype.Void, obj)
 
 # ____________________________________________________________
 
diff --git a/rpython/rtyper/lltypesystem/rffi.py 
b/rpython/rtyper/lltypesystem/rffi.py
--- a/rpython/rtyper/lltypesystem/rffi.py
+++ b/rpython/rtyper/lltypesystem/rffi.py
@@ -1313,14 +1313,15 @@
             calling_conv='c',
         )
 
-class RawBytes(object):
-    # literal copy of _cffi_backend/func.py
-    def __init__(self, string):
-        self.ptr = str2charp(string, track_allocation=False)
-    def __del__(self):
-        free_charp(self.ptr, track_allocation=False)
 
 if not we_are_translated():
+    class RawBytes(object):
+        # literal copy of _cffi_backend/func.py
+        def __init__(self, string):
+            self.ptr = str2charp(string, track_allocation=False)
+        def __del__(self):
+            free_charp(self.ptr, track_allocation=False)
+
     TEST_RAW_ADDR_KEEP_ALIVE = {}
 
 @jit.dont_look_inside
@@ -1336,23 +1337,23 @@
     referencing it goes out of scope.
     """
     assert isinstance(string, str)
-    from rpython.rtyper.annlowlevel import llstr, hlstr
+    from rpython.rtyper.annlowlevel import llstr
     from rpython.rtyper.lltypesystem.rstr import STR
     from rpython.rtyper.lltypesystem import llmemory
     from rpython.rlib import rgc
 
     if we_are_translated():
+        newstring = string
         if rgc.can_move(string):
-            # create a shadow object that is exposed
-            string = rgc.move_out_of_nursery(string)
+            newstring = rgc.move_out_of_nursery(string)
 
-        # string cannot move! just return the address then!
-        lldata = llstr(string)
+        # string cannot move now! return the address
+        lldata = llstr(newstring)
         data_start = (llmemory.cast_ptr_to_adr(lldata) +
                       offsetof(STR, 'chars') +
                       llmemory.itemoffsetof(STR.chars, 0))
         data_start = cast(CCHARP, data_start)
-        data_start[len(string)] = '\x00'   # write the final extra null
+        data_start[len(newstring)] = '\x00'   # write the final extra null
         return data_start
     else:
         global TEST_RAW_ADDR_KEEP_ALIVE
diff --git a/rpython/rtyper/lltypesystem/test/test_ztranslated.py 
b/rpython/rtyper/lltypesystem/test/test_ztranslated.py
--- a/rpython/rtyper/lltypesystem/test/test_ztranslated.py
+++ b/rpython/rtyper/lltypesystem/test/test_ztranslated.py
@@ -1,13 +1,9 @@
-import sys
 import gc
 from rpython.translator.c.test.test_genc import compile
 from rpython.rtyper.lltypesystem import rffi
 from rpython.rtyper.lltypesystem import lltype
-from rpython.rtyper.annlowlevel import llstr, hlstr
 from rpython.rtyper.lltypesystem.lloperation import llop
-
-def setup_module(mod):
-    pass
+from rpython.rlib import rgc
 
 def debug_assert(boolresult, msg):
     if not boolresult:
@@ -15,13 +11,14 @@
         assert boolresult
 
 def use_str():
-    mystr = b'abc'[:]
+    mystr = b'abc'
+    #debug_assert(rgc.can_move(mystr), "short string cannot move... why?")
     ptr = rffi.get_raw_address_of_string(mystr)
     ptr2 = rffi.get_raw_address_of_string(mystr)
     debug_assert(ptr == ptr2, "ptr != ptr2")
     debug_assert(ptr[0] == b'a', "notnurseryadr[0] == b'a' is is %s" % ptr[0])
     ptr[0] = b'x' # oh no no, in real programs nobody is allowed to modify that
-    debug_assert(mystr[0] in b'ax', "mystr[0] in b'ax'")
+    debug_assert(mystr[0] == b'a', "mystr[0] != b'a'")
     debug_assert(ptr[0] == b'x', "notnurseryadr[0] == b'x'")
     gc.collect()
     nptr = rffi.get_raw_address_of_string(mystr)
@@ -30,10 +27,26 @@
     debug_assert(nptr[0] == b'x', "failure b")
     mystr = None
 
+def long_str(lstr):
+    ptr = rffi.get_raw_address_of_string(lstr)
+    for i,c in enumerate(lstr):
+        debug_assert(ptr[i] == c, "failure c")
+    gc.collect()
+    ptr2 = rffi.get_raw_address_of_string(lstr)
+    debug_assert(ptr == ptr2, "ptr != ptr2!!!")
+    return ptr
+
 def main(argv=[]):
     use_str()
-    llop.debug_print(lltype.Void, "passed first call to use_str")
     gc.collect()
+    mystr = b"12341234aa"*4096*10
+    #debug_assert(not rgc.can_move(mystr), "long string can move... why?")
+    p1 = long_str(mystr)
+    gc.collect()
+    copystr = mystr[:]
+    copystr += 'a'
+    p2 = long_str(copystr)
+    debug_assert(p1 != p2, "p1 == p2")
     return 0
 
 # ____________________________________________________________
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to