[pypy-commit] pypy default: Internal string->bytes renaming

rlamy pypy.commits at gmail.com
Tue Jun 28 11:38:07 EDT 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: 
Changeset: r85436:4b3a2a0269e2
Date: 2016-06-28 16:37 +0100
http://bitbucket.org/pypy/pypy/changeset/4b3a2a0269e2/

Log:	Internal string->bytes renaming

diff --git a/pypy/module/cpyext/bytesobject.py b/pypy/module/cpyext/bytesobject.py
--- a/pypy/module/cpyext/bytesobject.py
+++ b/pypy/module/cpyext/bytesobject.py
@@ -11,7 +11,7 @@
 from pypy.objspace.std.bytesobject import W_BytesObject
 
 ##
-## Implementation of PyStringObject
+## Implementation of PyBytesObject
 ## ================================
 ##
 ## The problem
@@ -29,15 +29,15 @@
 ## Solution
 ## --------
 ##
-## PyStringObject contains two additional members: the ob_size and a pointer to a
+## PyBytesObject contains two additional members: the ob_size and a pointer to a
 ## char ob_sval; it may be NULL.
 ##
-## - A string allocated by pypy will be converted into a PyStringObject with a
+## - A string allocated by pypy will be converted into a PyBytesObject with a
 ##   NULL buffer.  The first time PyString_AsString() is called, memory is
 ##   allocated (with flavor='raw') and content is copied.
 ##
 ## - A string allocated with PyString_FromStringAndSize(NULL, size) will
-##   allocate a PyStringObject structure, and a buffer with the specified
+##   allocate a PyBytesObject structure, and a buffer with the specified
 ##   size+1, but the reference won't be stored in the global map; there is no
 ##   corresponding object in pypy.  When from_ref() or Py_INCREF() is called,
 ##   the pypy string is created, and added to the global map of tracked
@@ -55,58 +55,58 @@
 ##   corresponds to the pypy gc-managed string.
 ##
 
-PyStringObjectStruct = lltype.ForwardReference()
-PyStringObject = lltype.Ptr(PyStringObjectStruct)
-PyStringObjectFields = PyVarObjectFields + \
+PyBytesObjectStruct = lltype.ForwardReference()
+PyBytesObject = lltype.Ptr(PyBytesObjectStruct)
+PyBytesObjectFields = PyVarObjectFields + \
     (("ob_shash", rffi.LONG), ("ob_sstate", rffi.INT), ("ob_sval", rffi.CArray(lltype.Char)))
-cpython_struct("PyStringObject", PyStringObjectFields, PyStringObjectStruct)
+cpython_struct("PyStringObject", PyBytesObjectFields, PyBytesObjectStruct)
 
 @bootstrap_function
-def init_stringobject(space):
-    "Type description of PyStringObject"
+def init_bytesobject(space):
+    "Type description of PyBytesObject"
     make_typedescr(space.w_str.layout.typedef,
-                   basestruct=PyStringObject.TO,
-                   attach=string_attach,
-                   dealloc=string_dealloc,
-                   realize=string_realize)
+                   basestruct=PyBytesObject.TO,
+                   attach=bytes_attach,
+                   dealloc=bytes_dealloc,
+                   realize=bytes_realize)
 
 PyString_Check, PyString_CheckExact = build_type_checkers("String", "w_str")
 
 def new_empty_str(space, length):
     """
-    Allocate a PyStringObject and its ob_sval, but without a corresponding
-    interpreter object.  The ob_sval may be mutated, until string_realize() is
+    Allocate a PyBytesObject and its ob_sval, but without a corresponding
+    interpreter object.  The ob_sval may be mutated, until bytes_realize() is
     called.  Refcount of the result is 1.
     """
     typedescr = get_typedescr(space.w_str.layout.typedef)
     py_obj = typedescr.allocate(space, space.w_str, length)
-    py_str = rffi.cast(PyStringObject, py_obj)
+    py_str = rffi.cast(PyBytesObject, py_obj)
     py_str.c_ob_shash = -1
     py_str.c_ob_sstate = rffi.cast(rffi.INT, 0) # SSTATE_NOT_INTERNED
     return py_str
 
-def string_attach(space, py_obj, w_obj):
+def bytes_attach(space, py_obj, w_obj):
     """
-    Copy RPython string object contents to a PyStringObject. The
+    Copy RPython string object contents to a PyBytesObject. The
     c_ob_sval must not be modified.
     """
