Commit 1db7c13b authored by Martin v. Löwis's avatar Martin v. Löwis

Port encoders from Py_UNICODE API to unicode object API.

parent df8077ec
...@@ -1088,6 +1088,12 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7( ...@@ -1088,6 +1088,12 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */ int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
const char *errors /* error handling */ 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 #endif
/* --- UTF-8 Codecs ------------------------------------------------------- */ /* --- UTF-8 Codecs ------------------------------------------------------- */
...@@ -1195,6 +1201,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32( ...@@ -1195,6 +1201,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
const char *errors, /* error handling */ const char *errors, /* error handling */
int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */ 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 #endif
/* --- UTF-16 Codecs ------------------------------------------------------ */ /* --- UTF-16 Codecs ------------------------------------------------------ */
...@@ -1275,6 +1286,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16( ...@@ -1275,6 +1286,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
const char *errors, /* error handling */ const char *errors, /* error handling */
int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */ 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 #endif
/* --- Unicode-Escape Codecs ---------------------------------------------- */ /* --- Unicode-Escape Codecs ---------------------------------------------- */
......
...@@ -235,8 +235,10 @@ unicode_internal_decode(PyObject *self, ...@@ -235,8 +235,10 @@ unicode_internal_decode(PyObject *self,
return NULL; return NULL;
if (PyUnicode_Check(obj)) { if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) < 0)
return NULL;
Py_INCREF(obj); Py_INCREF(obj);
return codec_tuple(obj, PyUnicode_GET_SIZE(obj)); return codec_tuple(obj, PyUnicode_GET_LENGTH(obj));
} }
else { else {
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
...@@ -676,10 +678,12 @@ unicode_internal_encode(PyObject *self, ...@@ -676,10 +678,12 @@ unicode_internal_encode(PyObject *self,
return NULL; return NULL;
if (PyUnicode_Check(obj)) { if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) < 0)
return NULL;
data = PyUnicode_AS_DATA(obj); data = PyUnicode_AS_DATA(obj);
size = PyUnicode_GET_DATA_SIZE(obj); size = PyUnicode_GET_DATA_SIZE(obj);
return codec_tuple(PyBytes_FromStringAndSize(data, size), return codec_tuple(PyBytes_FromStringAndSize(data, size),
PyUnicode_GET_SIZE(obj)); PyUnicode_GET_LENGTH(obj));
} }
else { else {
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
...@@ -700,14 +704,10 @@ utf_7_encode(PyObject *self, ...@@ -700,14 +704,10 @@ utf_7_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
0,
0,
errors),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -752,13 +752,10 @@ utf_16_encode(PyObject *self, ...@@ -752,13 +752,10 @@ utf_16_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
errors,
byteorder),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -775,13 +772,10 @@ utf_16_le_encode(PyObject *self, ...@@ -775,13 +772,10 @@ utf_16_le_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
errors,
-1),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -798,13 +792,10 @@ utf_16_be_encode(PyObject *self, ...@@ -798,13 +792,10 @@ utf_16_be_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
errors,
+1),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -829,13 +820,10 @@ utf_32_encode(PyObject *self, ...@@ -829,13 +820,10 @@ utf_32_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
errors,
byteorder),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -852,13 +840,10 @@ utf_32_le_encode(PyObject *self, ...@@ -852,13 +840,10 @@ utf_32_le_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
errors,
-1),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -875,13 +860,10 @@ utf_32_be_encode(PyObject *self, ...@@ -875,13 +860,10 @@ utf_32_be_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str), v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
PyUnicode_GET_SIZE(str), PyUnicode_GET_LENGTH(str));
errors,
+1),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -898,11 +880,10 @@ unicode_escape_encode(PyObject *self, ...@@ -898,11 +880,10 @@ unicode_escape_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str), v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
PyUnicode_GET_SIZE(str)), PyUnicode_GET_LENGTH(str));
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -919,12 +900,10 @@ raw_unicode_escape_encode(PyObject *self, ...@@ -919,12 +900,10 @@ raw_unicode_escape_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape( v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
PyUnicode_AS_UNICODE(str), PyUnicode_GET_LENGTH(str));
PyUnicode_GET_SIZE(str)),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -941,13 +920,10 @@ latin_1_encode(PyObject *self, ...@@ -941,13 +920,10 @@ latin_1_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeLatin1( v = codec_tuple(_PyUnicode_AsLatin1String(str, errors),
PyUnicode_AS_UNICODE(str), PyUnicode_GET_LENGTH(str));
PyUnicode_GET_SIZE(str),
errors),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -964,13 +940,10 @@ ascii_encode(PyObject *self, ...@@ -964,13 +940,10 @@ ascii_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeASCII( v = codec_tuple(_PyUnicode_AsASCIIString(str, errors),
PyUnicode_AS_UNICODE(str), PyUnicode_GET_LENGTH(str));
PyUnicode_GET_SIZE(str),
errors),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -990,10 +963,10 @@ charmap_encode(PyObject *self, ...@@ -990,10 +963,10 @@ charmap_encode(PyObject *self,
mapping = NULL; mapping = NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors), v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
PyUnicode_GET_SIZE(str)); PyUnicode_GET_LENGTH(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -1021,13 +994,10 @@ mbcs_encode(PyObject *self, ...@@ -1021,13 +994,10 @@ mbcs_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeMBCS( v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
PyUnicode_AS_UNICODE(str), PyUnicode_GET_LENGTH(str));
PyUnicode_GET_SIZE(str),
errors),
PyUnicode_GET_SIZE(str));
Py_DECREF(str); Py_DECREF(str);
return v; return v;
} }
...@@ -1045,7 +1015,7 @@ code_page_encode(PyObject *self, ...@@ -1045,7 +1015,7 @@ code_page_encode(PyObject *self,
return NULL; return NULL;
str = PyUnicode_FromObject(str); str = PyUnicode_FromObject(str);
if (str == NULL) if (str == NULL || PyUnicode_READY(str) < 0)
return NULL; return NULL;
v = codec_tuple(PyUnicode_EncodeCodePage(code_page, v = codec_tuple(PyUnicode_EncodeCodePage(code_page,
str, 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