mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-44029: Remove Py_UNICODE APIs (GH-25881)
Remove deprecated `Py_UNICODE` APIs: `PyUnicode_Encode`, `PyUnicode_EncodeUTF7`, `PyUnicode_EncodeUTF8`, `PyUnicode_EncodeUTF16`, `PyUnicode_EncodeUTF32`, `PyUnicode_EncodeLatin1`, `PyUnicode_EncodeMBCS`, `PyUnicode_EncodeDecimal`, `PyUnicode_EncodeRawUnicodeEscape`, `PyUnicode_EncodeCharmap`, `PyUnicode_EncodeUnicodeEscape`, `PyUnicode_TransformDecimalToASCII`, `PyUnicode_TranslateCharmap`, `PyUnicodeEncodeError_Create`, `PyUnicodeTranslateError_Create`. See :pep:`393` and :pep:`624` for reference.
This commit is contained in:
parent
4ebf4a6bfa
commit
9ad8f109ac
11 changed files with 15 additions and 840 deletions
|
@ -3730,22 +3730,6 @@ PyUnicode_AsDecodedUnicode(PyObject *unicode,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_Encode(const Py_UNICODE *s,
|
||||
Py_ssize_t size,
|
||||
const char *encoding,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *v, *unicode;
|
||||
|
||||
unicode = PyUnicode_FromWideChar(s, size);
|
||||
if (unicode == NULL)
|
||||
return NULL;
|
||||
v = PyUnicode_AsEncodedString(unicode, encoding, errors);
|
||||
Py_DECREF(unicode);
|
||||
return v;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_AsEncodedObject(PyObject *unicode,
|
||||
const char *encoding,
|
||||
|
@ -5047,22 +5031,6 @@ encode_char:
|
|||
return NULL;
|
||||
return v;
|
||||
}
|
||||
PyObject *
|
||||
PyUnicode_EncodeUTF7(const Py_UNICODE *s,
|
||||
Py_ssize_t size,
|
||||
int base64SetO,
|
||||
int base64WhiteSpace,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *tmp = PyUnicode_FromWideChar(s, size);
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
result = _PyUnicode_EncodeUTF7(tmp, base64SetO,
|
||||
base64WhiteSpace, errors);
|
||||
Py_DECREF(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
#undef IS_BASE64
|
||||
#undef FROM_BASE64
|
||||
|
@ -5705,21 +5673,6 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
|
|||
}
|
||||
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeUTF8(const Py_UNICODE *s,
|
||||
Py_ssize_t size,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *v, *unicode;
|
||||
|
||||
unicode = PyUnicode_FromWideChar(s, size);
|
||||
if (unicode == NULL)
|
||||
return NULL;
|
||||
v = _PyUnicode_AsUTF8String(unicode, errors);
|
||||
Py_DECREF(unicode);
|
||||
return v;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_AsUTF8String(PyObject *unicode)
|
||||
{
|
||||
|
@ -6029,21 +5982,6 @@ _PyUnicode_EncodeUTF32(PyObject *str,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeUTF32(const Py_UNICODE *s,
|
||||
Py_ssize_t size,
|
||||
const char *errors,
|
||||
int byteorder)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *tmp = PyUnicode_FromWideChar(s, size);
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder);
|
||||
Py_DECREF(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_AsUTF32String(PyObject *unicode)
|
||||
{
|
||||
|
@ -6382,21 +6320,6 @@ _PyUnicode_EncodeUTF16(PyObject *str,
|
|||
#undef STORECHAR
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeUTF16(const Py_UNICODE *s,
|
||||
Py_ssize_t size,
|
||||
const char *errors,
|
||||
int byteorder)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *tmp = PyUnicode_FromWideChar(s, size);
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder);
|
||||
Py_DECREF(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_AsUTF16String(PyObject *unicode)
|
||||
{
|
||||
|
@ -6773,21 +6696,6 @@ PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
|
|||
return repr;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,
|
||||
Py_ssize_t size)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *tmp = PyUnicode_FromWideChar(s, size);
|
||||
if (tmp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result = PyUnicode_AsUnicodeEscapeString(tmp);
|
||||
Py_DECREF(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --- Raw Unicode Escape Codec ------------------------------------------- */
|
||||
|
||||
PyObject *
|
||||
|
@ -6988,19 +6896,6 @@ PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
|
|||
return repr;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
|
||||
Py_ssize_t size)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *tmp = PyUnicode_FromWideChar(s, size);
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
result = PyUnicode_AsRawUnicodeEscapeString(tmp);
|
||||
Py_DECREF(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --- Latin-1 Codec ------------------------------------------------------ */
|
||||
|
||||
PyObject *
|
||||
|
@ -7285,21 +7180,6 @@ unicode_encode_ucs1(PyObject *unicode,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Deprecated */
|
||||
PyObject *
|
||||
PyUnicode_EncodeLatin1(const Py_UNICODE *p,
|
||||
Py_ssize_t size,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *unicode = PyUnicode_FromWideChar(p, size);
|
||||
if (unicode == NULL)
|
||||
return NULL;
|
||||
result = unicode_encode_ucs1(unicode, errors, 256);
|
||||
Py_DECREF(unicode);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors)
|
||||
{
|
||||
|
@ -7426,21 +7306,6 @@ PyUnicode_DecodeASCII(const char *s,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Deprecated */
|
||||
PyObject *
|
||||
PyUnicode_EncodeASCII(const Py_UNICODE *p,
|
||||
Py_ssize_t size,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *unicode = PyUnicode_FromWideChar(p, size);
|
||||
if (unicode == NULL)
|
||||
return NULL;
|
||||
result = unicode_encode_ucs1(unicode, errors, 128);
|
||||
Py_DECREF(unicode);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
|
||||
{
|
||||
|
@ -8168,20 +8033,6 @@ encode_code_page(int code_page,
|
|||
return outbytes;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeMBCS(const Py_UNICODE *p,
|
||||
Py_ssize_t size,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *unicode, *res;
|
||||
unicode = PyUnicode_FromWideChar(p, size);
|
||||
if (unicode == NULL)
|
||||
return NULL;
|
||||
res = encode_code_page(CP_ACP, unicode, errors);
|
||||
Py_DECREF(unicode);
|
||||
return res;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_EncodeCodePage(int code_page,
|
||||
PyObject *unicode,
|
||||
|
@ -9008,22 +8859,6 @@ _PyUnicode_EncodeCharmap(PyObject *unicode,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Deprecated */
|
||||
PyObject *
|
||||
PyUnicode_EncodeCharmap(const Py_UNICODE *p,
|
||||
Py_ssize_t size,
|
||||
PyObject *mapping,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *unicode = PyUnicode_FromWideChar(p, size);
|
||||
if (unicode == NULL)
|
||||
return NULL;
|
||||
result = _PyUnicode_EncodeCharmap(unicode, mapping, errors);
|
||||
Py_DECREF(unicode);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_AsCharmapString(PyObject *unicode,
|
||||
PyObject *mapping)
|
||||
|
@ -9448,22 +9283,6 @@ _PyUnicode_TranslateCharmap(PyObject *input,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Deprecated. Use PyUnicode_Translate instead. */
|
||||
PyObject *
|
||||
PyUnicode_TranslateCharmap(const Py_UNICODE *p,
|
||||
Py_ssize_t size,
|
||||
PyObject *mapping,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *unicode = PyUnicode_FromWideChar(p, size);
|
||||
if (!unicode)
|
||||
return NULL;
|
||||
result = _PyUnicode_TranslateCharmap(unicode, mapping, errors);
|
||||
Py_DECREF(unicode);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_Translate(PyObject *str,
|
||||
PyObject *mapping,
|
||||
|
@ -9523,110 +9342,6 @@ _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
|
|||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
|
||||
Py_ssize_t length)
|
||||
{
|
||||
PyObject *decimal;
|
||||
Py_ssize_t i;
|
||||
Py_UCS4 maxchar;
|
||||
enum PyUnicode_Kind kind;
|
||||
const void *data;
|
||||
|
||||
maxchar = 127;
|
||||
for (i = 0; i < length; i++) {
|
||||
Py_UCS4 ch = s[i];
|
||||
if (ch > 127) {
|
||||
int decimal = Py_UNICODE_TODECIMAL(ch);
|
||||
if (decimal >= 0)
|
||||
ch = '0' + decimal;
|
||||
maxchar = Py_MAX(maxchar, ch);
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy to a new string */
|
||||
decimal = PyUnicode_New(length, maxchar);
|
||||
if (decimal == NULL)
|
||||
return decimal;
|
||||
kind = PyUnicode_KIND(decimal);
|
||||
data = PyUnicode_DATA(decimal);
|
||||
/* Iterate over code points */
|
||||
for (i = 0; i < length; i++) {
|
||||
Py_UCS4 ch = s[i];
|
||||
if (ch > 127) {
|
||||
int decimal = Py_UNICODE_TODECIMAL(ch);
|
||||
if (decimal >= 0)
|
||||
ch = '0' + decimal;
|
||||
}
|
||||
PyUnicode_WRITE(kind, data, i, ch);
|
||||
}
|
||||
return unicode_result(decimal);
|
||||
}
|
||||
/* --- Decimal Encoder ---------------------------------------------------- */
|
||||
|
||||
int
|
||||
PyUnicode_EncodeDecimal(Py_UNICODE *s,
|
||||
Py_ssize_t length,
|
||||
char *output,
|
||||
const char *errors)
|
||||
{
|
||||
PyObject *unicode;
|
||||
Py_ssize_t i;
|
||||
enum PyUnicode_Kind kind;
|
||||
const void *data;
|
||||
|
||||
if (output == NULL) {
|
||||
PyErr_BadArgument();
|
||||
return -1;
|
||||
}
|
||||
|
||||
unicode = PyUnicode_FromWideChar(s, length);
|
||||
if (unicode == NULL)
|
||||
return -1;
|
||||
|
||||
kind = PyUnicode_KIND(unicode);
|
||||
data = PyUnicode_DATA(unicode);
|
||||
|
||||
for (i=0; i < length; ) {
|
||||
PyObject *exc;
|
||||
Py_UCS4 ch;
|
||||
int decimal;
|
||||
Py_ssize_t startpos;
|
||||
|
||||
ch = PyUnicode_READ(kind, data, i);
|
||||
|
||||
if (Py_UNICODE_ISSPACE(ch)) {
|
||||
*output++ = ' ';
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
decimal = Py_UNICODE_TODECIMAL(ch);
|
||||
if (decimal >= 0) {
|
||||
*output++ = '0' + decimal;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (0 < ch && ch < 256) {
|
||||
*output++ = (char)ch;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
startpos = i;
|
||||
exc = NULL;
|
||||
raise_encode_exception(&exc, "decimal", unicode,
|
||||
startpos, startpos+1,
|
||||
"invalid decimal Unicode string");
|
||||
Py_XDECREF(exc);
|
||||
Py_DECREF(unicode);
|
||||
return -1;
|
||||
}
|
||||
/* 0-terminate the output string */
|
||||
*output++ = '\0';
|
||||
Py_DECREF(unicode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --- Helpers ------------------------------------------------------------ */
|
||||
|
||||
/* helper macro to fixup start/end slice values */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue