| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4efb518 commit b2750b5
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,7 @@ PyAPI_FUNC(int) PyCodec_Register( | |||
| 27 | 27 | PyObject *search_function | |
| 28 | 28 | ); | |
| 29 | 29 | ||
| 30 | - /* Codec register lookup API. | ||
| 30 | + /* Codec registry lookup API. | ||
| 31 | 31 | ||
| 32 | 32 | Looks up the given encoding and returns a CodecInfo object with | |
| 33 | 33 | function attributes which implement the different aspects of | |
@@ -49,6 +49,17 @@ PyAPI_FUNC(PyObject *) _PyCodec_Lookup( | |||
| 49 | 49 | const char *encoding | |
| 50 | 50 | ); | |
| 51 | 51 | ||
| 52 | + /* Codec registry encoding check API. | ||
| 53 | + | ||
| 54 | + Returns 1/0 depending on whether there is a registered codec for | ||
| 55 | + the given encoding. | ||
| 56 | + | ||
| 57 | + */ | ||
| 58 | + | ||
| 59 | + PyAPI_FUNC(int) PyCodec_KnownEncoding( | ||
| 60 | + const char *encoding | ||
| 61 | + ); | ||
| 62 | + | ||
| 52 | 63 | /* Generic codec based encoding API. | |
| 53 | 64 | ||
| 54 | 65 | object is passed through the encoder function found for the given | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,8 +139,11 @@ typedef PY_UNICODE_TYPE Py_UNICODE; | |||
| 139 | 139 | ||
| 140 | 140 | # define PyUnicode_AsASCIIString PyUnicodeUCS2_AsASCIIString | |
| 141 | 141 | # define PyUnicode_AsCharmapString PyUnicodeUCS2_AsCharmapString | |
| 142 | + # define PyUnicode_AsDecodedObject PyUnicodeUCS2_AsDecodedObject | ||
| 143 | + # define PyUnicode_AsDecodedUnicode PyUnicodeUCS2_AsDecodedUnicode | ||
| 142 | 144 | # define PyUnicode_AsEncodedObject PyUnicodeUCS2_AsEncodedObject | |
| 143 | 145 | # define PyUnicode_AsEncodedString PyUnicodeUCS2_AsEncodedString | |
| 146 | + # define PyUnicode_AsEncodedUnicode PyUnicodeUCS2_AsEncodedUnicode | ||
| 144 | 147 | # define PyUnicode_AsLatin1String PyUnicodeUCS2_AsLatin1String | |
| 145 | 148 | # define PyUnicode_AsRawUnicodeEscapeString PyUnicodeUCS2_AsRawUnicodeEscapeString | |
| 146 | 149 | # define PyUnicode_AsUTF32String PyUnicodeUCS2_AsUTF32String | |
@@ -233,8 +236,11 @@ typedef PY_UNICODE_TYPE Py_UNICODE; | |||
| 233 | 236 | ||
| 234 | 237 | # define PyUnicode_AsASCIIString PyUnicodeUCS4_AsASCIIString | |
| 235 | 238 | # define PyUnicode_AsCharmapString PyUnicodeUCS4_AsCharmapString | |
| 239 | + # define PyUnicode_AsDecodedObject PyUnicodeUCS4_AsDecodedObject | ||
| 240 | + # define PyUnicode_AsDecodedUnicode PyUnicodeUCS4_AsDecodedUnicode | ||
| 236 | 241 | # define PyUnicode_AsEncodedObject PyUnicodeUCS4_AsEncodedObject | |
| 237 | 242 | # define PyUnicode_AsEncodedString PyUnicodeUCS4_AsEncodedString | |
| 243 | + # define PyUnicode_AsEncodedUnicode PyUnicodeUCS4_AsEncodedUnicode | ||
| 238 | 244 | # define PyUnicode_AsLatin1String PyUnicodeUCS4_AsLatin1String | |
| 239 | 245 | # define PyUnicode_AsRawUnicodeEscapeString PyUnicodeUCS4_AsRawUnicodeEscapeString | |
| 240 | 246 | # define PyUnicode_AsUTF32String PyUnicodeUCS4_AsUTF32String | |
@@ -744,6 +750,24 @@ PyAPI_FUNC(PyObject*) PyUnicode_Decode( | |||
| 744 | 750 | const char *errors /* error handling */ | |
| 745 | 751 | ); | |
| 746 | 752 | ||
| 753 | + /* Decode a Unicode object unicode and return the result as Python | ||
| 754 | + object. */ | ||
| 755 | + | ||
| 756 | + PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedObject( | ||
| 757 | + PyObject *unicode, /* Unicode object */ | ||
| 758 | + const char *encoding, /* encoding */ | ||
| 759 | + const char *errors /* error handling */ | ||
| 760 | + ); | ||
| 761 | + | ||
| 762 | + /* Decode a Unicode object unicode and return the result as Unicode | ||
| 763 | + object. */ | ||
| 764 | + | ||
| 765 | + PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedUnicode( | ||
| 766 | + PyObject *unicode, /* Unicode object */ | ||
| 767 | + const char *encoding, /* encoding */ | ||
| 768 | + const char *errors /* error handling */ | ||
| 769 | + ); | ||
| 770 | + | ||
| 747 | 771 | /* Encodes a Py_UNICODE buffer of the given size and returns a | |
| 748 | 772 | Python string object. */ | |
| 749 | 773 | ||
@@ -772,11 +796,21 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString( | |||
| 772 | 796 | const char *errors /* error handling */ | |
| 773 | 797 | ); | |
| 774 | 798 | ||
| 799 | + /* Encodes a Unicode object and returns the result as Unicode | ||
| 800 | + object. */ | ||
| 801 | + | ||
| 802 | + PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedUnicode( | ||
| 803 | + PyObject *unicode, /* Unicode object */ | ||
| 804 | + const char *encoding, /* encoding */ | ||
| 805 | + const char *errors /* error handling */ | ||
| 806 | + ); | ||
| 807 | + | ||
| 808 | + /* Build an encoding map. */ | ||
| 809 | + | ||
| 775 | 810 | PyAPI_FUNC(PyObject*) PyUnicode_BuildEncodingMap( | |
| 776 | 811 | PyObject* string /* 256 character map */ | |
| 777 | 812 | ); | |
| 778 | 813 | ||
| 779 | - | ||
| 780 | 814 | /* --- UTF-7 Codecs ------------------------------------------------------- */ | |
| 781 | 815 | ||
| 782 | 816 | PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -183,6 +183,8 @@ def insertion_sort(base, extended, errors): | |||
| 183 | 183 | def punycode_decode(text, errors): | |
| 184 | 184 | if isinstance(text, str): | |
| 185 | 185 | text = text.encode("ascii") | |
| 186 | + if isinstance(text, memoryview): | ||
| 187 | + text = bytes(text) | ||
| 186 | 188 | pos = text.rfind(b"-") | |
| 187 | 189 | if pos == -1: | |
| 188 | 190 | base = "" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -725,7 +725,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 725 | 725 | "string argument without an encoding"); | |
| 726 | 726 | return -1; | |
| 727 | 727 | } | |
| 728 | - encoded = PyCodec_Encode(arg, encoding, errors); | ||
| 728 | + encoded = PyUnicode_AsEncodedString(arg, encoding, errors); | ||
| 729 | 729 | if (encoded == NULL) | |
| 730 | 730 | return -1; | |
| 731 | 731 | assert(PyBytes_Check(encoded)); | |
@@ -2854,7 +2854,7 @@ bytes_decode(PyObject *self, PyObject *args) | |||
| 2854 | 2854 | return NULL; | |
| 2855 | 2855 | if (encoding == NULL) | |
| 2856 | 2856 | encoding = PyUnicode_GetDefaultEncoding(); | |
| 2857 | - return PyCodec_Decode(self, encoding, errors); | ||
| 2857 | + return PyUnicode_FromEncodedObject(self, encoding, errors); | ||
| 2858 | 2858 | } | |
| 2859 | 2859 | ||
| 2860 | 2860 | PyDoc_STRVAR(alloc_doc, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2713,7 +2713,7 @@ string_decode(PyObject *self, PyObject *args) | |||
| 2713 | 2713 | return NULL; | |
| 2714 | 2714 | if (encoding == NULL) | |
| 2715 | 2715 | encoding = PyUnicode_GetDefaultEncoding(); | |
| 2716 | - return PyCodec_Decode(self, encoding, errors); | ||
| 2716 | + return PyUnicode_FromEncodedObject(self, encoding, errors); | ||
| 2717 | 2717 | } | |
| 2718 | 2718 | ||
| 2719 | 2719 | ||
@@ -2899,7 +2899,7 @@ string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||
| 2899 | 2899 | "string argument without an encoding"); | |
| 2900 | 2900 | return NULL; | |
| 2901 | 2901 | } | |
| 2902 | - new = PyCodec_Encode(x, encoding, errors); | ||
| 2902 | + new = PyUnicode_AsEncodedString(x, encoding, errors); | ||
| 2903 | 2903 | if (new == NULL) | |
| 2904 | 2904 | return NULL; | |
| 2905 | 2905 | assert(PyBytes_Check(new)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1099,14 +1099,18 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, | |||
| 1099 | 1099 | ||
| 1100 | 1100 | /* Coerce object */ | |
| 1101 | 1101 | if (PyBytes_Check(obj)) { | |
| 1102 | - s = PyBytes_AS_STRING(obj); | ||
| 1103 | - len = PyBytes_GET_SIZE(obj); | ||
| 1104 | - } | ||
| 1102 | + s = PyBytes_AS_STRING(obj); | ||
| 1103 | + len = PyBytes_GET_SIZE(obj); | ||
| 1104 | + } | ||
| 1105 | + else if (PyByteArray_Check(obj)) { | ||
| 1106 | + s = PyByteArray_AS_STRING(obj); | ||
| 1107 | + len = PyByteArray_GET_SIZE(obj); | ||
| 1108 | + } | ||
| 1105 | 1109 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { | |
| 1106 | 1110 | /* Overwrite the error message with something more useful in | |
| 1107 | 1111 | case of a TypeError. */ | |
| 1108 | 1112 | if (PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 1109 | - PyErr_Format(PyExc_TypeError, | ||
| 1113 | + PyErr_Format(PyExc_TypeError, | ||
| 1110 | 1114 | "coercing to Unicode: need string or buffer, " | |
| 1111 | 1115 | "%.80s found", | |
| 1112 | 1116 | Py_TYPE(obj)->tp_name); | |
@@ -1188,7 +1192,7 @@ PyObject *PyUnicode_Decode(const char *s, | |||
| 1188 | 1192 | goto onError; | |
| 1189 | 1193 | if (!PyUnicode_Check(unicode)) { | |
| 1190 | 1194 | PyErr_Format(PyExc_TypeError, | |
| 1191 | - "decoder did not return an unicode object (type=%.400s)", | ||
| 1195 | + "decoder did not return a unicode object (type=%.400s)", | ||
| 1192 | 1196 | Py_TYPE(unicode)->tp_name); | |
| 1193 | 1197 | Py_DECREF(unicode); | |
| 1194 | 1198 | goto onError; | |
@@ -1225,6 +1229,37 @@ PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, | |||
| 1225 | 1229 | return NULL; | |
| 1226 | 1230 | } | |
| 1227 | 1231 | ||
| 1232 | + PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, | ||
| 1233 | + const char *encoding, | ||
| 1234 | + const char *errors) | ||
| 1235 | + { | ||
| 1236 | + PyObject *v; | ||
| 1237 | + | ||
| 1238 | + if (!PyUnicode_Check(unicode)) { | ||
| 1239 | + PyErr_BadArgument(); | ||
| 1240 | + goto onError; | ||
| 1241 | + } | ||
| 1242 | + | ||
| 1243 | + if (encoding == NULL) | ||
| 1244 | + encoding = PyUnicode_GetDefaultEncoding(); | ||
| 1245 | + | ||
| 1246 | + /* Decode via the codec registry */ | ||
| 1247 | + v = PyCodec_Decode(unicode, encoding, errors); | ||
| 1248 | + if (v == NULL) | ||
| 1249 | + goto onError; | ||
| 1250 | + if (!PyUnicode_Check(v)) { | ||
| 1251 | + PyErr_Format(PyExc_TypeError, | ||
| 1252 | + "decoder did not return a unicode object (type=%.400s)", | ||
| 1253 | + Py_TYPE(v)->tp_name); | ||
| 1254 | + Py_DECREF(v); | ||
| 1255 | + goto onError; | ||
| 1256 | + } | ||
| 1257 | + return v; | ||
| 1258 | + | ||
| 1259 | + onError: | ||
| 1260 | + return NULL; | ||
| 1261 | + } | ||
| 1262 | + | ||
| 1228 | 1263 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, | |
| 1229 | 1264 | Py_ssize_t size, | |
| 1230 | 1265 | const char *encoding, | |
@@ -1296,7 +1331,54 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode, | |||
| 1296 | 1331 | v = PyCodec_Encode(unicode, encoding, errors); | |
| 1297 | 1332 | if (v == NULL) | |
| 1298 | 1333 | goto onError; | |
| 1299 | - assert(PyBytes_Check(v)); | ||
| 1334 | + if (PyByteArray_Check(v)) { | ||
| 1335 | + char msg[100]; | ||
| 1336 | + PyOS_snprintf(msg, sizeof(msg), | ||
| 1337 | + "encoder %s returned buffer instead of bytes", | ||
| 1338 | + encoding); | ||
| 1339 | + if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { | ||
| 1340 | + v = NULL; | ||
| 1341 | + goto onError; | ||
| 1342 | + } | ||
| 1343 | + v = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); | ||
| 1344 | + } | ||
| 1345 | + else if (!PyBytes_Check(v)) { | ||
| 1346 | + PyErr_Format(PyExc_TypeError, | ||
| 1347 | + "encoder did not return a bytes object (type=%.400s)", | ||
| 1348 | + Py_TYPE(v)->tp_name); | ||
| 1349 | + v = NULL; | ||
| 1350 | + } | ||
| 1351 | + return v; | ||
| 1352 | + | ||
| 1353 | + onError: | ||
| 1354 | + return NULL; | ||
| 1355 | + } | ||
| 1356 | + | ||
| 1357 | + PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, | ||
| 1358 | + const char *encoding, | ||
| 1359 | + const char *errors) | ||
| 1360 | + { | ||
| 1361 | + PyObject *v; | ||
| 1362 | + | ||
| 1363 | + if (!PyUnicode_Check(unicode)) { | ||
| 1364 | + PyErr_BadArgument(); | ||
| 1365 | + goto onError; | ||
| 1366 | + } | ||
| 1367 | + | ||
| 1368 | + if (encoding == NULL) | ||
| 1369 | + encoding = PyUnicode_GetDefaultEncoding(); | ||
| 1370 | + | ||
| 1371 | + /* Encode via the codec registry */ | ||
| 1372 | + v = PyCodec_Encode(unicode, encoding, errors); | ||
| 1373 | + if (v == NULL) | ||
| 1374 | + goto onError; | ||
| 1375 | + if (!PyUnicode_Check(v)) { | ||
| 1376 | + PyErr_Format(PyExc_TypeError, | ||
| 1377 | + "encoder did not return an unicode object (type=%.400s)", | ||
| 1378 | + Py_TYPE(v)->tp_name); | ||
| 1379 | + Py_DECREF(v); | ||
| 1380 | + goto onError; | ||
| 1381 | + } | ||
| 1300 | 1382 | return v; | |
| 1301 | 1383 | ||
| 1302 | 1384 | onError: | |
@@ -6617,7 +6699,7 @@ unicode_encode(PyUnicodeObject *self, PyObject *args) | |||
| 6617 | 6699 | ||
| 6618 | 6700 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) | |
| 6619 | 6701 | return NULL; | |
| 6620 | - v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); | ||
| 6702 | + v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); | ||
| 6621 | 6703 | if (v == NULL) | |
| 6622 | 6704 | goto onError; | |
| 6623 | 6705 | if (!PyBytes_Check(v)) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -183,6 +183,23 @@ PyObject *_PyCodec_Lookup(const char *encoding) | |||
| 183 | 183 | return NULL; | |
| 184 | 184 | } | |
| 185 | 185 | ||
| 186 | + /* Codec registry encoding check API. */ | ||
| 187 | + | ||
| 188 | + int PyCodec_KnownEncoding(const char *encoding) | ||
| 189 | + { | ||
| 190 | + PyObject *codecs; | ||
| 191 | + | ||
| 192 | + codecs = _PyCodec_Lookup(encoding); | ||
| 193 | + if (!codecs) { | ||
| 194 | + PyErr_Clear(); | ||
| 195 | + return 0; | ||
| 196 | + } | ||
| 197 | + else { | ||
| 198 | + Py_DECREF(codecs); | ||
| 199 | + return 1; | ||
| 200 | + } | ||
| 201 | + } | ||
| 202 | + | ||
| 186 | 203 | static | |
| 187 | 204 | PyObject *args_tuple(PyObject *object, | |
| 188 | 205 | const char *errors) | |
@@ -344,32 +361,20 @@ PyObject *PyCodec_Encode(PyObject *object, | |||
| 344 | 361 | "encoder must return a tuple (object, integer)"); | |
| 345 | 362 | goto onError; | |
| 346 | 363 | } | |
| 347 | - v = PyTuple_GET_ITEM(result, 0); | ||
| 348 | - if (PyByteArray_Check(v)) { | ||
| 349 | - char msg[100]; | ||
| 350 | - PyOS_snprintf(msg, sizeof(msg), | ||
| 351 | - "encoder %s returned buffer instead of bytes", | ||
| 352 | - encoding); | ||
| 353 | - if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { | ||
| 354 | - v = NULL; | ||
| 355 | - goto onError; | ||
| 356 | - } | ||
| 357 | - v = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); | ||
| 358 | - } | ||
| 359 | - else if (PyBytes_Check(v)) | ||
| 360 | - Py_INCREF(v); | ||
| 361 | - else { | ||
| 362 | - PyErr_SetString(PyExc_TypeError, | ||
| 363 | - "encoding must return a tuple(bytes, integer)"); | ||
| 364 | - v = NULL; | ||
| 365 | - } | ||
| 364 | + v = PyTuple_GET_ITEM(result,0); | ||
| 365 | + Py_INCREF(v); | ||
| 366 | 366 | /* We don't check or use the second (integer) entry. */ | |
| 367 | 367 | ||
| 368 | + Py_DECREF(args); | ||
| 369 | + Py_DECREF(encoder); | ||
| 370 | + Py_DECREF(result); | ||
| 371 | + return v; | ||
| 372 | + | ||
| 368 | 373 | onError: | |
| 369 | 374 | Py_XDECREF(result); | |
| 370 | 375 | Py_XDECREF(args); | |
| 371 | 376 | Py_XDECREF(encoder); | |
| 372 | - return v; | ||
| 377 | + return NULL; | ||
| 373 | 378 | } | |
| 374 | 379 | ||
| 375 | 380 | /* Decode an object (usually a Python string) using the given encoding | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -261,14 +261,10 @@ Py_InitializeEx(int install_sigs) | |||
| 261 | 261 | ||
| 262 | 262 | codeset = nl_langinfo(CODESET); | |
| 263 | 263 | if (codeset && *codeset) { | |
| 264 | - PyObject *enc = PyCodec_Encoder(codeset); | ||
| 265 | - if (enc) { | ||
| 266 | - codeset = strdup(codeset); | ||
| 267 | - Py_DECREF(enc); | ||
| 268 | - } else { | ||
| 269 | - codeset = NULL; | ||
| 270 | - PyErr_Clear(); | ||
| 271 | - } | ||
| 264 | + if (PyCodec_KnownEncoding(codeset)) | ||
| 265 | + codeset = strdup(codeset); | ||
| 266 | + else | ||
| 267 | + codeset = NULL; | ||
| 272 | 268 | } else | |
| 273 | 269 | codeset = NULL; | |
| 274 | 270 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments