| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 |
from rpython.rlib import rutf8 |
| 3 |
from pypy.interpreter.error import OperationError, oefmt |
| 4 |
from pypy.interpreter.unicodehelper import wcharpsize2utf8 |
| 5 |
from pypy.objspace.std import unicodeobject |
| 6 |
from pypy.module._hpy_universal.apiset import API |
| 7 |
|
| 8 |
def _maybe_utf8_to_w(space, utf8): |
| 9 |
|
| 10 |
s = rffi.constcharp2str(utf8) |
| 11 |
try: |
| 12 |
length = rutf8.check_utf8(s, allow_surrogates=False) |
| 13 |
except rutf8.CheckError: |
| 14 |
raise |
| 15 |
return space.newtext(s, length) |
| 16 |
|
| 17 |
@API.func("int HPyUnicode_Check(HPyContext *ctx, HPy h)", error_value=API.int(-1)) |
| 18 |
def HPyUnicode_Check(space, handles, ctx, h): |
| 19 |
w_obj = handles.deref(h) |
| 20 |
w_obj_type = space.type(w_obj) |
| 21 |
res = (space.is_w(w_obj_type, space.w_unicode) or |
| 22 |
space.issubtype_w(w_obj_type, space.w_unicode)) |
| 23 |
return API.int(res) |
| 24 |
|
| 25 |
@API.func("HPy HPyUnicode_FromString(HPyContext *ctx, const char *utf8)") |
| 26 |
def HPyUnicode_FromString(space, handles, ctx, utf8): |
| 27 |
w_obj = _maybe_utf8_to_w(space, utf8) |
| 28 |
return handles.new(w_obj) |
| 29 |
|
| 30 |
@API.func("HPy HPyUnicode_AsUTF8String(HPyContext *ctx, HPy h)") |
| 31 |
def HPyUnicode_AsUTF8String(space, handles, ctx, h): |
| 32 |
w_unicode = handles.deref(h) |
| 33 |
|
| 34 |
w_bytes = unicodeobject.encode_object(space, w_unicode, 'utf-8', 'strict') |
| 35 |
return handles.new(w_bytes) |
| 36 |
|
| 37 |
@API.func("HPy HPyUnicode_AsASCIIString(HPyContext *ctx, HPy h)") |
| 38 |
def HPyUnicode_AsASCIIString(space, handles, ctx, h): |
| 39 |
w_unicode = handles.deref(h) |
| 40 |
w_bytes = unicodeobject.encode_object(space, w_unicode, 'ascii', 'strict') |
| 41 |
return handles.new(w_bytes) |
| 42 |
|
| 43 |
@API.func("HPy HPyUnicode_AsLatin1String(HPyContext *ctx, HPy h)") |
| 44 |
def HPyUnicode_AsLatin1String(space, handles, ctx, h): |
| 45 |
w_unicode = handles.deref(h) |
| 46 |
w_bytes = unicodeobject.encode_object(space, w_unicode, 'latin1', 'strict') |
| 47 |
return handles.new(w_bytes) |
| 48 |
|
| 49 |
@API.func("HPy HPyUnicode_EncodeFSDefault(HPyContext *ctx, HPy h)") |
| 50 |
def HPyUnicode_EncodeFSDefault(space, handles, ctx, h): |
| 51 |
w_unicode = handles.deref(h) |
| 52 |
w_bytes = space.fsencode(w_unicode) |
| 53 |
return handles.new(w_bytes) |
| 54 |
|
| 55 |
@API.func("const char *HPyUnicode_AsUTF8AndSize(HPyContext *ctx, HPy h, HPy_ssize_t *size)") |
| 56 |
def HPyUnicode_AsUTF8AndSize(space, handles, ctx, h, size): |
| 57 |
w_unicode = handles.deref(h) |
| 58 |
|
| 59 |
s = space.utf8_w(w_unicode) |
| 60 |
if size: |
| 61 |
size[0] = len(s) |
| 62 |
res = handles.str2ownedptr(s, owner=h) |
| 63 |
return rffi.cast(rffi.CONST_CCHARP, res) |
| 64 |
|
| 65 |
@API.func("HPy HPyUnicode_FromWideChar(HPyContext *ctx, const wchar_t *w, HPy_ssize_t size)") |
| 66 |
def HPyUnicode_FromWideChar(space, handles, ctx, wchar_p, size): |
| 67 |
|
| 68 |
wchar_p = rffi.cast(rffi.CWCHARP, wchar_p) |
| 69 |
if wchar_p: |
| 70 |
if size == -1: |
| 71 |
size = wcharplen(wchar_p) |
| 72 |
|
| 73 |
|
| 74 |
s = wcharpsize2utf8(space, wchar_p, size) |
| 75 |
w_obj = space.newutf8(s, size) |
| 76 |
return handles.new(w_obj) |
| 77 |
else: |
| 78 |
|
| 79 |
raise NotImplementedError("WRITE TEST") |
| 80 |
|
| 81 |
|
| 82 |
def wcharplen(wchar_p): |
| 83 |
i = 0 |
| 84 |
while ord(wchar_p[i]): |
| 85 |
i += 1 |
| 86 |
return i |
| 87 |
|
| 88 |
@API.func("HPy HPyUnicode_DecodeFSDefault(HPyContext *ctx, const char *v)") |
| 89 |
def HPyUnicode_DecodeFSDefault(space, handles, ctx, v): |
| 90 |
w_bytes = space.newbytes(rffi.constcharp2str(v)) |
| 91 |
w_decoded = space.fsdecode(w_bytes) |
| 92 |
return handles.new(w_decoded) |
| 93 |
|
| 94 |
@API.func("HPy HPyUnicode_DecodeFSDefaultAndSize(HPyContext *ctx, const char *v, ssize_t size)") |
| 95 |
def HPyUnicode_DecodeFSDefaultAndSize(space, handles, ctx, v, size): |
| 96 |
w_bytes = space.newbytes(rffi.constcharpsize2str(v, size)) |
| 97 |
w_decoded = space.fsdecode(w_bytes) |
| 98 |
return handles.new(w_decoded) |
| 99 |
|
| 100 |
@API.func("HPy HPyUnicode_DecodeASCII(HPyContext *ctx, const char *v, ssize_t size, const char * errors)") |
| 101 |
def HPyUnicode_DecodeASCII(space, handles, ctx, v, size, errors): |
| 102 |
w_s = space.newbytes(rffi.constcharpsize2str(v, size)) |
| 103 |
if errors: |
| 104 |
w_errors = space.newtext(rffi.constcharp2str(errors)) |
| 105 |
else: |
| 106 |
w_errors = None |
| 107 |
w_decoded = space.call_method(w_s, 'decode', space.newtext('ascii'), w_errors) |
| 108 |
return handles.new(w_decoded) |
| 109 |
|
| 110 |
@API.func("HPy HPyUnicode_DecodeLatin1(HPyContext *ctx, const char *v, ssize_t size, const char * errors)") |
| 111 |
def HPyUnicode_DecodeLatin1(space, handles, ctx, v, size, errors): |
| 112 |
w_s = space.newbytes(rffi.constcharpsize2str(v, size)) |
| 113 |
if errors: |
| 114 |
w_errors = space.newtext(rffi.constcharp2str(errors)) |
| 115 |
else: |
| 116 |
w_errors = None |
| 117 |
w_decoded = space.call_method(w_s, 'decode', space.newtext('latin1'), w_errors) |
| 118 |
return handles.new(w_decoded) |
| 119 |
|
| 120 |
|
| 121 |
@API.func("long HPyUnicode_ReadChar(HPyContext *ctx, HPy h, HPy_ssize_t index)", |
| 122 |
error_value=rffi.cast(rffi.LONG, -1)) |
| 123 |
def HPyUnicode_ReadChar(space, handles, ctx, h, index): |
| 124 |
w_unicode = handles.deref(h) |
| 125 |
if index < 0 or index > space.len_w(w_unicode): |
| 126 |
raise oefmt(space.w_IndexError, "string index out of range") |
| 127 |
w_ch = space.getitem(w_unicode, space.newint(index)) |
| 128 |
return rffi.cast(rffi.LONG, space.int_w(space.ord(w_ch))) |
| 129 |
|
| 130 |
@API.func("HPy HPyUnicode_Substring(HPyContext *ctx, HPy str, HPy_ssize_t start, HPy_ssize_t end)") |
| 131 |
def HPyUnicode_Substring(space, handles, ctx, h, start, end): |
| 132 |
w_obj = handles.deref(h) |
| 133 |
if not space.isinstance_w(w_obj, space.w_text): |
| 134 |
raise oefmt(space.w_TypeError, "expected string object") |
| 135 |
if start == 0 and end == space.len_w(w_obj): |
| 136 |
return handles.new(w_obj) |
| 137 |
if start < 0 or end < 0: |
| 138 |
raise oefmt(space.w_IndexError, "string index out of range") |
| 139 |
w_result = space.call_method(w_obj, '__getitem__', |
| 140 |
space.newslice(space.newint(start), space.newint(end), |
| 141 |
space.newint(1))) |
| 142 |
return handles.new(w_result) |
| 143 |
|
| 144 |
@API.func("HPy HPyUnicode_FromEncodedObject(HPyContext *ctx, HPy obj, const char *encoding, const char *errors)") |
| 145 |
def HPyUnicode_FromEncodedObject(space, handles, ctx, h, encoding, errors): |
| 146 |
if not h: |
| 147 |
raise oefmt(space.w_SystemError, "NULL handle passed to HPyUnicode_FromEncodedObject") |
| 148 |
w_obj = handles.deref(h) |
| 149 |
if space.isinstance_w(w_obj, space.w_bytes): |
| 150 |
s = space.bytes_w(w_obj) |
| 151 |
if not s: |
| 152 |
return handles.new(space.newtext('')) |
| 153 |
elif space.isinstance_w(w_obj, space.w_unicode): |
| 154 |
raise oefmt(space.w_TypeError, "decoding str is not supported") |
| 155 |
elif space.isinstance_w(w_obj, space.w_bytearray): |
| 156 |
raise oefmt(space.w_TypeError, "decoding bytearray is not supported") |
| 157 |
else: |
| 158 |
s = space.charbuf_w(w_obj) |
| 159 |
if encoding: |
| 160 |
w_encoding = space.newtext(rffi.constcharp2str(encoding)) |
| 161 |
else: |
| 162 |
w_encoding = space.newtext('utf-8') |
| 163 |
w_str = space.newbytes(s) |
| 164 |
if errors: |
| 165 |
w_errors = space.newtext(rffi.constcharp2str(errors)) |
| 166 |
else: |
| 167 |
w_errors = None |
| 168 |
w_result = space.call_method(w_str, 'decode', w_encoding, w_errors) |
| 169 |
return handles.new(w_result) |
| 170 |
|
| 171 |
|