def _PyBytes_Resize(space, ref, newsize): """A way to resize a string object even though it is "immutable". Only use this to build up a brand new string object; don't use this if the string may already be known in other parts of the code. It is an error to call this function if the refcount on the input string object is not one. Pass the address of an existing string object as an lvalue (it may be written into), and the new size desired. On success, *string holds the resized string object and 0 is returned; the address in *string may differ from its input value. If the reallocation fails, the original string object at *string is deallocated, *string is set to NULL, a memory exception is set, and -1 is returned. """ # XXX always create a new string so far if pyobj_has_w_obj(ref[0]): raise oefmt(space.w_SystemError, "_PyBytes_Resize called on already created string") py_str = rffi.cast(PyBytesObject, ref[0]) try: py_newstr = new_empty_str(space, newsize) except MemoryError: decref(space, ref[0]) ref[0] = lltype.nullptr(PyObject.TO) raise to_cp = newsize oldsize = py_str.c_ob_size if oldsize < newsize: to_cp = oldsize for i in range(to_cp): py_newstr.c_ob_sval[i] = py_str.c_ob_sval[i] decref(space, ref[0]) ref[0] = rffi.cast(PyObject, py_newstr) return 0
def _PyString_Resize(space, ref, newsize): """A way to resize a string object even though it is "immutable". Only use this to build up a brand new string object; don't use this if the string may already be known in other parts of the code. It is an error to call this function if the refcount on the input string object is not one. Pass the address of an existing string object as an lvalue (it may be written into), and the new size desired. On success, *string holds the resized string object and 0 is returned; the address in *string may differ from its input value. If the reallocation fails, the original string object at *string is deallocated, *string is set to NULL, a memory exception is set, and -1 is returned. """ # XXX always create a new string so far if pyobj_has_w_obj(ref[0]): raise oefmt(space.w_SystemError, "_PyString_Resize called on already created string") py_str = rffi.cast(PyBytesObject, ref[0]) try: py_newstr = new_empty_str(space, newsize) except MemoryError: Py_DecRef(space, ref[0]) ref[0] = lltype.nullptr(PyObject.TO) raise to_cp = newsize oldsize = py_str.c_ob_size if oldsize < newsize: to_cp = oldsize for i in range(to_cp): py_newstr.c_ob_sval[i] = py_str.c_ob_sval[i] Py_DecRef(space, ref[0]) ref[0] = rffi.cast(PyObject, py_newstr) return 0
def PyBytes_AS_STRING(space, void_ref): ref = rffi.cast(PyObject, void_ref) # 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(PyBytesObject, ref) return py_str.c_ob_sval return _PyBytes_AsString(space, ref)
def PyString_AS_STRING(space, void_ref): ref = rffi.cast(PyObject, void_ref) # 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(PyBytesObject, ref) return py_str.c_ob_sval return _PyString_AsString(space, ref)
def PyTuple_SetItem(space, ref, index, py_obj): if not tuple_check_ref(space, ref): decref(space, py_obj) PyErr_BadInternalCall(space) tupleobj = rffi.cast(PyTupleObject, ref) size = tupleobj.c_ob_size if index < 0 or index >= size: decref(space, py_obj) raise oefmt(space.w_IndexError, "tuple assignment index out of range") old_ref = tupleobj.c_ob_item[index] if pyobj_has_w_obj(ref): # similar but not quite equal to ref.c_ob_refcnt != 1 on CPython raise oefmt(space.w_SystemError, "PyTuple_SetItem called on tuple after" " use of tuple") tupleobj.c_ob_item[index] = py_obj # consumes a reference if old_ref: decref(space, old_ref) return 0