-    py_str = rffi.cast(PyStringObject, py_obj)
+    py_str = rffi.cast(PyBytesObject, py_obj)
     s = space.str_w(w_obj)
     if py_str.c_ob_size  < len(s):
         raise oefmt(space.w_ValueError,
-            "string_attach called on object with ob_size %d but trying to store %d",
-            py_str.c_ob_size, len(s)) 
+            "bytes_attach called on object with ob_size %d but trying to store %d",
+            py_str.c_ob_size, len(s))
     rffi.c_memcpy(py_str.c_ob_sval, rffi.str2charp(s), len(s))
     py_str.c_ob_sval[len(s)] = '\0'
     py_str.c_ob_shash = space.hash_w(w_obj)
     py_str.c_ob_sstate = rffi.cast(rffi.INT, 1) # SSTATE_INTERNED_MORTAL
 
-def string_realize(space, py_obj):
+def bytes_realize(space, py_obj):
     """
-    Creates the string in the interpreter. The PyStringObject ob_sval must not
+    Creates the string in the interpreter. The PyBytesObject ob_sval must not
     be modified after this call.
     """
-    py_str = rffi.cast(PyStringObject, py_obj)
+    py_str = rffi.cast(PyBytesObject, py_obj)
     s = rffi.charpsize2str(py_str.c_ob_sval, py_str.c_ob_size)
     w_type = from_ref(space, rffi.cast(PyObject, py_obj.c_ob_type))
     w_obj = space.allocate_instance(W_BytesObject, w_type)
@@ -117,8 +117,8 @@
     return w_obj
 
 @cpython_api([PyObject], lltype.Void, header=None)
-def string_dealloc(space, py_obj):
-    """Frees allocated PyStringObject resources.
+def bytes_dealloc(space, py_obj):
+    """Frees allocated PyBytesObject resources.
     """
     from pypy.module.cpyext.object import _dealloc
     _dealloc(space, py_obj)
@@ -154,10 +154,10 @@
             raise oefmt(space.w_TypeError,
                         "expected string or Unicode object, %T found",
                         from_ref(space, ref))
-    ref_str = rffi.cast(PyStringObject, ref)
+    ref_str = rffi.cast(PyBytesObject, ref)
     if not pyobj_has_w_obj(ref):
         # XXX Force the ref?
-        string_realize(space, ref)
+        bytes_realize(space, ref)
     return ref_str.c_ob_sval
 
 @cpython_api([rffi.VOIDP], rffi.CCHARP, error=0)
@@ -166,7 +166,7 @@
     # if no w_str is associated with this ref,
     # return the c-level ptr as RW
     if not pyobj_has_w_obj(ref):
-        py_str = rffi.cast(PyStringObject, ref)
+        py_str = rffi.cast(PyBytesObject, ref)
         return py_str.c_ob_sval
     return _PyString_AsString(space, ref)
 
@@ -183,8 +183,8 @@
                         from_ref(space, ref))
     if not pyobj_has_w_obj(ref):
         # force the ref
-        string_realize(space, ref)
-    ref_str = rffi.cast(PyStringObject, ref)
+        bytes_realize(space, ref)
+    ref_str = rffi.cast(PyBytesObject, ref)
     data[0] = ref_str.c_ob_sval
     if length:
         length[0] = ref_str.c_ob_size
@@ -200,7 +200,7 @@
 @cpython_api([PyObject], Py_ssize_t, error=-1)
 def PyString_Size(space, ref):
     if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_str:
-        ref = rffi.cast(PyStringObject, ref)
+        ref = rffi.cast(PyBytesObject, ref)
         return ref.c_ob_size
     else:
         w_obj = from_ref(space, ref)
@@ -222,7 +222,7 @@
     if pyobj_has_w_obj(ref[0]):
         raise oefmt(space.w_SystemError,
                     "_PyString_Resize called on already created string")
-    py_str = rffi.cast(PyStringObject, ref[0])
+    py_str = rffi.cast(PyBytesObject, ref[0])
     try:
         py_newstr = new_empty_str(space, newsize)
     except MemoryError:
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -164,7 +164,7 @@
     pytype = rffi.cast(PyTypeObjectPtr, as_pyobj(space, w_type))
     typedescr = get_typedescr(w_obj.typedef)
     if pytype.c_tp_itemsize != 0:
-        itemcount = space.len_w(w_obj) # PyStringObject and subclasses
+        itemcount = space.len_w(w_obj) # PyBytesObject and subclasses
     else:
         itemcount = 0
     py_obj = typedescr.allocate(space, w_type, itemcount=itemcount)
diff --git a/pypy/module/cpyext/sliceobject.py b/pypy/module/cpyext/sliceobject.py
--- a/pypy/module/cpyext/sliceobject.py
+++ b/pypy/module/cpyext/sliceobject.py
@@ -38,7 +38,7 @@
 
 @cpython_api([PyObject], lltype.Void, header=None)
 def slice_dealloc(space, py_obj):
-    """Frees allocated PyStringObject resources.
+    """Frees allocated PyBytesObject resources.
     """
     py_slice = rffi.cast(PySliceObject, py_obj)
     Py_DecRef(space, py_slice.c_start)
