GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing bugs.python.org — bugs.python.org ? Include/.codecs.h.swp
? Include/.unicodeobject.h.swp
? Modules/._codecsmodule.c.swp
? Objects/.unicodeobject.c.swp
? Python/.codecs.c.swp
Index: Include/codecs.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/codecs.h,v
retrieving revision 2.3
diff -u -r2.3 codecs.h
--- Include/codecs.h 2000/08/03 16:24:24 2.3
+++ Include/codecs.h 2001/07/12 10:50:32
@@ -53,12 +53,18 @@
object is passed through the encoder function found for the given
encoding using the error handling method defined by errors. errors
- may be NULL to use the default method defined for the codec.
+ may be NULL to use the strict encoding.
Raises a LookupError in case no encoder can be found.
*/
+extern DL_IMPORT(PyObject *) PyCodec_EncodeEx(
+ PyObject *object,
+ const char *encoding,
+ PyObject *errors
+ );
+
extern DL_IMPORT(PyObject *) PyCodec_Encode(
PyObject *object,
const char *encoding,
@@ -111,11 +117,52 @@
/* Get a StreamWriter factory function for the given encoding. */
+extern DL_IMPORT(PyObject *) PyCodec_StreamWriterEx(
+ const char *encoding,
+ PyObject *stream,
+ PyObject *errors
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject *) PyCodec_StreamWriter(
const char *encoding,
PyObject *stream,
const char *errors
);
+
+/* Return a new reference to one of the builtin unicode
+ encode error handlers. error can be:
+
+ * NULL, Py_None, "strict" or u"strict" for
+ codecs.raise_unicodeencode_errors
+ * "ignore" or u"ignore" for codecs.ignore_unicodeencode_errors
+ * "replace" or u"replace" for codecs.replace_unicodeencode_errors
+ * a callable which will be returned directy
+
+ everything else will raise an exception */
+extern DL_IMPORT(PyObject *) PyCodec_UnicodeEncodeHandlerForObject(PyObject *error);
+
+/* Raises a Unicode exception */
+extern DL_IMPORT(void) PyCodec_RaiseUnicodeEncodeError(const char *encoding, Py_UNICODE c, int pos);
+
+/* Encode error handler that raises an exception */
+extern DL_IMPORT(PyObject *) PyCodec_RaiseUnicodeEncodeErrors(PyObject *self, PyObject *args);
+
+/* Encode error handler that returns a empty string and so ignores the
+ unencodable character */
+extern DL_IMPORT(PyObject *) PyCodec_IgnoreUnicodeEncodeErrors(PyObject *self, PyObject *args);
+
+/* Encode error handler that returns an Unicode replacement character
+ that will be used by the codec to replace the unencodable character */
+extern DL_IMPORT(PyObject *) PyCodec_ReplaceUnicodeEncodeErrors(PyObject *self, PyObject *args);
+
+/* Encode error handler that returns an XML character reference for the
+ unencodable character */
+extern DL_IMPORT(PyObject *) PyCodec_XMLCharRefReplaceUnicodeEncodeErrors(PyObject *self, PyObject *args);
+
+/* Encode error handler that returns an \u (or \U) escape sequence
+ for the unencodable character */
+extern DL_IMPORT(PyObject *) PyCodec_EscapeReplaceUnicodeEncodeErrors(PyObject *self, PyObject *args);
#ifdef __cplusplus
}
Index: Include/unicodeobject.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/unicodeobject.h,v
retrieving revision 2.27
diff -u -r2.27 unicodeobject.h
--- Include/unicodeobject.h 2001/06/27 22:08:26 2.27
+++ Include/unicodeobject.h 2001/07/12 10:50:33
@@ -419,9 +419,15 @@
const char *errors /* error handling */
);
-/* Encodes a Py_UNICODE buffer of the given size and returns a
- Python string object. */
+/* Encodes a Unicode object and returns a Python string object. */
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeEx(
+ PyObject *unicode, /* Unicode object */
+ const char *encoding, /* encoding */
+ PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_Encode(
const Py_UNICODE *s, /* Unicode char buffer */
int size, /* number of Py_UNICODE chars to encode */
@@ -429,9 +435,12 @@
const char *errors /* error handling */
);
+#define PyUnicode_AsEncodedStringEx PyUnicode_EncodeEx
+
/* Encodes a Unicode object and returns the result as Python string
object. */
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_AsEncodedString(
PyObject *unicode, /* Unicode object */
const char *encoding, /* encoding */
@@ -450,6 +459,12 @@
PyObject *unicode /* Unicode object */
);
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeUTF8Ex(
+ PyObject *unicode, /* Unicode object */
+ PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_EncodeUTF8(
const Py_UNICODE *data, /* Unicode char buffer */
int length, /* number of Py_UNICODE chars to encode */
@@ -517,6 +532,13 @@
*/
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeUTF16Ex(
+ PyObject *unicode, /* Unicode object */
+ PyObject *errors, /* error handling */
+ int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_EncodeUTF16(
const Py_UNICODE *data, /* Unicode char buffer */
int length, /* number of Py_UNICODE chars to encode */
@@ -536,6 +558,10 @@
PyObject *unicode /* Unicode object */
);
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeUnicodeEscapeEx(
+ PyObject *unicode /* Unicode object */
+ );
+
extern DL_IMPORT(PyObject*) PyUnicode_EncodeUnicodeEscape(
const Py_UNICODE *data, /* Unicode char buffer */
int length /* Number of Py_UNICODE chars to encode */
@@ -549,7 +575,7 @@
const char *errors /* error handling */
);
-extern DL_IMPORT(PyObject*) PyUnicode_AsRawUnicodeEscapeString(
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeRawUnicodeEscapeEx(
PyObject *unicode /* Unicode object */
);
@@ -558,6 +584,8 @@
int length /* Number of Py_UNICODE chars to encode */
);
+#define PyUnicode_AsRawUnicodeEscapeString PyUnicode_EncodeRawUnicodeEscapeEx
+
/* --- Latin-1 Codecs -----------------------------------------------------
Note: Latin-1 corresponds to the first 256 Unicode ordinals.
@@ -574,6 +602,12 @@
PyObject *unicode /* Unicode object */
);
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeLatin1Ex(
+ PyObject *unicode, /* Unicode object */
+ PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_EncodeLatin1(
const Py_UNICODE *data, /* Unicode char buffer */
int length, /* Number of Py_UNICODE chars to encode */
@@ -596,6 +630,12 @@
PyObject *unicode /* Unicode object */
);
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeASCIIEx(
+ PyObject *unicode, /* Unicode object */
+ PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_EncodeASCII(
const Py_UNICODE *data, /* Unicode char buffer */
int length, /* Number of Py_UNICODE chars to encode */
@@ -638,6 +678,14 @@
(unicode ordinal -> char ordinal) */
);
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeCharmapEx(
+ PyObject *unicode, /* Unicode object */
+ PyObject *mapping, /* character mapping
+ (unicode ordinal -> char ordinal) */
+ PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_EncodeCharmap(
const Py_UNICODE *data, /* Unicode char buffer */
int length, /* Number of Py_UNICODE chars to encode */
@@ -680,6 +728,12 @@
PyObject *unicode /* Unicode object */
);
+extern DL_IMPORT(PyObject*) PyUnicode_EncodeMBCSEx(
+ const Pyobject *unicode, /* Unicode object */
+ const PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(PyObject*) PyUnicode_EncodeMBCS(
const Py_UNICODE *data, /* Unicode char buffer */
int length, /* Number of Py_UNICODE chars to encode */
@@ -694,7 +748,8 @@
an output buffer using standard ASCII digit codes.
The output buffer has to provide at least length+1 bytes of storage
- area. The output string is 0-terminated.
+ area (more if longer replacement string are generated).
+ The output string is 0-terminated.
The encoder converts whitespace to ' ', decimal characters to their
corresponding ASCII digit and all other Latin-1 characters except
@@ -703,15 +758,23 @@
Error handling is defined by the errors argument:
- NULL or "strict": raise a ValueError
- "ignore": ignore the wrong characters (these are not copied to the
- output buffer)
- "replace": replaces illegal characters with '?'
+ NULL, None, "strict" or u"strict": raise a UnicodeError
+ "ignore" or u"ignore": ignore the wrong characters (these are
+ not copied to the output buffer)
+ "replace" or u"replace": replaces illegal characters with '?'
+ callable object: use what the object returns as replacement
Returns 0 on success, -1 on failure.
*/
+extern DL_IMPORT(int) PyUnicode_EncodeDecimalEx(
+ PyObject *unicode, /* Unicode object */
+ char *output, /* Output buffer; must have size >= length */
+ PyObject *errors /* error handling */
+ );
+
+/* DEPRECATED */
extern DL_IMPORT(int) PyUnicode_EncodeDecimal(
Py_UNICODE *s, /* Unicode buffer */
int length, /* Number of Py_UNICODE chars to encode */
@@ -772,7 +835,7 @@
*/
extern DL_IMPORT(PyObject *) PyUnicode_Translate(
- PyObject *str, /* String */
+ PyObject *str, /* String */
PyObject *table, /* Translate table */
const char *errors /* error handling */
);
Index: Lib/codecs.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/codecs.py,v
retrieving revision 1.19
diff -u -r1.19 codecs.py
--- Lib/codecs.py 2001/05/29 06:06:54 1.19
+++ Lib/codecs.py 2001/07/12 10:50:33
@@ -51,13 +51,17 @@
The .encode()/.decode() methods may implement different error
handling schemes by providing the errors argument. These
- string values are defined:
+ values are defined:
- 'strict' - raise a ValueError error (or a subclass)
+ None or 'strict' - raise a UnicodeError error (or a subclass)
'ignore' - ignore the character and continue with the next
'replace' - replace with a suitable replacement character;
- Python will use the official U+FFFD REPLACEMENT
- CHARACTER for the builtin Unicode codecs.
+ Python will use the official U+FFFD REPLACEMENT
+ CHARACTER for the builtin Unicode codecs.
+ callable object - call the object with the arguments
+ encoding name, character, position
+ and encode the unicode object returned
+ instead of the original character.
"""
def encode(self, input, errors='strict'):
@@ -66,7 +70,7 @@
object, length consumed).
errors defines the error handling to apply. It defaults to
- 'strict' handling.
+ strict handling.
The method may not store state in the Codec instance. Use
StreamCodec for codecs which have to keep state in order to
@@ -122,9 +126,15 @@
schemes by providing the errors keyword argument. These
parameters are defined:
- 'strict' - raise a ValueError (or a subclass)
- 'ignore' - ignore the character and continue with the next
- 'replace'- replace with a suitable replacement character
+ None or 'strict' - raise a UnicodeError error (or a subclass)
+ 'ignore' - ignore the character and continue with the next
+ 'replace' - replace with a suitable replacement character;
+ Python will use the official U+FFFD REPLACEMENT
+ CHARACTER for the builtin Unicode codecs.
+ callable object - call the object with the arguments
+ encoding name, character, position
+ and encode the unicode object returned
+ instead of the original character.
"""
self.stream = stream
Index: Lib/encodings/base64_codec.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/encodings/base64_codec.py,v
retrieving revision 1.1
diff -u -r1.1 base64_codec.py
--- Lib/encodings/base64_codec.py 2001/05/15 12:00:02 1.1
+++ Lib/encodings/base64_codec.py 2001/07/12 10:50:34
@@ -10,17 +10,15 @@
### Codec APIs
-def base64_encode(input,errors='strict'):
+def base64_encode(input,errors='None'):
""" Encodes the object input and returns a tuple (output
object, length consumed).
- errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
- error handling for this codec.
+ errors defines the error handling to apply. As there are
+ no unencodable characters, errors will be ignored.
"""
- assert errors == 'strict'
output = base64.encodestring(input)
return (output, len(input))
@@ -34,7 +32,7 @@
mapped files are examples of objects providing this slot.
errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
+ strict handling which is the only currently supported
error handling for this codec.
"""
Index: Lib/encodings/hex_codec.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/encodings/hex_codec.py,v
retrieving revision 1.1
diff -u -r1.1 hex_codec.py
--- Lib/encodings/hex_codec.py 2001/05/15 12:00:02 1.1
+++ Lib/encodings/hex_codec.py 2001/07/12 10:50:34
@@ -10,17 +10,15 @@
### Codec APIs
-def hex_encode(input,errors='strict'):
+def hex_encode(input,errors=None):
""" Encodes the object input and returns a tuple (output
object, length consumed).
- errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
- error handling for this codec.
+ errors defines the error handling to apply. As there
+ are no unencodable characters, errors will be ignored.
"""
- assert errors == 'strict'
output = binascii.b2a_hex(input)
return (output, len(input))
@@ -34,7 +32,7 @@
mapped files are examples of objects providing this slot.
errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
+ strict handling which is the only currently supported
error handling for this codec.
"""
Index: Lib/encodings/quopri_codec.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/encodings/quopri_codec.py,v
retrieving revision 1.1
diff -u -r1.1 quopri_codec.py
--- Lib/encodings/quopri_codec.py 2001/05/15 15:34:07 1.1
+++ Lib/encodings/quopri_codec.py 2001/07/12 10:50:34
@@ -9,15 +9,12 @@
except ImportError:
from StringIO import StringIO
-def quopri_encode(input, errors='strict'):
+def quopri_encode(input, errors=None):
"""Encode the input, returning a tuple (output object, length consumed).
- errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
- error handling for this codec.
-
+ errors defines the error handling to apply. As there are
+ not unencodable characters for quopri, errors will be ignored.
"""
- assert errors == 'strict'
f = StringIO(input)
g = StringIO()
quopri.encode(f, g, 1)
Index: Lib/encodings/uu_codec.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/encodings/uu_codec.py,v
retrieving revision 1.1
diff -u -r1.1 uu_codec.py
--- Lib/encodings/uu_codec.py 2001/05/15 12:00:02 1.1
+++ Lib/encodings/uu_codec.py 2001/07/12 10:50:34
@@ -12,17 +12,17 @@
### Codec APIs
-def uu_encode(input,errors='strict',filename='',mode=0666):
+def uu_encode(input,errors=None,filename='',mode=0666):
""" Encodes the object input and returns a tuple (output
object, length consumed).
errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
+ strict handling which is the only currently supported
error handling for this codec.
"""
- assert errors == 'strict'
+ assert errors is None or errors == "strict" or errors == codecs.raise_unicodeencode_errors
from cStringIO import StringIO
from binascii import b2a_uu
infile = StringIO(input)
@@ -50,7 +50,7 @@
mapped files are examples of objects providing this slot.
errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
+ strict handling which is the only currently supported
error handling for this codec.
Note: filename and file mode information in the input data is
Index: Lib/encodings/zlib_codec.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/encodings/zlib_codec.py,v
retrieving revision 1.1
diff -u -r1.1 zlib_codec.py
--- Lib/encodings/zlib_codec.py 2001/05/15 12:00:02 1.1
+++ Lib/encodings/zlib_codec.py 2001/07/12 10:50:35
@@ -11,17 +11,15 @@
### Codec APIs
-def zlib_encode(input,errors='strict'):
+def zlib_encode(input,errors=None):
""" Encodes the object input and returns a tuple (output
object, length consumed).
- errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
- error handling for this codec.
+ errors defines the error handling to apply. As there
+ are no unencodable characters errors will be ignored.
"""
- assert errors == 'strict'
output = zlib.compress(input)
return (output, len(input))
@@ -35,7 +33,7 @@
mapped files are examples of objects providing this slot.
errors defines the error handling to apply. It defaults to
- 'strict' handling which is the only currently supported
+ strict handling which is the only currently supported
error handling for this codec.
"""
Index: Modules/_codecsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_codecsmodule.c,v
retrieving revision 2.8
diff -u -r2.8 _codecsmodule.c
--- Modules/_codecsmodule.c 2001/06/26 15:11:00 2.8
+++ Modules/_codecsmodule.c 2001/07/12 10:50:39
@@ -327,9 +327,9 @@
{
const char *data;
int size;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",
+ if (!PyArg_ParseTuple(args, "s#|O:readbuffer_encode",
&data, &size, &errors))
return NULL;
@@ -343,9 +343,9 @@
{
const char *data;
int size;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
+ if (!PyArg_ParseTuple(args, "t#|O:charbuffer_encode",
&data, &size, &errors))
return NULL;
@@ -358,11 +358,11 @@
PyObject *args)
{
PyObject *obj;
- const char *errors = NULL;
+ PyObject *errors = NULL;
const char *data;
int size;
- if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
+ if (!PyArg_ParseTuple(args, "O|O:unicode_internal_encode",
&obj, &errors))
return NULL;
@@ -382,22 +382,21 @@
static PyObject *
utf_8_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
+ if (!PyArg_ParseTuple(args, "O|O:utf_8_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeUTF8Ex(str, errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -411,172 +410,161 @@
static PyObject *
utf_16_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
int byteorder = 0;
- if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
+ if (!PyArg_ParseTuple(args, "O|Oi:utf_16_encode",
&str, &errors, &byteorder))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
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_EncodeUTF16Ex(str, errors, byteorder),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_16_le_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
+ if (!PyArg_ParseTuple(args, "O|O:utf_16_le_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
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_EncodeUTF16Ex(str, errors, -1),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_16_be_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
+ if (!PyArg_ParseTuple(args, "O|O:utf_16_be_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
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_EncodeUTF16Ex(str, errors, +1),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
unicode_escape_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
+ if (!PyArg_ParseTuple(args, "O|O:unicode_escape_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str)),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeUnicodeEscapeEx(str),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
raw_unicode_escape_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
+ if (!PyArg_ParseTuple(args, "O|O:raw_unicode_escape_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str)),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeRawUnicodeEscapeEx(str),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
latin_1_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
+ if (!PyArg_ParseTuple(args, "O|O:latin_1_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeLatin1(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeLatin1Ex(str, errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
ascii_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
+ if (!PyArg_ParseTuple(args, "O|O:ascii_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeASCII(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeASCIIEx(str, errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
charmap_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
PyObject *mapping = NULL;
- if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
+ if (!PyArg_ParseTuple(args, "O|OO:charmap_encode",
&str, &errors, &mapping))
return NULL;
if (mapping == Py_None)
@@ -585,12 +573,9 @@
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeCharmap(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- mapping,
- errors),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeCharmapEx(str, mapping, errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -602,20 +587,18 @@
PyObject *args)
{
PyObject *str, *v;
- const char *errors = NULL;
+ PyObject *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
+ if (!PyArg_ParseTuple(args, "O|O:mbcs_encode",
&str, &errors))
return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
return NULL;
- v = codec_tuple(PyUnicode_EncodeMBCS(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ v = codec_tuple(
+ PyUnicode_EncodeMBCSEx(str, errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -654,7 +637,12 @@
{"mbcs_encode", mbcs_encode, 1},
{"mbcs_decode", mbcs_decode, 1},
#endif
- {NULL, NULL} /* sentinel */
+ {"raise_unicodeencode_errors", PyCodec_RaiseUnicodeEncodeErrors, 1},
+ {"ignore_unicodeencode_errors", PyCodec_IgnoreUnicodeEncodeErrors, 1},
+ {"replace_unicodeencode_errors", PyCodec_ReplaceUnicodeEncodeErrors, 1},
+ {"xmlcharrefreplace_unicodeencode_errors", PyCodec_XMLCharRefReplaceUnicodeEncodeErrors, 1},
+ {"escapereplace_unicodeencode_errors", PyCodec_EscapeReplaceUnicodeEncodeErrors, 1},
+ {NULL, NULL} /* sentinel */
};
DL_EXPORT(void)
Index: Objects/unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.101
diff -u -r2.101 unicodeobject.c
--- Objects/unicodeobject.c 2001/06/27 18:59:43 2.101
+++ Objects/unicodeobject.c 2001/07/12 10:50:43
@@ -509,47 +509,30 @@
return NULL;
}
-PyObject *PyUnicode_Encode(const Py_UNICODE *s,
- int size,
- const char *encoding,
- const char *errors)
+PyObject *PyUnicode_EncodeEx(PyObject *unicode,
+ const char *encoding,
+ PyObject *errors)
{
- PyObject *v, *unicode;
-
- unicode = PyUnicode_FromUnicode(s, size);
- if (unicode == NULL)
- return NULL;
- v = PyUnicode_AsEncodedString(unicode, encoding, errors);
- Py_DECREF(unicode);
- return v;
-}
-
-PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
- const char *encoding,
- const char *errors)
-{
PyObject *v;
-
+
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
- if (encoding == NULL)
+ if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Shortcuts for common default encodings */
- if (errors == NULL) {
- if (strcmp(encoding, "utf-8") == 0)
- return PyUnicode_AsUTF8String(unicode);
- else if (strcmp(encoding, "latin-1") == 0)
- return PyUnicode_AsLatin1String(unicode);
- else if (strcmp(encoding, "ascii") == 0)
- return PyUnicode_AsASCIIString(unicode);
- }
+ if (strcmp(encoding, "utf-8") == 0)
+ return PyUnicode_EncodeUTF8Ex(unicode, errors);
+ else if ((strcmp(encoding, "latin-1") == 0) || (strcmp(encoding, "iso-8859-1") == 0))
+ return PyUnicode_EncodeLatin1Ex(unicode, errors);
+ else if (strcmp(encoding, "ascii") == 0)
+ return PyUnicode_EncodeASCIIEx(unicode, errors);
/* Encode via the codec registry */
- v = PyCodec_Encode(unicode, encoding, errors);
+ v = PyCodec_EncodeEx(unicode, encoding, errors);
if (v == NULL)
goto onError;
/* XXX Should we really enforce this ? */
@@ -566,6 +549,28 @@
return NULL;
}
+PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
+ const char *encoding,
+ const char *errors)
+{
+ PyObject *errorstr;
+ PyObject *res;
+
+ if (errors) {
+ errorstr = PyString_FromString(errors);
+ if (!errorstr)
+ return NULL;
+ }
+ else {
+ Py_INCREF(Py_None);
+ errorstr = Py_None;
+ }
+
+ res = PyUnicode_AsEncodedStringEx(unicode, encoding, errorstr);
+ Py_DECREF(errorstr);
+ return res;
+}
+
/* Return a Python string holding the default encoded value of the
Unicode object.
@@ -865,23 +870,36 @@
}
#endif
-PyObject *PyUnicode_EncodeUTF8(const Py_UNICODE *s,
- int size,
- const char *errors)
+PyObject *PyUnicode_EncodeUTF8Ex(PyObject *unicode,
+ PyObject *errors)
{
+ Py_UNICODE *s;
+ int size;
PyObject *v;
char *p;
char *q;
Py_UCS4 ch2;
- unsigned int cbAllocated = 3 * size;
+ unsigned int cbAllocated;
unsigned int cbWritten = 0;
int i = 0;
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ errors = PyCodec_UnicodeEncodeHandlerForObject(errors);
+ if (errors == NULL)
+ return NULL;
+ s = PyUnicode_AS_UNICODE(unicode);
+ size = PyUnicode_GET_SIZE(unicode);
+ cbAllocated = 3 * size;
v = PyString_FromStringAndSize(NULL, cbAllocated);
if (v == NULL)
return NULL;
- if (size == 0)
+ if (size == 0) {
+ Py_DECREF(errors);
return v;
+ }
p = q = PyString_AS_STRING(v);
while (i < size) {
@@ -941,18 +959,42 @@
onError:
Py_DECREF(v);
+ Py_DECREF(errors);
return NULL;
}
-PyObject *PyUnicode_AsUTF8String(PyObject *unicode)
+PyObject *PyUnicode_EncodeUTF8(const Py_UNICODE *s,
+ int size,
+ const char *errors)
{
- if (!PyUnicode_Check(unicode)) {
- PyErr_BadArgument();
- return NULL;
+ PyObject *unicode;
+ PyObject *errorstr;
+ PyObject *res;
+
+ unicode = PyUnicode_FromUnicode(s, size);
+ if (!unicode)
+ return NULL;
+ if (errors) {
+ errorstr = PyString_FromString(errors);
+ if (!errorstr) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
}
- return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- NULL);
+ else {
+ Py_INCREF(Py_None);
+ errorstr = Py_None;
+ }
+
+ res = PyUnicode_EncodeUTF8Ex(unicode, errorstr);
+ Py_DECREF(unicode);
+ Py_DECREF(errorstr);
+ return res;
+}
+
+PyObject *PyUnicode_AsUTF8String(PyObject *unicode)
+{
+ return PyUnicode_EncodeUTF8Ex(unicode, NULL);
}
/* --- UTF-16 Codec ------------------------------------------------------- */
@@ -1123,23 +1165,32 @@
#undef UTF16_ERROR
-PyObject *PyUnicode_EncodeUTF16(const Py_UNICODE *s,
- int size,
- const char *errors,
- int byteorder)
-{
- PyObject *v;
+PyObject *PyUnicode_EncodeUTF16Ex(PyObject *unicode,
+ PyObject *errors,
+ int byteorder)
+{
+ Py_UNICODE *s;
+ int size;
+ PyObject *v = NULL;
Py_UCS2 *p;
char *q;
int i, pairs, doswap = 1;
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+
+ s = PyUnicode_AS_UNICODE(unicode);
+ size = PyUnicode_GET_SIZE(unicode);
+
for (i = pairs = 0; i < size; i++)
if (s[i] >= 0x10000)
pairs++;
v = PyString_FromStringAndSize(NULL,
sizeof(Py_UCS2) * (size + pairs + (byteorder == 0)));
if (v == NULL)
- return NULL;
+ goto finish;
q = PyString_AS_STRING(v);
p = (Py_UCS2 *)q;
@@ -1172,19 +1223,44 @@
*p++ = ch2;
}
}
+ finish:
+ Py_DECREF(errors);
return v;
}
-PyObject *PyUnicode_AsUTF16String(PyObject *unicode)
+PyObject *PyUnicode_EncodeUTF16(const Py_UNICODE *s,
+ int size,
+ const char *errors,
+ int byteorder)
{
- if (!PyUnicode_Check(unicode)) {
- PyErr_BadArgument();
- return NULL;
+ PyObject *unicode;
+ PyObject *errorstr;
+ PyObject *res;
+
+ unicode = PyUnicode_FromUnicode(s, size);
+ if (!unicode)
+ return NULL;
+ if (errors) {
+ errorstr = PyString_FromString(errors);
+ if (!errorstr) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
+ }
+ else {
+ Py_INCREF(Py_None);
+ errorstr = Py_None;
}
- return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- NULL,
- 0);
+
+ res = PyUnicode_EncodeUTF16Ex(unicode, errorstr, byteorder);
+ Py_DECREF(unicode);
+ Py_DECREF(errorstr);
+ return res;
+}
+
+PyObject *PyUnicode_AsUTF16String(PyObject *unicode)
+{
+ return PyUnicode_EncodeUTF16Ex(unicode, NULL, 0);
}
/* --- Unicode Escape Codec ----------------------------------------------- */
@@ -1501,6 +1577,18 @@
return NULL;
}
+PyObject *PyUnicode_EncodeUnicodeEscapeEx(PyObject *unicode)
+{
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+
+ return unicodeescape_string(
+ PyUnicode_AS_UNICODE(unicode),
+ PyUnicode_GET_SIZE(unicode), 0);
+}
+
PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,
int size)
{
@@ -1509,12 +1597,7 @@
PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
{
- if (!PyUnicode_Check(unicode)) {
- PyErr_BadArgument();
- return NULL;
- }
- return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode));
+ return PyUnicode_EncodeUnicodeEscapeEx(unicode);
}
/* --- Raw Unicode Escape Codec ------------------------------------------- */
@@ -1595,15 +1678,22 @@
return NULL;
}
-PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
- int size)
+PyObject *PyUnicode_EncodeRawUnicodeEscapeEx(PyObject *unicode)
{
PyObject *repr;
char *p;
char *q;
+ Py_UNICODE *s;
+ int size;
static const char *hexdigit = "0123456789abcdef";
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ s = PyUnicode_AS_UNICODE(unicode);
+ size = PyUnicode_GET_SIZE(unicode);
repr = PyString_FromStringAndSize(NULL, 6 * size);
if (repr == NULL)
return NULL;
@@ -1637,14 +1727,19 @@
return NULL;
}
-PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
+PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
+ int size)
{
- if (!PyUnicode_Check(unicode)) {
- PyErr_BadArgument();
+ PyObject *unicode;
+ PyObject *res;
+
+ unicode = PyUnicode_FromUnicode(s, size);
+ if (!unicode)
return NULL;
- }
- return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode));
+
+ res = PyUnicode_EncodeRawUnicodeEscapeEx(unicode);
+ Py_DECREF(unicode);
+ return res;
}
/* --- Latin-1 Codec ------------------------------------------------------ */
@@ -1677,83 +1772,212 @@
return NULL;
}
-static
-int latin1_encoding_error(const Py_UNICODE **source,
- char **dest,
- const char *errors,
- const char *details)
-{
- if ((errors == NULL) ||
- (strcmp(errors,"strict") == 0)) {
- PyErr_Format(PyExc_UnicodeError,
- "Latin-1 encoding error: %.400s",
- details);
- return -1;
- }
- else if (strcmp(errors,"ignore") == 0) {
- return 0;
- }
- else if (strcmp(errors,"replace") == 0) {
- **dest = '?';
- (*dest)++;
- return 0;
+/* error handling callback helper:
+ build arguments, call the callback and check the arguments,
+ put the result into newpos and return the replacement string, which
+ has to be freed by the caller
+*/
+
+static PyObject *unicode_encode_call_errorhandler(PyObject *errors, PyObject **errorHandler,
+ const char *encoding, PyObject *unicode, int unicodepos,
+ int *newpos)
+{
+ static char *argparse = "O!i;encoding error handler must return (unicode, int) tuple";
+
+ PyObject *args;
+ PyObject *restuple;
+ PyObject *resunicode;
+ int size = PyUnicode_GET_SIZE(unicode);
+
+ if (*errorHandler == NULL) {
+ *errorHandler = PyCodec_UnicodeEncodeHandlerForObject(errors);
+ if (*errorHandler == NULL)
+ return NULL;
+ }
+
+ /* we don't need a state */
+ args = Py_BuildValue("sOiO", encoding, unicode, unicodepos, Py_None);
+ if (args == NULL)
+ return NULL;
+ restuple = PyEval_CallObject(*errorHandler, args);
+ Py_DECREF(args);
+ if (restuple == NULL)
+ return NULL;
+ if (!PyTuple_Check(restuple)) {
+ PyErr_Format(PyExc_TypeError, &argparse[4]);
+ Py_DECREF(restuple);
+ return NULL;
}
- else {
- PyErr_Format(PyExc_ValueError,
- "Latin-1 encoding error; "
- "unknown error handling code: %.400s",
- errors);
- return -1;
+ if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &resunicode, newpos)) {
+ Py_DECREF(restuple);
+ return NULL;
}
+ if (*newpos<0)
+ *newpos = -1;
+ else if (*newpos>size)
+ *newpos = size-1;
+ else
+ --*newpos;
+ Py_INCREF(resunicode);
+ Py_DECREF(restuple);
+ return resunicode;
}
-PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p,
- int size,
- const char *errors)
-{
- PyObject *repr;
- char *s, *start;
+/* Encode a Unicode object as ASCII (limit==128) or
+ latin-1 (limit==256)
+*/
- repr = PyString_FromStringAndSize(NULL, size);
- if (repr == NULL)
- return NULL;
- if (size == 0)
- return repr;
+static PyObject *unicode_encode_ucs1ex(PyObject *unicode,
+ PyObject *errors, int limit)
+{
+ /* output object */
+ PyObject *res;
+ /* size of input */
+ int unicodesize;
+ /* remaining size of input */
+ int remainingunicodesize;
+ Py_UNICODE *uni;
+ Py_UNICODE *origuni;
+ char *str;
+ /* current output position */
+ int respos = 0;
+ int ressize;
+ char *encoding = (limit == 256) ? "latin-1" : "ascii";
+ PyObject *errorHandler = NULL;
- s = PyString_AS_STRING(repr);
- start = s;
- while (size-- > 0) {
- Py_UNICODE ch = *p++;
- if (ch >= 256) {
- if (latin1_encoding_error(&p, &s, errors,
- "ordinal not in range(256)"))
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ remainingunicodesize = unicodesize = PyUnicode_GET_SIZE(unicode);
+ origuni = uni = PyUnicode_AS_UNICODE(unicode);
+ /* allocate enough for a simple encoding without
+ replacements, if we need more, we'll resize */
+ res = PyString_FromStringAndSize(NULL, unicodesize);
+ if (res == NULL)
+ goto onError;
+ if (remainingunicodesize == 0)
+ return res;
+ str = PyString_AS_STRING(res);
+ ressize = unicodesize;
+
+ for (;remainingunicodesize; --remainingunicodesize, ++uni) {
+ Py_UNICODE c = *uni;
+
+ /* can we encode this? */
+ if (c |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |