| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from rpython.rlib.objectmodel import specialize, we_are_translated |
| 3 |
from pypy.interpreter.error import oefmt |
| 4 |
from pypy.objspace.std.bytearrayobject import new_bytearray |
| 5 |
from pypy.module.cpyext.api import ( |
| 6 |
cpython_api, cpython_struct, build_type_checkers, |
| 7 |
PyVarObjectFields, Py_ssize_t, CONST_STRING) |
| 8 |
from pypy.module.cpyext.pyerrors import PyErr_BadArgument |
| 9 |
from pypy.module.cpyext.pyobject import ( |
| 10 |
PyObject, make_ref, make_typedescr, get_typedescr) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
PyByteArrayObjectStruct = lltype.ForwardReference() |
| 26 |
PyByteArrayObject = lltype.Ptr(PyByteArrayObjectStruct) |
| 27 |
PyByteArrayObjectFields = PyVarObjectFields |
| 28 |
cpython_struct("PyByteArrayObject", PyByteArrayObjectFields, PyByteArrayObjectStruct) |
| 29 |
|
| 30 |
PyByteArray_Check, PyByteArray_CheckExact = build_type_checkers("ByteArray", "w_bytearray") |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@cpython_api([PyObject], PyObject, result_is_ll=True) |
| 35 |
def PyByteArray_FromObject(space, w_obj): |
| 36 |
"""Return a new bytearray object from any object, o, that implements the |
| 37 |
buffer protocol. |
| 38 |
|
| 39 |
XXX expand about the buffer protocol, at least somewhere""" |
| 40 |
w_buffer = space.call_function(space.w_bytearray, w_obj) |
| 41 |
return make_ref(space, w_buffer) |
| 42 |
|
| 43 |
@cpython_api([CONST_STRING, Py_ssize_t], PyObject, result_is_ll=True) |
| 44 |
def PyByteArray_FromStringAndSize(space, char_p, length): |
| 45 |
"""Create a new bytearray object from string and its length, len. On |
| 46 |
failure, NULL is returned.""" |
| 47 |
if char_p: |
| 48 |
w_s = space.newbytes(rffi.charpsize2str(char_p, length)) |
| 49 |
else: |
| 50 |
w_s = space.newint(length) |
| 51 |
w_buffer = space.call_function(space.w_bytearray, w_s) |
| 52 |
return make_ref(space, w_buffer) |
| 53 |
|
| 54 |
@cpython_api([PyObject, PyObject], PyObject) |
| 55 |
def PyByteArray_Concat(space, w_left, w_right): |
| 56 |
"""Concat bytearrays a and b and return a new bytearray with the result.""" |
| 57 |
return space.add(w_left, w_right) |
| 58 |
|
| 59 |
@cpython_api([PyObject], Py_ssize_t, error=-1) |
| 60 |
def PyByteArray_Size(space, w_obj): |
| 61 |
"""Return the size of bytearray after checking for a NULL pointer.""" |
| 62 |
if not w_obj: |
| 63 |
return 0 |
| 64 |
return space.len_w(w_obj) |
| 65 |
|
| 66 |
@cpython_api([PyObject], rffi.CCHARP, error=0) |
| 67 |
def PyByteArray_AsString(space, w_obj): |
| 68 |
"""Return the contents of bytearray as a char array after checking for a |
| 69 |
NULL pointer.""" |
| 70 |
if space.isinstance_w(w_obj, space.w_bytearray): |
| 71 |
return w_obj.nonmovable_carray(space) |
| 72 |
else: |
| 73 |
raise oefmt(space.w_TypeError, |
| 74 |
"expected bytearray object, %T found", w_obj) |
| 75 |
|
| 76 |
@cpython_api([PyObject, Py_ssize_t], rffi.INT_real, error=-1) |
| 77 |
def PyByteArray_Resize(space, w_obj, newlen): |
| 78 |
"""Resize the internal buffer of bytearray to len.""" |
| 79 |
if space.isinstance_w(w_obj, space.w_bytearray): |
| 80 |
oldlen = space.len_w(w_obj) |
| 81 |
if newlen > oldlen: |
| 82 |
space.call_method(w_obj, 'extend', space.newbytes('\x00' * (newlen - oldlen))) |
| 83 |
elif oldlen > newlen: |
| 84 |
assert newlen >= 0 |
| 85 |
space.delslice(w_obj, space.newint(newlen), space.newint(oldlen)) |
| 86 |
return 0 |
| 87 |
else: |
| 88 |
raise oefmt(space.w_TypeError, |
| 89 |
"expected bytearray object, %T found", w_obj) |