@@ -73,7 +73,7 @@
     length length, and store the length of the slice in slicelength.  Out
     of bounds indices are clipped in a manner consistent with the handling of
     normal slices.
-    
+
     Returns 0 on success and -1 on error with exception set."""
     if not PySlice_Check(space, w_slice):
         PyErr_BadInternalCall(space)
@@ -88,11 +88,11 @@
     """Retrieve the start, stop and step indices from the slice object slice,
     assuming a sequence of length length. Treats indices greater than
     length as errors.
-    
+
     Returns 0 on success and -1 on error with no exception set (unless one of
     the indices was not None and failed to be converted to an integer,
     in which case -1 is returned with an exception set).
-    
+
     You probably do not want to use this function.  If you want to use slice
     objects in versions of Python prior to 2.3, you would probably do well to
     incorporate the source of PySlice_GetIndicesEx(), suitably renamed,
diff --git a/pypy/module/cpyext/test/test_bytesobject.py b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -2,7 +2,7 @@
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-from pypy.module.cpyext.bytesobject import new_empty_str, PyStringObject
+from pypy.module.cpyext.bytesobject import new_empty_str, PyBytesObject
 from pypy.module.cpyext.api import PyObjectP, PyObject, Py_ssize_tP, generic_cpy_call
 from pypy.module.cpyext.pyobject import Py_DecRef, from_ref, make_ref
 from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
@@ -82,14 +82,14 @@
              """
                 PyObject *base;
                 PyTypeObject * type;
-                PyStringObject *obj;
+                PyBytesObject *obj;
                 base = PyBytes_FromString("test");
                 if (PyBytes_GET_SIZE(base) != 4)
                     return PyLong_FromLong(-PyBytes_GET_SIZE(base));
                 type = base->ob_type;
                 if (type->tp_itemsize != 1)
                     return PyLong_FromLong(type->tp_itemsize);
-                obj = (PyStringObject*)type->tp_alloc(type, 10);
+                obj = (PyBytesObject*)type->tp_alloc(type, 10);
                 if (PyBytes_GET_SIZE(obj) != 10)
                     return PyLong_FromLong(PyBytes_GET_SIZE(obj));
                 /* cannot work, there is only RO access
@@ -266,7 +266,7 @@
                  PyObject *s = args;
                  Py_INCREF(s);
                  PyString_InternInPlace(&s);
-                 if (((PyStringObject*)s)->ob_sstate == SSTATE_NOT_INTERNED)
+                 if (((PyBytesObject*)s)->ob_sstate == SSTATE_NOT_INTERNED)
                  {
                     Py_DECREF(s);
                     s = PyString_FromString("interned error");
@@ -284,7 +284,7 @@
              ("test_macro_invocations", "METH_NOARGS",
              """
                 PyObject* o = PyString_FromString("");
-                PyStringObject* u = (PyStringObject*)o;
+                PyBytesObject* u = (PyBytesObject*)o;
 
                 PyString_GET_SIZE(u);
                 PyString_GET_SIZE(o);
