diff options
author | Guido van Rossum <guido@python.org> | 2000-03-10 23:03:02 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2000-03-10 23:03:02 (GMT) |
commit | c279b53b4f010b0b296720c183cb78569812fbba (patch) | |
tree | d42fea9a3dc1552d0b793794f43c1e276f8d15a0 /Python | |
parent | e826ef0a89808da65b24f820feb62b3c49fd369f (diff) | |
download | cpython-c279b53b4f010b0b296720c183cb78569812fbba.zip cpython-c279b53b4f010b0b296720c183cb78569812fbba.tar.gz cpython-c279b53b4f010b0b296720c183cb78569812fbba.tar.bz2 |
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Diffstat (limited to 'Python')
-rw-r--r-- | Python/marshal.c | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/Python/marshal.c b/Python/marshal.c index ddccc89..519081d 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -52,6 +52,7 @@ PERFORMANCE OF THIS SOFTWARE. #define TYPE_LIST '[' #define TYPE_DICT '{' #define TYPE_CODE 'c' +#define TYPE_UNICODE 'u' #define TYPE_UNKNOWN '?' typedef struct { @@ -213,9 +214,22 @@ w_object(v, p) #endif else if (PyString_Check(v)) { w_byte(TYPE_STRING, p); - n = PyString_Size(v); + n = PyString_GET_SIZE(v); w_long((long)n, p); - w_string(PyString_AsString(v), n, p); + w_string(PyString_AS_STRING(v), n, p); + } + else if (PyUnicode_Check(v)) { + PyObject *utf8; + utf8 = PyUnicode_AsUTF8String(v); + if (utf8 == NULL) { + p->error = 1; + return; + } + w_byte(TYPE_UNICODE, p); + n = PyString_GET_SIZE(utf8); + w_long((long)n, p); + w_string(PyString_AS_STRING(utf8), n, p); + Py_DECREF(utf8); } else if (PyTuple_Check(v)) { w_byte(TYPE_TUPLE, p); @@ -227,10 +241,10 @@ w_object(v, p) } else if (PyList_Check(v)) { w_byte(TYPE_LIST, p); - n = PyList_Size(v); + n = PyList_GET_SIZE(v); w_long((long)n, p); for (i = 0; i < n; i++) { - w_object(PyList_GetItem(v, i), p); + w_object(PyList_GET_ITEM(v, i), p); } } else if (PyDict_Check(v)) { @@ -482,7 +496,7 @@ r_object(p) } v = PyString_FromStringAndSize((char *)NULL, n); if (v != NULL) { - if (r_string(PyString_AsString(v), (int)n, p) != n) { + if (r_string(PyString_AS_STRING(v), (int)n, p) != n) { Py_DECREF(v); v = NULL; PyErr_SetString(PyExc_EOFError, @@ -491,6 +505,29 @@ r_object(p) } return v; + case TYPE_UNICODE: + { + char *buffer; + + n = r_long(p); + if (n < 0) { + PyErr_SetString(PyExc_ValueError, "bad marshal data"); + return NULL; + } + buffer = (char *)Py_Malloc(n); + if (buffer == NULL) + return NULL; + if (r_string(buffer, (int)n, p) != n) { + free(buffer); + PyErr_SetString(PyExc_EOFError, + "EOF read where object expected"); + return NULL; + } + v = PyUnicode_DecodeUTF8(buffer, n, NULL); + free(buffer); + return v; + } + case TYPE_TUPLE: n = r_long(p); if (n < 0) { |