| 1 | from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 | from rpython.rlib import rutf8 |
| 3 | from pypy.interpreter.error import oefmt |
| 4 | from pypy.module._hpy_universal.apiset import API |
| 5 | |
| 6 | @API.func("int HPyBytes_Check(HPyContext *ctx, HPy h)", error_value='CANNOT_FAIL') |
| 7 | def HPyBytes_Check(space, handles, ctx, h): |
| 8 | w_obj = handles.deref(h) |
| 9 | w_obj_type = space.type(w_obj) |
| 10 | res = (space.is_w(w_obj_type, space.w_bytes) or |
| 11 | space.issubtype_w(w_obj_type, space.w_bytes)) |
| 12 | return API.int(res) |
| 13 | |
| 14 | @API.func("HPy_ssize_t HPyBytes_Size(HPyContext *ctx, HPy h)", error_value=-1) |
| 15 | def HPyBytes_Size(space, handles, ctx, h): |
| 16 | w_obj = handles.deref(h) |
| 17 | return space.len_w(w_obj) |
| 18 | |
| 19 | @API.func("HPy_ssize_t HPyBytes_GET_SIZE(HPyContext *ctx, HPy h)", error_value=-1) |
| 20 | def HPyBytes_GET_SIZE(space, handles, ctx, h): |
| 21 | return HPyBytes_Size(space, handles, ctx, h) |
| 22 | |
| 23 | @API.func("char *HPyBytes_AsString(HPyContext *ctx, HPy h)") |
| 24 | def HPyBytes_AsString(space, handles, ctx, h): |
| 25 | w_obj = handles.deref(h) |
| 26 | s = space.bytes_w(w_obj) |
| 27 | return handles.str2ownedptr(s, owner=h) |
| 28 | |
| 29 | @API.func("char *HPyBytes_AS_STRING(HPyContext *ctx, HPy h)") |
| 30 | def HPyBytes_AS_STRING(space, handles, ctx, h): |
| 31 | return HPyBytes_AsString(space, handles, ctx, h) |
| 32 | |
| 33 | @API.func("HPy HPyBytes_FromString(HPyContext *ctx, const char *v)") |
| 34 | def HPyBytes_FromString(space, handles, ctx, char_p): |
| 35 | s = rffi.constcharp2str(char_p) |
| 36 | w_bytes = space.newbytes(s) |
| 37 | return handles.new(w_bytes) |
| 38 | |
| 39 | @API.func("HPy HPyBytes_FromStringAndSize(HPyContext *ctx, const char *v, HPy_ssize_t len)") |
| 40 | def HPyBytes_FromStringAndSize(space, handles, ctx, char_p, length): |
| 41 | if not char_p: |
| 42 | raise oefmt( |
| 43 | space.w_ValueError, |
| 44 | "NULL char * passed to HPyBytes_FromStringAndSize" |
| 45 | ) |
| 46 | if length < 0: |
| 47 | raise oefmt(space.w_SystemError, |
| 48 | "Negative size passed to HPyBytes_FromStringAndSize") |
| 49 | s = rffi.constcharpsize2str(char_p, length) |
| 50 | w_bytes = space.newbytes(s) |
| 51 | return handles.new(w_bytes) |