@@ -301,17 +301,17 @@
             ("test_hash", "METH_VARARGS",
              '''
                 PyObject* obj = (PyTuple_GetItem(args, 0));
-                long hash = ((PyStringObject*)obj)->ob_shash;
+                long hash = ((PyBytesObject*)obj)->ob_shash;
                 return PyLong_FromLong(hash);
              '''
              ),
             ("test_sstate", "METH_NOARGS",
              '''
                 PyObject *s = PyString_FromString("xyz");
-                /*int sstate = ((PyStringObject*)s)->ob_sstate;
+                /*int sstate = ((PyBytesObject*)s)->ob_sstate;
                 printf("sstate now %d\\n", sstate);*/
                 PyString_InternInPlace(&s);
-                /*sstate = ((PyStringObject*)s)->ob_sstate;
+                /*sstate = ((PyBytesObject*)s)->ob_sstate;
                 printf("sstate now %d\\n", sstate);*/
                 Py_DECREF(s);
                 return PyBool_FromLong(1);
@@ -345,7 +345,7 @@
                     PyObject_HEAD_INIT(NULL)
                     0,                            /* ob_size */
                     "bar.string_",                /* tp_name*/
-                    sizeof(PyStringObject), /* tp_basicsize*/
+                    sizeof(PyBytesObject), /* tp_basicsize*/
                     0                             /* tp_itemsize */
                     };
 
@@ -401,7 +401,7 @@
                             return NULL;
                         }
                         destptr = PyString_AS_STRING(obj);
-                        ((PyStringObject *)obj)->ob_shash = -1;
+                        ((PyBytesObject *)obj)->ob_shash = -1;
                         memcpy(destptr, data, itemsize);
                         return obj;
                     }
@@ -429,14 +429,14 @@
         py_str.c_ob_sval[2] = 'c'
         ar[0] = rffi.cast(PyObject, py_str)
         api._PyString_Resize(ar, 3)
-        py_str = rffi.cast(PyStringObject, ar[0])
+        py_str = rffi.cast(PyBytesObject, ar[0])
         assert py_str.c_ob_size == 3
         assert py_str.c_ob_sval[1] == 'b'
         assert py_str.c_ob_sval[3] == '\x00'
         # the same for growing
         ar[0] = rffi.cast(PyObject, py_str)
         api._PyString_Resize(ar, 10)
-        py_str = rffi.cast(PyStringObject, ar[0])
+        py_str = rffi.cast(PyBytesObject, ar[0])
         assert py_str.c_ob_size == 10
         assert py_str.c_ob_sval[1] == 'b'
         assert py_str.c_ob_sval[10] == '\x00'
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -167,7 +167,7 @@
     py_memberdescr.c_d_member = w_obj.member
 
 def memberdescr_realize(space, obj):
-    # XXX NOT TESTED When is this ever called? 
+    # XXX NOT TESTED When is this ever called?
     member = rffi.cast(lltype.Ptr(PyMemberDef), obj)
     w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
     w_obj = space.allocate_instance(W_MemberDescr, w_type)
@@ -192,7 +192,7 @@
     py_methoddescr.c_d_method = w_obj.ml
 
 def classmethoddescr_realize(space, obj):
-    # XXX NOT TESTED When is this ever called? 
+    # XXX NOT TESTED When is this ever called?
     method = rffi.cast(lltype.Ptr(PyMethodDef), obj)
     w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
     w_obj = space.allocate_instance(W_PyCClassMethodObject, w_type)
@@ -201,7 +201,7 @@
     return w_obj
 
 def methoddescr_realize(space, obj):
-    # XXX NOT TESTED When is this ever called? 
+    # XXX NOT TESTED When is this ever called?
     method = rffi.cast(lltype.Ptr(PyMethodDef), obj)
     w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
     w_obj = space.allocate_instance(W_PyCMethodObject, w_type)
@@ -566,7 +566,7 @@
     if space.is_w(w_type, space.w_str):
         # Special case: str doesn't support get_raw_address(), so we have a
         # custom get*buffer that instead gives the address of the char* in the
-        # PyStringObject*!
+        # PyBytesObject*!
         c_buf.c_bf_getreadbuffer = llhelper(
             str_getreadbuffer.api_func.functype,
             str_getreadbuffer.api_func.get_wrapper(space))
@@ -782,7 +782,7 @@
 
     if py_type.c_ob_type:
         w_metatype = from_ref(space, rffi.cast(PyObject, py_type.c_ob_type))
-    else: 
+    else:
         # Somehow the tp_base type is created with no ob_type, notably
         # PyString_Type and PyBaseString_Type
         # While this is a hack, cpython does it as well.
@@ -797,10 +797,10 @@
     # inheriting tp_as_* slots
     base = py_type.c_tp_base
     if base:
-        if not py_type.c_tp_as_number: py_type.c_tp_as_number = base.c_tp_as_number 
-        if not py_type.c_tp_as_sequence: py_type.c_tp_as_sequence = base.c_tp_as_sequence 
-        if not py_type.c_tp_as_mapping: py_type.c_tp_as_mapping = base.c_tp_as_mapping 
-        if not py_type.c_tp_as_buffer: py_type.c_tp_as_buffer = base.c_tp_as_buffer 
+        if not py_type.c_tp_as_number: py_type.c_tp_as_number = base.c_tp_as_number
+        if not py_type.c_tp_as_sequence: py_type.c_tp_as_sequence = base.c_tp_as_sequence
+        if not py_type.c_tp_as_mapping: py_type.c_tp_as_mapping = base.c_tp_as_mapping
+        if not py_type.c_tp_as_buffer: py_type.c_tp_as_buffer = base.c_tp_as_buffer
 
     return w_obj
 


More information about the pypy-commit mailing list