| 1 |
""" |
| 2 |
Provides internal 'locale' codecs (via POSIX wcstombs/mbrtowc) for use |
| 3 |
by PyUnicode_Decode/EncodeFSDefault during interpreter bootstrap |
| 4 |
""" |
| 5 |
import os |
| 6 |
import py |
| 7 |
import sys |
| 8 |
from rpython.rlib.objectmodel import we_are_translated |
| 9 |
from rpython.rlib.rstring import StringBuilder, assert_str0 |
| 10 |
from rpython.rlib.runicode import ( |
| 11 |
default_unicode_error_decode, default_unicode_error_encode) |
| 12 |
from rpython.rlib.rutf8 import ( |
| 13 |
Utf8StringIterator, unichr_as_utf8, codepoints_in_utf8) |
| 14 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 15 |
from rpython.rlib.rarithmetic import r_uint |
| 16 |
from rpython.translator import cdir |
| 17 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 18 |
|
| 19 |
cwd = py.path.local(__file__).dirpath() |
| 20 |
eci = ExternalCompilationInfo( |
| 21 |
includes=[cwd.join('locale_codec.h')], |
| 22 |
include_dirs=[str(cwd), cdir], |
| 23 |
separate_module_files=[cwd.join('locale_codec.c')]) |
| 24 |
|
| 25 |
def llexternal(*args, **kwargs): |
| 26 |
kwargs.setdefault('compilation_info', eci) |
| 27 |
kwargs.setdefault('sandboxsafe', True) |
| 28 |
kwargs.setdefault('_nowrapper', True) |
| 29 |
return rffi.llexternal(*args, **kwargs) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
RAW_WCHARP = lltype.Ptr(lltype.Array(rffi.WCHAR_T, hints={'nolength': True})) |
| 34 |
pypy_char2wchar = llexternal('pypy_char2wchar', [rffi.CCHARP, rffi.SIZE_TP], |
| 35 |
RAW_WCHARP) |
| 36 |
pypy_char2wchar_strict = llexternal('pypy_char2wchar_strict', |
| 37 |
[rffi.CCHARP, rffi.SIZE_TP], RAW_WCHARP) |
| 38 |
pypy_char2wchar_free = llexternal('pypy_char2wchar_free', [RAW_WCHARP], |
| 39 |
lltype.Void) |
| 40 |
pypy_wchar2char = llexternal('pypy_wchar2char', [RAW_WCHARP, rffi.SIZE_TP], |
| 41 |
rffi.CCHARP) |
| 42 |
pypy_wchar2char_strict = llexternal('pypy_wchar2char_strict', |
| 43 |
[RAW_WCHARP, rffi.SIZE_TP], rffi.CCHARP) |
| 44 |
pypy_wchar2char_free = llexternal('pypy_wchar2char_free', [rffi.CCHARP], |
| 45 |
lltype.Void) |
| 46 |
|
| 47 |
def utf8_encode_locale_strict(utf8, ulen): |
| 48 |
"""Encode unicode via the locale codecs (POSIX wcstombs) with the |
| 49 |
strict handler. |
| 50 |
|
| 51 |
The errorhandler is never called |
| 52 |
""" |
| 53 |
errorhandler = default_unicode_error_encode |
| 54 |
|
| 55 |
with lltype.scoped_alloc(rffi.SIZE_TP.TO, 1) as errorposp: |
| 56 |
with scoped_utf82rawwcharp(utf8, ulen) as ubuf: |
| 57 |
sbuf = pypy_wchar2char_strict(ubuf, errorposp) |
| 58 |
try: |
| 59 |
if not sbuf: |
| 60 |
errorpos = rffi.cast(lltype.Signed, errorposp[0]) |
| 61 |
if errorpos == -1: |
| 62 |
raise MemoryError |
| 63 |
errmsg = _errmsg("pypy_wchar2char") |
| 64 |
u = utf8.decode('utf-8') |
| 65 |
errorhandler('strict', 'filesystemencoding', errmsg, u, |
| 66 |
errorpos, errorpos + 1) |
| 67 |
return rffi.charp2str(sbuf) |
| 68 |
finally: |
| 69 |
pypy_wchar2char_free(sbuf) |
| 70 |
|
| 71 |
|
| 72 |
def utf8_encode_locale_surrogateescape(utf8, ulen): |
| 73 |
"""Encode unicode via the locale codecs (POSIX wcstombs) with the |
| 74 |
surrogateescape handler. |
| 75 |
|
| 76 |
The errorhandler raises a UnicodeEncodeError |
| 77 |
""" |
| 78 |
if not we_are_translated(): |
| 79 |
assert codepoints_in_utf8(utf8) == ulen |
| 80 |
errorhandler = default_unicode_error_encode |
| 81 |
|
| 82 |
with lltype.scoped_alloc(rffi.SIZE_TP.TO, 1) as errorposp: |
| 83 |
with scoped_utf82rawwcharp(utf8, ulen) as ubuf: |
| 84 |
sbuf = pypy_wchar2char(ubuf, errorposp) |
| 85 |
try: |
| 86 |
if not sbuf: |
| 87 |
errorpos = rffi.cast(lltype.Signed, errorposp[0]) |
| 88 |
if errorpos == -1: |
| 89 |
raise MemoryError |
| 90 |
errmsg = _errmsg("pypy_wchar2char") |
| 91 |
u = utf8.decode('utf-8') |
| 92 |
errorhandler('surrogateescape', 'filesystemencoding', errmsg, u, |
| 93 |
errorpos, errorpos + 1) |
| 94 |
return rffi.charp2str(sbuf) |
| 95 |
finally: |
| 96 |
pypy_wchar2char_free(sbuf) |
| 97 |
|
| 98 |
def utf8_encode_locale(utf8, ulen, errors): |
| 99 |
if errors == 'strict': |
| 100 |
return utf8_encode_locale_strict(utf8, ulen) |
| 101 |
return utf8_encode_locale_surrogateescape(utf8, ulen) |
| 102 |
|
| 103 |
def str_decode_locale_strict(s): |
| 104 |
"""Decode strs via the locale codecs (POSIX mrbtowc) with the |
| 105 |
surrogateescape handler. |
| 106 |
|
| 107 |
The errorhandler is never called |
| 108 |
errors. |
| 109 |
""" |
| 110 |
errorhandler = default_unicode_error_decode |
| 111 |
|
| 112 |
with lltype.scoped_alloc(rffi.SIZE_TP.TO, 1) as sizep: |
| 113 |
with rffi.scoped_str2charp(s) as sbuf: |
| 114 |
ubuf = pypy_char2wchar_strict(sbuf, sizep) |
| 115 |
try: |
| 116 |
if not ubuf: |
| 117 |
errmsg = _errmsg("pypy_char2wchar_strict") |
| 118 |
errorhandler('strict', 'filesystemencoding', errmsg, s, 0, 1) |
| 119 |
size = rffi.cast(lltype.Signed, sizep[0]) |
| 120 |
return rawwcharp2utf8en(ubuf, size), size |
| 121 |
finally: |
| 122 |
pypy_char2wchar_free(ubuf) |
| 123 |
|
| 124 |
|
| 125 |
def str_decode_locale_surrogateescape(s): |
| 126 |
"""Decode strs via the locale codecs (POSIX mrbtowc) with the |
| 127 |
surrogateescape handler. |
| 128 |
|
| 129 |
The errorhandler is never called |
| 130 |
errors. |
| 131 |
""" |
| 132 |
errorhandler = default_unicode_error_decode |
| 133 |
|
| 134 |
with lltype.scoped_alloc(rffi.SIZE_TP.TO, 1) as sizep: |
| 135 |
with rffi.scoped_str2charp(s) as sbuf: |
| 136 |
ubuf = pypy_char2wchar(sbuf, sizep) |
| 137 |
try: |
| 138 |
if not ubuf: |
| 139 |
errmsg = _errmsg("pypy_char2wchar") |
| 140 |
errorhandler('strict', 'filesystemencoding', errmsg, s, 0, 1) |
| 141 |
size = rffi.cast(lltype.Signed, sizep[0]) |
| 142 |
return rawwcharp2utf8en(ubuf, size), size |
| 143 |
finally: |
| 144 |
pypy_char2wchar_free(ubuf) |
| 145 |
|
| 146 |
def str_decode_locale(s, errors): |
| 147 |
if errors == 'strict': |
| 148 |
return str_decode_locale_strict(s) |
| 149 |
return str_decode_locale_surrogateescape(s) |
| 150 |
|
| 151 |
def _errmsg(what): |
| 152 |
|
| 153 |
return "%s failed" % what |
| 154 |
|
| 155 |
|
| 156 |
class scoped_utf82rawwcharp: |
| 157 |
def __init__(self, value, lgt): |
| 158 |
if value is not None: |
| 159 |
self.buf = utf82rawwcharp(value, lgt) |
| 160 |
else: |
| 161 |
self.buf = lltype.nullptr(RAW_WCHARP.TO) |
| 162 |
def __enter__(self): |
| 163 |
return self.buf |
| 164 |
def __exit__(self, *args): |
| 165 |
if self.buf: |
| 166 |
lltype.free(self.buf, flavor='raw') |
| 167 |
|
| 168 |
def utf82rawwcharp(utf8, size): |
| 169 |
"""utf8, lgt -> raw wchar_t*""" |
| 170 |
if not we_are_translated(): |
| 171 |
assert codepoints_in_utf8(utf8) == size |
| 172 |
if _should_merge_surrogates(): |
| 173 |
size = _utf82rawwcharp_loop(utf8, size, lltype.nullptr(RAW_WCHARP.TO)) |
| 174 |
array = lltype.malloc(RAW_WCHARP.TO, size + 1, flavor='raw') |
| 175 |
array[size] = rffi.cast(rffi.WCHAR_T, u'\x00') |
| 176 |
_utf82rawwcharp_loop(utf8, size, array) |
| 177 |
return array |
| 178 |
utf82rawwcharp._annenforceargs_ = [str, int] |
| 179 |
|
| 180 |
def _utf82rawwcharp_loop(utf8, ulen, array): |
| 181 |
count = 0 |
| 182 |
u_iter = Utf8StringIterator(utf8) |
| 183 |
for oc in u_iter: |
| 184 |
if (_should_merge_surrogates() and 0xD800 <= oc <= 0xDBFF): |
| 185 |
try: |
| 186 |
oc1 = u_iter.next() |
| 187 |
if 0xDC00 <= oc1 <= 0xDFFF: |
| 188 |
if array: |
| 189 |
merged = (((oc & 0x03FF) << 10) | |
| 190 |
(oc1 & 0x03FF)) + 0x10000 |
| 191 |
array[count] = rffi.cast(rffi.WCHAR_T, merged) |
| 192 |
else: |
| 193 |
if array: |
| 194 |
array[count] = rffi.cast(rffi.WCHAR_T, oc) |
| 195 |
count += 1 |
| 196 |
array[count] = rffi.cast(rffi.WCHAR_T, oc1) |
| 197 |
except StopIteration: |
| 198 |
if array: |
| 199 |
array[count] = rffi.cast(rffi.WCHAR_T, oc) |
| 200 |
count += 1 |
| 201 |
break |
| 202 |
else: |
| 203 |
if array: |
| 204 |
array[count] = rffi.cast(rffi.WCHAR_T, oc) |
| 205 |
count += 1 |
| 206 |
return count |
| 207 |
_utf82rawwcharp_loop._annenforceargs_ = [str, int, None] |
| 208 |
|
| 209 |
|
| 210 |
def rawwcharp2utf8en(wcp, maxlen): |
| 211 |
b = StringBuilder(maxlen) |
| 212 |
i = 0 |
| 213 |
while i < maxlen: |
| 214 |
v = r_uint(wcp[i]) |
| 215 |
if v == 0: |
| 216 |
break |
| 217 |
b.append(unichr_as_utf8(v, True)) |
| 218 |
i += 1 |
| 219 |
return assert_str0(b.build()) |
| 220 |
rawwcharp2utf8en._annenforceargs_ = [None, int] |
| 221 |
|
| 222 |
|
| 223 |
def _should_merge_surrogates(): |
| 224 |
if we_are_translated(): |
| 225 |
unichar_size = rffi.sizeof(lltype.UniChar) |
| 226 |
else: |
| 227 |
unichar_size = 2 if sys.maxunicode == 0xFFFF else 4 |
| 228 |
return unichar_size == 2 and rffi.sizeof(rffi.WCHAR_T) == 4 |
| 229 |
|