Kaydet (Commit) 1db7c13b authored tarafından Martin v. Löwis's avatar Martin v. Löwis

Port encoders from Py_UNICODE API to unicode object API.

üst df8077ec
......@@ -1088,6 +1088,12 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF7(
PyObject *unicode, /* Unicode object */
int base64SetO, /* Encode RFC2152 Set O characters in base64 */
int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
const char *errors /* error handling */
);
#endif
/* --- UTF-8 Codecs ------------------------------------------------------- */
......@@ -1195,6 +1201,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
const char *errors, /* error handling */
int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
);
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
PyObject *object, /* Unicode object */
const char *errors, /* error handling */
int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
);
#endif
/* --- UTF-16 Codecs ------------------------------------------------------ */
......@@ -1275,6 +1286,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
const char *errors, /* error handling */
int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
);
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
PyObject* unicode, /* Unicode object */
const char *errors, /* error handling */
int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
);
#endif
/* --- Unicode-Escape Codecs ---------------------------------------------- */
......
......@@ -235,8 +235,10 @@ unicode_internal_decode(PyObject *self,
return NULL;
if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) < 0)
return NULL;
Py_INCREF(obj);
return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
return codec_tuple(obj, PyUnicode_GET_LENGTH(obj));
}
else {
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
......@@ -676,10 +678,12 @@ unicode_internal_encode(PyObject *self,
return NULL;
if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) < 0)
return NULL;
data = PyUnicode_AS_DATA(obj);
size = PyUnicode_GET_DATA_SIZE(obj);
return codec_tuple(PyBytes_FromStringAndSize(data, size),
PyUnicode_GET_SIZE(obj));
PyUnicode_GET_LENGTH(obj));
}
else {
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
......@@ -700,14 +704,10 @@ utf_7_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
0,
0,
errors),
PyUnicode_GET_SIZE(str));
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -752,13 +752,10 @@ utf_16_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
byteorder),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -775,13 +772,10 @@ utf_16_le_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
-1),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -798,13 +792,10 @@ utf_16_be_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
+1),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -829,13 +820,10 @@ utf_32_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
byteorder),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -852,13 +840,10 @@ utf_32_le_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
-1),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -875,13 +860,10 @@ utf_32_be_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
+1),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -898,11 +880,10 @@ unicode_escape_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str)),
PyUnicode_GET_SIZE(str));
v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -919,12 +900,10 @@ raw_unicode_escape_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str)),
PyUnicode_GET_SIZE(str));
v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -941,13 +920,10 @@ latin_1_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeLatin1(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_AsLatin1String(str, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -964,13 +940,10 @@ ascii_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeASCII(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors),
PyUnicode_GET_SIZE(str));
v = codec_tuple(_PyUnicode_AsASCIIString(str, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -990,10 +963,10 @@ charmap_encode(PyObject *self,
mapping = NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
PyUnicode_GET_SIZE(str));
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -1021,13 +994,10 @@ mbcs_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeMBCS(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors),
PyUnicode_GET_SIZE(str));
v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
PyUnicode_GET_LENGTH(str));
Py_DECREF(str);
return v;
}
......@@ -1045,7 +1015,7 @@ code_page_encode(PyObject *self,
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
if (str == NULL || PyUnicode_READY(str) < 0)
return NULL;
v = codec_tuple(PyUnicode_EncodeCodePage(code_page,
str,
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment