diff options
Diffstat (limited to 'Objects')
43 files changed, 7225 insertions, 5016 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 2f887aa..9105769 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -237,7 +237,8 @@ PyObject_AsCharBuffer(PyObject *obj, pb = obj->ob_type->tp_as_buffer; if (pb == NULL || pb->bf_getbuffer == NULL) { PyErr_SetString(PyExc_TypeError, - "expected an object with the buffer interface"); + "expected bytes, bytearray " + "or buffer compatible object"); return -1; } if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1; @@ -331,7 +332,7 @@ PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) { if (!PyObject_CheckBuffer(obj)) { PyErr_Format(PyExc_TypeError, - "'%100s' does not support the buffer interface", + "'%.100s' does not support the buffer interface", Py_TYPE(obj)->tp_name); return -1; } @@ -792,8 +793,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot) return x; Py_DECREF(x); /* can't do it */ } - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } static PyObject * @@ -1379,9 +1379,7 @@ PyNumber_Long(PyObject *o) PyBytes_GET_SIZE(o)); if (PyUnicode_Check(o)) /* The above check is done in PyLong_FromUnicode(). */ - return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), - PyUnicode_GET_SIZE(o), - 10); + return PyLong_FromUnicodeObject(o, 10); if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) return long_from_string(buffer, buffer_len); diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 0b45394..72e5815 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -854,83 +854,79 @@ bytearray_repr(PyByteArrayObject *self) const char *quote_prefix = "bytearray(b"; const char *quote_postfix = ")"; Py_ssize_t length = Py_SIZE(self); - /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */ + /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ size_t newsize; PyObject *v; - if (length > (PY_SSIZE_T_MAX - 14) / 4) { + register Py_ssize_t i; + register char c; + register char *p; + int quote; + char *test, *start; + char *buffer; + + if (length > (PY_SSIZE_T_MAX - 15) / 4) { PyErr_SetString(PyExc_OverflowError, "bytearray object is too large to make repr"); return NULL; } - newsize = 14 + 4 * length; - v = PyUnicode_FromUnicode(NULL, newsize); - if (v == NULL) { + + newsize = 15 + length * 4; + buffer = PyMem_Malloc(newsize); + if (buffer == NULL) { + PyErr_NoMemory(); return NULL; } - else { - register Py_ssize_t i; - register Py_UNICODE c; - register Py_UNICODE *p; - int quote; - - /* Figure out which quote to use; single is preferred */ - quote = '\''; - { - char *test, *start; - start = PyByteArray_AS_STRING(self); - for (test = start; test < start+length; ++test) { - if (*test == '"') { - quote = '\''; /* back to single */ - goto decided; - } - else if (*test == '\'') - quote = '"'; - } - decided: - ; - } - p = PyUnicode_AS_UNICODE(v); - while (*quote_prefix) - *p++ = *quote_prefix++; - *p++ = quote; - - for (i = 0; i < length; i++) { - /* There's at least enough room for a hex escape - and a closing quote. */ - assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); - c = self->ob_bytes[i]; - if (c == '\'' || c == '\\') - *p++ = '\\', *p++ = c; - else if (c == '\t') - *p++ = '\\', *p++ = 't'; - else if (c == '\n') - *p++ = '\\', *p++ = 'n'; - else if (c == '\r') - *p++ = '\\', *p++ = 'r'; - else if (c == 0) - *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; - else if (c < ' ' || c >= 0x7f) { - *p++ = '\\'; - *p++ = 'x'; - *p++ = hexdigits[(c & 0xf0) >> 4]; - *p++ = hexdigits[c & 0xf]; - } - else - *p++ = c; + /* Figure out which quote to use; single is preferred */ + quote = '\''; + start = PyByteArray_AS_STRING(self); + for (test = start; test < start+length; ++test) { + if (*test == '"') { + quote = '\''; /* back to single */ + break; } - assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); - *p++ = quote; - while (*quote_postfix) { - *p++ = *quote_postfix++; + else if (*test == '\'') + quote = '"'; + } + + p = buffer; + while (*quote_prefix) + *p++ = *quote_prefix++; + *p++ = quote; + + for (i = 0; i < length; i++) { + /* There's at least enough room for a hex escape + and a closing quote. */ + assert(newsize - (p - buffer) >= 5); + c = self->ob_bytes[i]; + if (c == '\'' || c == '\\') + *p++ = '\\', *p++ = c; + else if (c == '\t') + *p++ = '\\', *p++ = 't'; + else if (c == '\n') + *p++ = '\\', *p++ = 'n'; + else if (c == '\r') + *p++ = '\\', *p++ = 'r'; + else if (c == 0) + *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; + else if (c < ' ' || c >= 0x7f) { + *p++ = '\\'; + *p++ = 'x'; + *p++ = hexdigits[(c & 0xf0) >> 4]; + *p++ = hexdigits[c & 0xf]; } - *p = '\0'; - if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) { - Py_DECREF(v); - return NULL; - } - return v; + else + *p++ = c; } + assert(newsize - (p - buffer) >= 1); + *p++ = quote; + while (*quote_postfix) { + *p++ = *quote_postfix++; + } + + v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); + PyMem_Free(buffer); + return v; } static PyObject * @@ -964,23 +960,20 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op) return NULL; } - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } self_size = _getbuffer(self, &self_bytes); if (self_size < 0) { PyErr_Clear(); - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } other_size = _getbuffer(other, &other_bytes); if (other_size < 0) { PyErr_Clear(); PyBuffer_Release(&self_bytes); - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { @@ -1037,6 +1030,8 @@ bytearray_dealloc(PyByteArrayObject *self) /* -------------------------------------------------------------------- */ /* Methods */ +#define FASTSEARCH fastsearch +#define STRINGLIB(F) stringlib_##F #define STRINGLIB_CHAR char #define STRINGLIB_LEN PyByteArray_GET_SIZE #define STRINGLIB_STR PyByteArray_AS_STRING @@ -1147,6 +1142,30 @@ bytearray_count(PyByteArrayObject *self, PyObject *args) return count_obj; } +PyDoc_STRVAR(clear__doc__, +"B.clear() -> None\n\ +\n\ +Remove all items from B."); + +static PyObject * +bytearray_clear(PyByteArrayObject *self) +{ + if (PyByteArray_Resize((PyObject *)self, 0) < 0) + return NULL; + Py_RETURN_NONE; +} + +PyDoc_STRVAR(copy__doc__, +"B.copy() -> bytearray\n\ +\n\ +Return a copy of B."); + +static PyObject * +bytearray_copy(PyByteArrayObject *self) +{ + return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), + PyByteArray_GET_SIZE(self)); +} PyDoc_STRVAR(index__doc__, "B.index(sub[, start[, end]]) -> int\n\ @@ -2444,7 +2463,7 @@ If the argument is omitted, strip trailing ASCII whitespace."); static PyObject * bytearray_rstrip(PyByteArrayObject *self, PyObject *args) { - Py_ssize_t left, right, mysize, argsize; + Py_ssize_t right, mysize, argsize; void *myptr, *argptr; PyObject *arg = Py_None; Py_buffer varg; @@ -2462,11 +2481,10 @@ bytearray_rstrip(PyByteArrayObject *self, PyObject *args) } myptr = self->ob_bytes; mysize = Py_SIZE(self); - left = 0; right = rstrip_helper(myptr, mysize, argptr, argsize); if (arg != Py_None) PyBuffer_Release(&varg); - return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); + return PyByteArray_FromStringAndSize(self->ob_bytes, right); } PyDoc_STRVAR(decode_doc, @@ -2588,11 +2606,13 @@ Line breaks are not included in the resulting list unless keepends\n\ is given and true."); static PyObject* -bytearray_splitlines(PyObject *self, PyObject *args) +bytearray_splitlines(PyObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"keepends", 0}; int keepends = 0; - if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", + kwlist, &keepends)) return NULL; return stringlib_splitlines( @@ -2609,7 +2629,7 @@ Spaces between two numbers are accepted.\n\ Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."); static int -hex_digit_to_int(Py_UNICODE c) +hex_digit_to_int(Py_UCS4 c) { if (c >= 128) return -1; @@ -2629,15 +2649,20 @@ bytearray_fromhex(PyObject *cls, PyObject *args) { PyObject *newbytes, *hexobj; char *buf; - Py_UNICODE *hex; Py_ssize_t hexlen, byteslen, i, j; int top, bot; + void *data; + unsigned int kind; if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) return NULL; assert(PyUnicode_Check(hexobj)); - hexlen = PyUnicode_GET_SIZE(hexobj); - hex = PyUnicode_AS_UNICODE(hexobj); + if (PyUnicode_READY(hexobj)) + return NULL; + kind = PyUnicode_KIND(hexobj); + data = PyUnicode_DATA(hexobj); + hexlen = PyUnicode_GET_LENGTH(hexobj); + byteslen = hexlen/2; /* This overestimates if there are spaces */ newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); if (!newbytes) @@ -2645,12 +2670,12 @@ bytearray_fromhex(PyObject *cls, PyObject *args) buf = PyByteArray_AS_STRING(newbytes); for (i = j = 0; i < hexlen; i += 2) { /* skip over spaces in the input */ - while (hex[i] == ' ') + while (PyUnicode_READ(kind, data, i) == ' ') i++; if (i >= hexlen) break; - top = hex_digit_to_int(hex[i]); - bot = hex_digit_to_int(hex[i+1]); + top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); + bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); if (top == -1 || bot == -1) { PyErr_Format(PyExc_ValueError, "non-hexadecimal number found in " @@ -2736,6 +2761,8 @@ bytearray_methods[] = { {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, _Py_capitalize__doc__}, {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, + {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, clear__doc__}, + {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, copy__doc__}, {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, decode_doc}, {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, @@ -2779,8 +2806,8 @@ bytearray_methods[] = { {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS, rsplit__doc__}, {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, {"split", (PyCFunction)bytearray_split, METH_VARARGS, split__doc__}, - {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS, - splitlines__doc__}, + {"splitlines", (PyCFunction)bytearray_splitlines, + METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , startswith__doc__}, {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 8e35fa9..fa0e8c2 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -41,10 +41,6 @@ static PyBytesObject *nullstring; #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) /* - For both PyBytes_FromString() and PyBytes_FromStringAndSize(), the - parameter `size' denotes number of characters to allocate, not counting any - null terminating character. - For PyBytes_FromString(), the parameter `str' points to a null-terminated string containing exactly `size' bytes. @@ -61,8 +57,8 @@ static PyBytesObject *nullstring; The PyObject member `op->ob_size', which denotes the number of "extra items" in a variable-size object, will contain the number of bytes - allocated for string data, not counting the null terminating character. It - is therefore equal to the equal to the `size' parameter (for + allocated for string data, not counting the null terminating character. + It is therefore equal to the `size' parameter (for PyBytes_FromStringAndSize()) or the length of the string in the `str' parameter (for PyBytes_FromString()). */ @@ -570,74 +566,68 @@ PyBytes_Repr(PyObject *obj, int smartquotes) { static const char *hexdigits = "0123456789abcdef"; register PyBytesObject* op = (PyBytesObject*) obj; - Py_ssize_t length = Py_SIZE(op); - size_t newsize; + Py_ssize_t i, length = Py_SIZE(op); + size_t newsize, squotes, dquotes; PyObject *v; - if (length > (PY_SSIZE_T_MAX - 3) / 4) { + unsigned char quote, *s, *p; + + /* Compute size of output string */ + squotes = dquotes = 0; + newsize = 3; /* b'' */ + s = (unsigned char*)op->ob_sval; + for (i = 0; i < length; i++) { + switch(s[i]) { + case '\'': squotes++; newsize++; break; + case '"': dquotes++; newsize++; break; + case '\\': case '\t': case '\n': case '\r': + newsize += 2; break; /* \C */ + default: + if (s[i] < ' ' || s[i] >= 0x7f) + newsize += 4; /* \xHH */ + else + newsize++; + } + } + quote = '\''; + if (smartquotes && squotes && !dquotes) + quote = '"'; + if (squotes && quote == '\'') + newsize += squotes; + + if (newsize > (PY_SSIZE_T_MAX - sizeof(PyUnicodeObject) - 1)) { PyErr_SetString(PyExc_OverflowError, "bytes object is too large to make repr"); return NULL; } - newsize = 3 + 4 * length; - v = PyUnicode_FromUnicode(NULL, newsize); + + v = PyUnicode_New(newsize, 127); if (v == NULL) { return NULL; } - else { - register Py_ssize_t i; - register Py_UNICODE c; - register Py_UNICODE *p = PyUnicode_AS_UNICODE(v); - int quote; - - /* Figure out which quote to use; single is preferred */ - quote = '\''; - if (smartquotes) { - char *test, *start; - start = PyBytes_AS_STRING(op); - for (test = start; test < start+length; ++test) { - if (*test == '"') { - quote = '\''; /* back to single */ - goto decided; - } - else if (*test == '\'') - quote = '"'; - } - decided: - ; - } - - *p++ = 'b', *p++ = quote; - for (i = 0; i < length; i++) { - /* There's at least enough room for a hex escape - and a closing quote. */ - assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); - c = op->ob_sval[i]; - if (c == quote || c == '\\') - *p++ = '\\', *p++ = c; - else if (c == '\t') - *p++ = '\\', *p++ = 't'; - else if (c == '\n') - *p++ = '\\', *p++ = 'n'; - else if (c == '\r') - *p++ = '\\', *p++ = 'r'; - else if (c < ' ' || c >= 0x7f) { - *p++ = '\\'; - *p++ = 'x'; - *p++ = hexdigits[(c & 0xf0) >> 4]; - *p++ = hexdigits[c & 0xf]; - } - else - *p++ = c; - } - assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); - *p++ = quote; - *p = '\0'; - if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) { - Py_DECREF(v); - return NULL; + p = PyUnicode_1BYTE_DATA(v); + + *p++ = 'b', *p++ = quote; + for (i = 0; i < length; i++) { + unsigned char c = op->ob_sval[i]; + if (c == quote || c == '\\') + *p++ = '\\', *p++ = c; + else if (c == '\t') + *p++ = '\\', *p++ = 't'; + else if (c == '\n') + *p++ = '\\', *p++ = 'n'; + else if (c == '\r') + *p++ = '\\', *p++ = 'r'; + else if (c < ' ' || c >= 0x7f) { + *p++ = '\\'; + *p++ = 'x'; + *p++ = hexdigits[(c & 0xf0) >> 4]; + *p++ = hexdigits[c & 0xf]; } - return v; + else + *p++ = c; } + *p++ = quote; + return v; } static PyObject * @@ -873,16 +863,16 @@ bytes_hash(PyBytesObject *a) { register Py_ssize_t len; register unsigned char *p; - register Py_hash_t x; + register Py_uhash_t x; if (a->ob_shash != -1) return a->ob_shash; len = Py_SIZE(a); p = (unsigned char *) a->ob_sval; - x = *p << 7; + x = (Py_uhash_t)*p << 7; while (--len >= 0) - x = (1000003*x) ^ *p++; - x ^= Py_SIZE(a); + x = (1000003U*x) ^ (Py_uhash_t)*p++; + x ^= (Py_uhash_t)Py_SIZE(a); if (x == -1) x = -2; a->ob_shash = x; @@ -2316,11 +2306,13 @@ Line breaks are not included in the resulting list unless keepends\n\ is given and true."); static PyObject* -bytes_splitlines(PyObject *self, PyObject *args) +bytes_splitlines(PyObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"keepends", 0}; int keepends = 0; - if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", + kwlist, &keepends)) return NULL; return stringlib_splitlines( @@ -2338,7 +2330,7 @@ Spaces between two numbers are accepted.\n\ Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'."); static int -hex_digit_to_int(Py_UNICODE c) +hex_digit_to_int(Py_UCS4 c) { if (c >= 128) return -1; @@ -2358,15 +2350,20 @@ bytes_fromhex(PyObject *cls, PyObject *args) { PyObject *newstring, *hexobj; char *buf; - Py_UNICODE *hex; Py_ssize_t hexlen, byteslen, i, j; int top, bot; + void *data; + unsigned int kind; if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) return NULL; assert(PyUnicode_Check(hexobj)); - hexlen = PyUnicode_GET_SIZE(hexobj); - hex = PyUnicode_AS_UNICODE(hexobj); + if (PyUnicode_READY(hexobj)) + return NULL; + kind = PyUnicode_KIND(hexobj); + data = PyUnicode_DATA(hexobj); + hexlen = PyUnicode_GET_LENGTH(hexobj); + byteslen = hexlen/2; /* This overestimates if there are spaces */ newstring = PyBytes_FromStringAndSize(NULL, byteslen); if (!newstring) @@ -2374,12 +2371,12 @@ bytes_fromhex(PyObject *cls, PyObject *args) buf = PyBytes_AS_STRING(newstring); for (i = j = 0; i < hexlen; i += 2) { /* skip over spaces in the input */ - while (hex[i] == ' ') + while (PyUnicode_READ(kind, data, i) == ' ') i++; if (i >= hexlen) break; - top = hex_digit_to_int(hex[i]); - bot = hex_digit_to_int(hex[i+1]); + top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); + bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); if (top == -1 || bot == -1) { PyErr_Format(PyExc_ValueError, "non-hexadecimal number found in " @@ -2462,7 +2459,7 @@ bytes_methods[] = { {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__}, {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__}, {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__}, - {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS, + {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, startswith__doc__}, diff --git a/Objects/classobject.c b/Objects/classobject.c index 6df930f..b52a209 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -190,8 +190,7 @@ method_richcompare(PyObject *self, PyObject *other, int op) !PyMethod_Check(self) || !PyMethod_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } a = (PyMethodObject *)self; b = (PyMethodObject *)other; @@ -516,8 +515,7 @@ instancemethod_richcompare(PyObject *self, PyObject *other, int op) !PyInstanceMethod_Check(self) || !PyInstanceMethod_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } a = (PyInstanceMethodObject *)self; b = (PyInstanceMethodObject *)other; diff --git a/Objects/codeobject.c b/Objects/codeobject.c index bb938ea..0489c7b 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -8,19 +8,24 @@ /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ static int -all_name_chars(Py_UNICODE *s) +all_name_chars(PyObject *o) { static char ok_name_char[256]; static unsigned char *name_chars = (unsigned char *)NAME_CHARS; + PyUnicodeObject *u = (PyUnicodeObject *)o; + const unsigned char *s; + + if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 || + PyUnicode_MAX_CHAR_VALUE(u) >= 128) + return 0; if (ok_name_char[*name_chars] == 0) { unsigned char *p; for (p = name_chars; *p; p++) ok_name_char[*p] = 1; } + s = PyUnicode_1BYTE_DATA(u); while (*s) { - if (*s >= 128) - return 0; if (ok_name_char[*s++] == 0) return 0; } @@ -51,7 +56,8 @@ PyCode_New(int argcount, int kwonlyargcount, PyObject *lnotab) { PyCodeObject *co; - Py_ssize_t i; + unsigned char *cell2arg = NULL; + Py_ssize_t i, n_cellvars; /* Check argument types */ if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || @@ -68,48 +74,79 @@ PyCode_New(int argcount, int kwonlyargcount, PyErr_BadInternalCall(); return NULL; } + n_cellvars = PyTuple_GET_SIZE(cellvars); intern_strings(names); intern_strings(varnames); intern_strings(freevars); intern_strings(cellvars); /* Intern selected string constants */ - for (i = PyTuple_Size(consts); --i >= 0; ) { + for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) { PyObject *v = PyTuple_GetItem(consts, i); - if (!PyUnicode_Check(v)) - continue; - if (!all_name_chars(PyUnicode_AS_UNICODE(v))) + if (!all_name_chars(v)) continue; PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); } + /* Create mapping between cells and arguments if needed. */ + if (n_cellvars) { + Py_ssize_t total_args = argcount + kwonlyargcount + + ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); + Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars; + int used_cell2arg = 0; + cell2arg = PyMem_MALLOC(alloc_size); + if (cell2arg == NULL) + return NULL; + memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size); + /* Find cells which are also arguments. */ + for (i = 0; i < n_cellvars; i++) { + Py_ssize_t j; + PyObject *cell = PyTuple_GET_ITEM(cellvars, i); + for (j = 0; j < total_args; j++) { + PyObject *arg = PyTuple_GET_ITEM(varnames, j); + if (!PyUnicode_Compare(cell, arg)) { + cell2arg[i] = j; + used_cell2arg = 1; + break; + } + } + } + if (!used_cell2arg) { + PyMem_FREE(cell2arg); + cell2arg = NULL; + } + } co = PyObject_NEW(PyCodeObject, &PyCode_Type); - if (co != NULL) { - co->co_argcount = argcount; - co->co_kwonlyargcount = kwonlyargcount; - co->co_nlocals = nlocals; - co->co_stacksize = stacksize; - co->co_flags = flags; - Py_INCREF(code); - co->co_code = code; - Py_INCREF(consts); - co->co_consts = consts; - Py_INCREF(names); - co->co_names = names; - Py_INCREF(varnames); - co->co_varnames = varnames; - Py_INCREF(freevars); - co->co_freevars = freevars; - Py_INCREF(cellvars); - co->co_cellvars = cellvars; - Py_INCREF(filename); - co->co_filename = filename; - Py_INCREF(name); - co->co_name = name; - co->co_firstlineno = firstlineno; - Py_INCREF(lnotab); - co->co_lnotab = lnotab; - co->co_zombieframe = NULL; - co->co_weakreflist = NULL; + if (co == NULL) { + if (cell2arg) + PyMem_FREE(cell2arg); + return NULL; } + co->co_argcount = argcount; + co->co_kwonlyargcount = kwonlyargcount; + co->co_nlocals = nlocals; + co->co_stacksize = stacksize; + co->co_flags = flags; + Py_INCREF(code); + co->co_code = code; + Py_INCREF(consts); + co->co_consts = consts; + Py_INCREF(names); + co->co_names = names; + Py_INCREF(varnames); + co->co_varnames = varnames; + Py_INCREF(freevars); + co->co_freevars = freevars; + Py_INCREF(cellvars); + co->co_cellvars = cellvars; + co->co_cell2arg = cell2arg; + Py_INCREF(filename); + co->co_filename = filename; + Py_INCREF(name); + co->co_name = name; + co->co_firstlineno = firstlineno; + Py_INCREF(lnotab); + co->co_lnotab = lnotab; + co->co_zombieframe = NULL; + co->co_weakreflist = NULL; return co; } @@ -330,6 +367,8 @@ code_dealloc(PyCodeObject *co) Py_XDECREF(co->co_filename); Py_XDECREF(co->co_name); Py_XDECREF(co->co_lnotab); + if (co->co_cell2arg != NULL) + PyMem_FREE(co->co_cell2arg); if (co->co_zombieframe != NULL) PyObject_GC_Del(co->co_zombieframe); if (co->co_weakreflist != NULL) @@ -366,8 +405,7 @@ code_richcompare(PyObject *self, PyObject *other, int op) if ((op != Py_EQ && op != Py_NE) || !PyCode_Check(self) || !PyCode_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } co = (PyCodeObject *)self; diff --git a/Objects/complexobject.c b/Objects/complexobject.c index e247ba9..1e61b96 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -330,12 +330,10 @@ complex_repr(PyComplexObject *v) int precision = 0; char format_code = 'r'; PyObject *result = NULL; - Py_ssize_t len; /* If these are non-NULL, they'll need to be freed. */ char *pre = NULL; char *im = NULL; - char *buf = NULL; /* These do not need to be freed. re is either an alias for pre or a pointer to a constant. lead and tail @@ -374,20 +372,10 @@ complex_repr(PyComplexObject *v) lead = "("; tail = ")"; } - /* Alloc the final buffer. Add one for the "j" in the format string, - and one for the trailing zero byte. */ - len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2; - buf = PyMem_Malloc(len); - if (!buf) { - PyErr_NoMemory(); - goto done; - } - PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail); - result = PyUnicode_FromString(buf); + result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail); done: PyMem_Free(im); PyMem_Free(pre); - PyMem_Free(buf); return result; } @@ -662,8 +650,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op) return res; Unimplemented: - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } static PyObject * @@ -715,9 +702,8 @@ complex__format__(PyObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - return _PyComplex_FormatAdvanced(self, - PyUnicode_AS_UNICODE(format_spec), - PyUnicode_GET_SIZE(format_spec)); + return _PyComplex_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); } #if 0 @@ -768,20 +754,10 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) Py_ssize_t len; if (PyUnicode_Check(v)) { - Py_ssize_t i, buflen = PyUnicode_GET_SIZE(v); - Py_UNICODE *bufptr; - s_buffer = PyUnicode_TransformDecimalToASCII( - PyUnicode_AS_UNICODE(v), buflen); + s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v); if (s_buffer == NULL) return NULL; - /* Replace non-ASCII whitespace with ' ' */ - bufptr = PyUnicode_AS_UNICODE(s_buffer); - for (i = 0; i < buflen; i++) { - Py_UNICODE ch = bufptr[i]; - if (ch > 127 && Py_UNICODE_ISSPACE(ch)) - bufptr[i] = ' '; - } - s = _PyUnicode_AsStringAndSize(s_buffer, &len); + s = PyUnicode_AsUTF8AndSize(s_buffer, &len); if (s == NULL) goto error; } diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 3fa5cc4..c4265da 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -418,7 +418,7 @@ lookdict_unicode(PyDictObject *mp, PyObject *key, register Py_hash_t hash) mp->ma_lookup = lookdict; return lookdict(mp, key, hash); } - i = hash & mask; + i = (size_t)hash & mask; ep = &ep0[i]; if (ep->me_key == NULL || ep->me_key == key) return ep; @@ -572,7 +572,7 @@ insertdict_clean(register PyDictObject *mp, PyObject *key, Py_hash_t hash, register PyDictEntry *ep; MAINTAIN_TRACKING(mp, key, value); - i = hash & mask; + i = (size_t)hash & mask; ep = &ep0[i]; for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { i = (i << 2) + i + perturb + 1; @@ -710,7 +710,7 @@ PyDict_GetItem(PyObject *op, PyObject *key) if (!PyDict_Check(op)) return NULL; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) { @@ -762,7 +762,7 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key) return NULL; } if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) { @@ -797,7 +797,7 @@ PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) assert(value); mp = (PyDictObject *)op; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) @@ -842,7 +842,7 @@ PyDict_DelItem(PyObject *op, PyObject *key) } assert(key); if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return -1; @@ -1122,7 +1122,7 @@ dict_subscript(PyDictObject *mp, register PyObject *key) PyDictEntry *ep; assert(mp->ma_table != NULL); if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; @@ -1726,7 +1726,7 @@ dict_contains(register PyDictObject *mp, PyObject *key) PyDictEntry *ep; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; @@ -1750,7 +1750,7 @@ dict_get(register PyDictObject *mp, PyObject *args) return NULL; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; @@ -1779,7 +1779,7 @@ dict_setdefault(register PyDictObject *mp, PyObject *args) return NULL; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; @@ -1824,7 +1824,7 @@ dict_pop(PyDictObject *mp, PyObject *args) return NULL; } if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; @@ -2033,7 +2033,7 @@ PyDict_Contains(PyObject *op, PyObject *key) PyDictEntry *ep; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return -1; @@ -2608,10 +2608,8 @@ dictview_richcompare(PyObject *self, PyObject *other, int op) assert(PyDictViewSet_Check(self)); assert(other != NULL); - if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; len_self = PyObject_Size(self); if (len_self < 0) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 0106ba3..60b340b 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -499,7 +499,7 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) Py_CLEAR(self->code); if (size == 1) self->code = PyTuple_GET_ITEM(args, 0); - else if (size > 1) + else /* size > 1 */ self->code = args; Py_INCREF(self->code); return 0; @@ -962,21 +962,23 @@ SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) static PyObject* my_basename(PyObject *name) { - Py_UNICODE *unicode; Py_ssize_t i, size, offset; + int kind; + void *data; - unicode = PyUnicode_AS_UNICODE(name); - size = PyUnicode_GET_SIZE(name); + if (PyUnicode_READY(name)) + return NULL; + kind = PyUnicode_KIND(name); + data = PyUnicode_DATA(name); + size = PyUnicode_GET_LENGTH(name); offset = 0; for(i=0; i < size; i++) { - if (unicode[i] == SEP) + if (PyUnicode_READ(kind, data, i) == SEP) offset = i + 1; } - if (offset != 0) { - return PyUnicode_FromUnicode( - PyUnicode_AS_UNICODE(name) + offset, - size - offset); - } else { + if (offset != 0) + return PyUnicode_Substring(name, offset, size); + else { Py_INCREF(name); return name; } @@ -1712,6 +1714,7 @@ static PyTypeObject _PyExc_UnicodeTranslateError = { }; PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError; +/* Deprecated. */ PyObject * PyUnicodeTranslateError_Create( const Py_UNICODE *object, Py_ssize_t length, @@ -1721,6 +1724,14 @@ PyUnicodeTranslateError_Create( object, length, start, end, reason); } +PyObject * +_PyUnicodeTranslateError_Create( + PyObject *object, + Py_ssize_t start, Py_ssize_t end, const char *reason) +{ + return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Ons", + object, start, end, reason); +} /* * AssertionError extends Exception diff --git a/Objects/fileobject.c b/Objects/fileobject.c index d20f196..6421543 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -103,23 +103,18 @@ PyFile_GetLine(PyObject *f, int n) } } if (n < 0 && result != NULL && PyUnicode_Check(result)) { - Py_UNICODE *s = PyUnicode_AS_UNICODE(result); - Py_ssize_t len = PyUnicode_GET_SIZE(result); + Py_ssize_t len = PyUnicode_GET_LENGTH(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } - else if (s[len-1] == '\n') { - if (result->ob_refcnt == 1) - PyUnicode_Resize(&result, len-1); - else { - PyObject *v; - v = PyUnicode_FromUnicode(s, len-1); - Py_DECREF(result); - result = v; - } + else if (PyUnicode_READ_CHAR(result, len-1) == '\n') { + PyObject *v; + v = PyUnicode_Substring(result, 0, len-1); + Py_DECREF(result); + result = v; } } return result; @@ -297,8 +292,8 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) *p++ = c; if (c == '\n') break; } - if ( c == EOF && skipnextlf ) - newlinetypes |= NEWLINE_CR; + /* if ( c == EOF && skipnextlf ) + newlinetypes |= NEWLINE_CR; */ FUNLOCKFILE(stream); *p = '\0'; if ( skipnextlf ) { diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 09c0e961..1c8a6a3 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -15,11 +15,6 @@ #define MIN(x, y) ((x) < (y) ? (x) : (y)) -#ifdef _OSF_SOURCE -/* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ -extern int finite(double); -#endif - /* Special free list Since some Python programs can spend much of their time allocating @@ -179,25 +174,14 @@ PyFloat_FromString(PyObject *v) PyObject *result = NULL; if (PyUnicode_Check(v)) { - Py_ssize_t i, buflen = PyUnicode_GET_SIZE(v); - Py_UNICODE *bufptr; - s_buffer = PyUnicode_TransformDecimalToASCII( - PyUnicode_AS_UNICODE(v), buflen); + s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v); if (s_buffer == NULL) return NULL; - /* Replace non-ASCII whitespace with ' ' */ - bufptr = PyUnicode_AS_UNICODE(s_buffer); - for (i = 0; i < buflen; i++) { - Py_UNICODE ch = bufptr[i]; - if (ch > 127 && Py_UNICODE_ISSPACE(ch)) - bufptr[i] = ' '; - } - s = _PyUnicode_AsStringAndSize(s_buffer, &len); + s = PyUnicode_AsUTF8AndSize(s_buffer, &len); if (s == NULL) { Py_DECREF(s_buffer); return NULL; } - last = s + len; } else if (PyObject_AsCharBuffer(v, &s, &len)) { PyErr_SetString(PyExc_TypeError, @@ -523,8 +507,7 @@ float_richcompare(PyObject *v, PyObject *w, int op) return PyBool_FromLong(r); Unimplemented: - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } static Py_hash_t @@ -1748,9 +1731,8 @@ float__format__(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - return _PyFloat_FormatAdvanced(self, - PyUnicode_AS_UNICODE(format_spec), - PyUnicode_GET_SIZE(format_spec)); + return _PyFloat_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); } PyDoc_STRVAR(float__format__doc, @@ -2246,7 +2228,7 @@ _PyFloat_Pack8(double x, unsigned char *p, int le) /* Eighth byte */ *p = flo & 0xFF; - p += incr; + /* p += incr; */ /* Done */ return 0; diff --git a/Objects/genobject.c b/Objects/genobject.c index 01cd44a..43d0335 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -406,15 +406,13 @@ PyGen_NeedsFinalizing(PyGenObject *gen) int i; PyFrameObject *f = gen->gi_frame; - if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0) + if (f == NULL || f->f_stacktop == NULL) return 0; /* no frame or empty blockstack == no finalization */ /* Any block type besides a loop requires cleanup. */ - i = f->f_iblock; - while (--i >= 0) { + for (i = 0; i < f->f_iblock; i++) if (f->f_blockstack[i].b_type != SETUP_LOOP) return 1; - } /* No blocks except loops, it's safe to skip finalization. */ return 0; diff --git a/Objects/listobject.c b/Objects/listobject.c index 00de597..049f2a8 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -736,6 +736,19 @@ listinsert(PyListObject *self, PyObject *args) } static PyObject * +listclear(PyListObject *self) +{ + list_clear(self); + Py_RETURN_NONE; +} + +static PyObject * +listcopy(PyListObject *self) +{ + return list_slice(self, 0, Py_SIZE(self)); +} + +static PyObject * listappend(PyListObject *self, PyObject *v) { if (app1(self, v) == 0) @@ -2201,10 +2214,8 @@ list_richcompare(PyObject *v, PyObject *w, int op) PyListObject *vl, *wl; Py_ssize_t i; - if (!PyList_Check(v) || !PyList_Check(w)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyList_Check(v) || !PyList_Check(w)) + Py_RETURN_NOTIMPLEMENTED; vl = (PyListObject *)v; wl = (PyListObject *)w; @@ -2313,17 +2324,21 @@ PyDoc_STRVAR(reversed_doc, "L.__reversed__() -- return a reverse iterator over the list"); PyDoc_STRVAR(sizeof_doc, "L.__sizeof__() -- size of L in memory, in bytes"); +PyDoc_STRVAR(clear_doc, +"L.clear() -> None -- remove all items from L"); +PyDoc_STRVAR(copy_doc, +"L.copy() -> list -- a shallow copy of L"); PyDoc_STRVAR(append_doc, -"L.append(object) -- append object to end"); +"L.append(object) -> None -- append object to end"); PyDoc_STRVAR(extend_doc, -"L.extend(iterable) -- extend list by appending elements from the iterable"); +"L.extend(iterable) -> None -- extend list by appending elements from the iterable"); PyDoc_STRVAR(insert_doc, "L.insert(index, object) -- insert object before index"); PyDoc_STRVAR(pop_doc, "L.pop([index]) -> item -- remove and return item at index (default last).\n" "Raises IndexError if list is empty or index is out of range."); PyDoc_STRVAR(remove_doc, -"L.remove(value) -- remove first occurrence of value.\n" +"L.remove(value) -> None -- remove first occurrence of value.\n" "Raises ValueError if the value is not present."); PyDoc_STRVAR(index_doc, "L.index(value, [start, [stop]]) -> integer -- return first index of value.\n" @@ -2333,7 +2348,7 @@ PyDoc_STRVAR(count_doc, PyDoc_STRVAR(reverse_doc, "L.reverse() -- reverse *IN PLACE*"); PyDoc_STRVAR(sort_doc, -"L.sort(key=None, reverse=False) -- stable sort *IN PLACE*"); +"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*"); static PyObject *list_subscript(PyListObject*, PyObject*); @@ -2341,9 +2356,11 @@ static PyMethodDef list_methods[] = { {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc}, {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc}, + {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc}, + {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc}, {"append", (PyCFunction)listappend, METH_O, append_doc}, {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, - {"extend", (PyCFunction)listextend, METH_O, extend_doc}, + {"extend", (PyCFunction)listextend, METH_O, extend_doc}, {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, {"remove", (PyCFunction)listremove, METH_O, remove_doc}, {"index", (PyCFunction)listindex, METH_VARARGS, index_doc}, @@ -2406,7 +2423,7 @@ list_subscript(PyListObject* self, PyObject* item) src = self->ob_item; dest = ((PyListObject *)result)->ob_item; for (cur = start, i = 0; i < slicelength; - cur += step, i++) { + cur += (size_t)step, i++) { it = src[cur]; Py_INCREF(it); dest[i] = it; @@ -2497,7 +2514,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) self->ob_item + cur + 1, lim * sizeof(PyObject *)); } - cur = start + slicelength*step; + cur = start + (size_t)slicelength * step; if (cur < (size_t)Py_SIZE(self)) { memmove(self->ob_item + cur - slicelength, self->ob_item + cur, @@ -2561,7 +2578,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) selfitems = self->ob_item; seqitems = PySequence_Fast_ITEMS(seq); for (cur = start, i = 0; i < slicelength; - cur += step, i++) { + cur += (size_t)step, i++) { garbage[i] = selfitems[cur]; ins = seqitems[i]; Py_INCREF(ins); diff --git a/Objects/longobject.c b/Objects/longobject.c index a0b16a6..43e5f01 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -1193,10 +1193,14 @@ PyLong_AsUnsignedLongLong(PyObject *vv) int one = 1; int res; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); return (unsigned PY_LONG_LONG)-1; } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (unsigned PY_LONG_LONG)-1; + } v = (PyLongObject*)vv; switch(Py_SIZE(v)) { @@ -1382,10 +1386,8 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) #define CHECK_BINOP(v,w) \ do { \ - if (!PyLong_Check(v) || !PyLong_Check(w)) { \ - Py_INCREF(Py_NotImplemented); \ - return Py_NotImplemented; \ - } \ + if (!PyLong_Check(v) || !PyLong_Check(w)) \ + Py_RETURN_NOTIMPLEMENTED; \ } while(0) /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d < @@ -1549,7 +1551,7 @@ long_to_decimal_string(PyObject *aa) PyObject *str; Py_ssize_t size, strlen, size_a, i, j; digit *pout, *pin, rem, tenpow; - Py_UNICODE *p; + unsigned char *p; int negative; a = (PyLongObject *)aa; @@ -1617,14 +1619,15 @@ long_to_decimal_string(PyObject *aa) tenpow *= 10; strlen++; } - str = PyUnicode_FromUnicode(NULL, strlen); + str = PyUnicode_New(strlen, '9'); if (str == NULL) { Py_DECREF(scratch); return NULL; } /* fill the string right-to-left */ - p = PyUnicode_AS_UNICODE(str) + strlen; + assert(PyUnicode_KIND(str) == PyUnicode_1BYTE_KIND); + p = PyUnicode_1BYTE_DATA(str) + strlen; *p = '\0'; /* pout[0] through pout[size-2] contribute exactly _PyLong_DECIMAL_SHIFT digits each */ @@ -1647,7 +1650,7 @@ long_to_decimal_string(PyObject *aa) *--p = '-'; /* check we've counted correctly */ - assert(p == PyUnicode_AS_UNICODE(str)); + assert(p == PyUnicode_1BYTE_DATA(str)); Py_DECREF(scratch); return (PyObject *)str; } @@ -1660,10 +1663,12 @@ PyObject * _PyLong_Format(PyObject *aa, int base) { register PyLongObject *a = (PyLongObject *)aa; - PyObject *str; + PyObject *v; Py_ssize_t i, sz; Py_ssize_t size_a; - Py_UNICODE *p, sign = '\0'; + char *p; + char sign = '\0'; + char *buffer; int bits; assert(base == 2 || base == 8 || base == 10 || base == 16); @@ -1693,7 +1698,7 @@ _PyLong_Format(PyObject *aa, int base) } /* compute length of output string: allow 2 characters for prefix and 1 for possible '-' sign. */ - if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { + if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT / sizeof(Py_UCS4)) { PyErr_SetString(PyExc_OverflowError, "int is too large to format"); return NULL; @@ -1702,11 +1707,12 @@ _PyLong_Format(PyObject *aa, int base) is safe from overflow */ sz = 3 + (size_a * PyLong_SHIFT + (bits - 1)) / bits; assert(sz >= 0); - str = PyUnicode_FromUnicode(NULL, sz); - if (str == NULL) + buffer = PyMem_Malloc(sz); + if (buffer == NULL) { + PyErr_NoMemory(); return NULL; - p = PyUnicode_AS_UNICODE(str) + sz; - *p = '\0'; + } + p = &buffer[sz]; if (Py_SIZE(a) < 0) sign = '-'; @@ -1722,10 +1728,10 @@ _PyLong_Format(PyObject *aa, int base) accumbits += PyLong_SHIFT; assert(accumbits >= bits); do { - Py_UNICODE cdigit; - cdigit = (Py_UNICODE)(accum & (base - 1)); + char cdigit; + cdigit = (char)(accum & (base - 1)); cdigit += (cdigit < 10) ? '0' : 'a'-10; - assert(p > PyUnicode_AS_UNICODE(str)); + assert(p > buffer); *--p = cdigit; accumbits -= bits; accum >>= bits; @@ -1742,19 +1748,9 @@ _PyLong_Format(PyObject *aa, int base) *--p = '0'; if (sign) *--p = sign; - if (p != PyUnicode_AS_UNICODE(str)) { - Py_UNICODE *q = PyUnicode_AS_UNICODE(str); - assert(p > q); - do { - } while ((*q++ = *p++) != '\0'); - q--; - if (PyUnicode_Resize(&str,(Py_ssize_t) (q - - PyUnicode_AS_UNICODE(str)))) { - Py_DECREF(str); - return NULL; - } - } - return (PyObject *)str; + v = PyUnicode_DecodeASCII(p, &buffer[sz] - p, NULL); + PyMem_Free(buffer); + return v; } /* Table of digit values for 8-bit string -> integer conversion. @@ -2132,23 +2128,26 @@ digit beyond the first. PyObject * PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) { + PyObject *v, *unicode = PyUnicode_FromUnicode(u, length); + if (unicode == NULL) + return NULL; + v = PyLong_FromUnicodeObject(unicode, base); + Py_DECREF(unicode); + return v; +} + +PyObject * +PyLong_FromUnicodeObject(PyObject *u, int base) +{ PyObject *result; PyObject *asciidig; char *buffer, *end; - Py_ssize_t i, buflen; - Py_UNICODE *ptr; + Py_ssize_t buflen; - asciidig = PyUnicode_TransformDecimalToASCII(u, length); + asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); if (asciidig == NULL) return NULL; - /* Replace non-ASCII whitespace with ' ' */ - ptr = PyUnicode_AS_UNICODE(asciidig); - for (i = 0; i < length; i++) { - Py_UNICODE ch = ptr[i]; - if (ch > 127 && Py_UNICODE_ISSPACE(ch)) - ptr[i] = ' '; - } - buffer = _PyUnicode_AsStringAndSize(asciidig, &buflen); + buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); if (buffer == NULL) { Py_DECREF(asciidig); return NULL; @@ -2445,8 +2444,7 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) break; } } - assert(1 <= x_size && - x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit))); + assert(1 <= x_size && x_size <= (Py_ssize_t)Py_ARRAY_LENGTH(x_digits)); /* Round, and convert to double. */ x_digits[0] += half_even_correction[x_digits[0] & 7]; @@ -2483,10 +2481,14 @@ PyLong_AsDouble(PyObject *v) Py_ssize_t exponent; double x; - if (v == NULL || !PyLong_Check(v)) { + if (v == NULL) { PyErr_BadInternalCall(); return -1.0; } + if (!PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1.0; + } x = _PyLong_Frexp((PyLongObject *)v, &exponent); if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { PyErr_SetString(PyExc_OverflowError, @@ -3611,8 +3613,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) else { Py_DECREF(a); Py_DECREF(b); - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } if (Py_SIZE(b) < 0) { /* if exponent is negative */ @@ -4139,9 +4140,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } if (PyUnicode_Check(x)) - return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), - PyUnicode_GET_SIZE(x), - (int)base); + return PyLong_FromUnicodeObject(x, (int)base); else if (PyByteArray_Check(x) || PyBytes_Check(x)) { /* Since PyLong_FromString doesn't have a length parameter, * check here for possible NULs in the string. */ @@ -4223,9 +4222,8 @@ long__format__(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - return _PyLong_FormatAdvanced(self, - PyUnicode_AS_UNICODE(format_spec), - PyUnicode_GET_SIZE(format_spec)); + return _PyLong_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); } /* Return a pair (q, r) such that a = b * q + r, and diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 2e32b2a..e0d1a89 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -773,8 +773,7 @@ _end: _notimpl: PyBuffer_Release(&vv); PyBuffer_Release(&ww); - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } diff --git a/Objects/methodobject.c b/Objects/methodobject.c index f1a9f4b..a7334cf 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -208,8 +208,7 @@ meth_richcompare(PyObject *self, PyObject *other, int op) !PyCFunction_Check(self) || !PyCFunction_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } a = (PyCFunctionObject *)self; b = (PyCFunctionObject *)other; diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index f31b5da..4b4f9d1 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -27,36 +27,45 @@ static PyTypeObject moduledef_type = { PyObject * -PyModule_New(const char *name) +PyModule_NewObject(PyObject *name) { PyModuleObject *m; - PyObject *nameobj; m = PyObject_GC_New(PyModuleObject, &PyModule_Type); if (m == NULL) return NULL; m->md_def = NULL; m->md_state = NULL; - nameobj = PyUnicode_FromString(name); m->md_dict = PyDict_New(); - if (m->md_dict == NULL || nameobj == NULL) + if (m->md_dict == NULL) goto fail; - if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0) + if (PyDict_SetItemString(m->md_dict, "__name__", name) != 0) goto fail; if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0) goto fail; if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0) goto fail; - Py_DECREF(nameobj); PyObject_GC_Track(m); return (PyObject *)m; fail: - Py_XDECREF(nameobj); Py_DECREF(m); return NULL; } PyObject * +PyModule_New(const char *name) +{ + PyObject *nameobj, *module; + nameobj = PyUnicode_FromString(name); + if (nameobj == NULL) + return NULL; + module = PyModule_NewObject(nameobj); + Py_DECREF(nameobj); + return module; +} + + +PyObject * PyModule_Create2(struct PyModuleDef* module, int module_api_version) { PyObject *d, *v, *n; @@ -169,24 +178,35 @@ PyModule_GetDict(PyObject *m) return d; } -const char * -PyModule_GetName(PyObject *m) +PyObject* +PyModule_GetNameObject(PyObject *m) { PyObject *d; - PyObject *nameobj; + PyObject *name; if (!PyModule_Check(m)) { PyErr_BadArgument(); return NULL; } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || - (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || - !PyUnicode_Check(nameobj)) + (name = PyDict_GetItemString(d, "__name__")) == NULL || + !PyUnicode_Check(name)) { PyErr_SetString(PyExc_SystemError, "nameless module"); return NULL; } - return _PyUnicode_AsString(nameobj); + Py_INCREF(name); + return name; +} + +const char * +PyModule_GetName(PyObject *m) +{ + PyObject *name = PyModule_GetNameObject(m); + if (name == NULL) + return NULL; + Py_DECREF(name); /* module dict has still a reference */ + return _PyUnicode_AsString(name); } PyObject* @@ -219,7 +239,7 @@ PyModule_GetFilename(PyObject *m) if (fileobj == NULL) return NULL; utf8 = _PyUnicode_AsString(fileobj); - Py_DECREF(fileobj); + Py_DECREF(fileobj); /* module dict has still a reference */ return utf8; } @@ -265,8 +285,8 @@ _PyModule_Clear(PyObject *m) pos = 0; while (PyDict_Next(d, &pos, &key, &value)) { if (value != Py_None && PyUnicode_Check(key)) { - Py_UNICODE *u = PyUnicode_AS_UNICODE(key); - if (u[0] == '_' && u[1] != '_') { + if (PyUnicode_READ_CHAR(key, 0) == '_' && + PyUnicode_READ_CHAR(key, 1) != '_') { if (Py_VerboseFlag > 1) { const char *s = _PyUnicode_AsString(key); if (s != NULL) @@ -283,9 +303,8 @@ _PyModule_Clear(PyObject *m) pos = 0; while (PyDict_Next(d, &pos, &key, &value)) { if (value != Py_None && PyUnicode_Check(key)) { - Py_UNICODE *u = PyUnicode_AS_UNICODE(key); - if (u[0] != '_' - || PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) + if (PyUnicode_READ_CHAR(key, 0) != '_' || + PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) { if (Py_VerboseFlag > 1) { const char *s = _PyUnicode_AsString(key); @@ -347,21 +366,25 @@ module_dealloc(PyModuleObject *m) static PyObject * module_repr(PyModuleObject *m) { - const char *name; - PyObject *filename, *repr; + PyObject *name, *filename, *repr; - name = PyModule_GetName((PyObject *)m); + name = PyModule_GetNameObject((PyObject *)m); if (name == NULL) { PyErr_Clear(); - name = "?"; + name = PyUnicode_FromStringAndSize("?", 1); + if (name == NULL) + return NULL; } filename = PyModule_GetFilenameObject((PyObject *)m); if (filename == NULL) { PyErr_Clear(); - return PyUnicode_FromFormat("<module '%s' (built-in)>", name); + repr = PyUnicode_FromFormat("<module %R (built-in)>", name); + } + else { + repr = PyUnicode_FromFormat("<module %R from %R>", name, filename); + Py_DECREF(filename); } - repr = PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename); - Py_DECREF(filename); + Py_DECREF(name); return repr; } @@ -389,6 +412,34 @@ module_clear(PyModuleObject *m) return 0; } +static PyObject * +module_dir(PyObject *self, PyObject *args) +{ + PyObject *result = NULL; + PyObject *dict = PyObject_GetAttrString(self, "__dict__"); + + if (dict != NULL) { + if (PyDict_Check(dict)) + result = PyDict_Keys(dict); + else { + const char *name = PyModule_GetName(self); + if (name) + PyErr_Format(PyExc_TypeError, + "%.200s.__dict__ is not a dictionary", + name); + } + } + + Py_XDECREF(dict); + return result; +} + +static PyMethodDef module_methods[] = { + {"__dir__", module_dir, METH_NOARGS, + PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")}, + {0} +}; + PyDoc_STRVAR(module_doc, "module(name[, doc])\n\ @@ -425,7 +476,7 @@ PyTypeObject PyModule_Type = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + module_methods, /* tp_methods */ module_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ diff --git a/Objects/object.c b/Objects/object.c index 694e7e7..aeaa4b5 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1,5 +1,5 @@ -/* Generic object operations; and implementation of None (NoObject) */ +/* Generic object operations; and implementation of None */ #include "Python.h" #include "frameobject.h" @@ -29,8 +29,6 @@ _Py_GetRefTotal(void) } #endif /* Py_REF_DEBUG */ -int Py_DivisionWarningFlag; - /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. These are used by the individual routines for object creation. Do not call them otherwise, they do not initialize the object! */ @@ -297,9 +295,7 @@ PyObject_Print(PyObject *op, FILE *fp, int flags) } else if (PyUnicode_Check(s)) { PyObject *t; - t = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(s), - PyUnicode_GET_SIZE(s), - "backslashreplace"); + t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace"); if (t == NULL) ret = 0; else { @@ -441,11 +437,7 @@ PyObject_ASCII(PyObject *v) return NULL; /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ - ascii = PyUnicode_EncodeASCII( - PyUnicode_AS_UNICODE(repr), - PyUnicode_GET_SIZE(repr), - "backslashreplace"); - + ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); Py_DECREF(repr); if (ascii == NULL) return NULL; @@ -1184,66 +1176,6 @@ PyCallable_Check(PyObject *x) return x->ob_type->tp_call != NULL; } -/* ------------------------- PyObject_Dir() helpers ------------------------- */ - -/* Helper for PyObject_Dir. - Merge the __dict__ of aclass into dict, and recursively also all - the __dict__s of aclass's base classes. The order of merging isn't - defined, as it's expected that only the final set of dict keys is - interesting. - Return 0 on success, -1 on error. -*/ - -static int -merge_class_dict(PyObject* dict, PyObject* aclass) -{ - PyObject *classdict; - PyObject *bases; - - assert(PyDict_Check(dict)); - assert(aclass); - - /* Merge in the type's dict (if any). */ - classdict = PyObject_GetAttrString(aclass, "__dict__"); - if (classdict == NULL) - PyErr_Clear(); - else { - int status = PyDict_Update(dict, classdict); - Py_DECREF(classdict); - if (status < 0) - return -1; - } - - /* Recursively merge in the base types' (if any) dicts. */ - bases = PyObject_GetAttrString(aclass, "__bases__"); - if (bases == NULL) - PyErr_Clear(); - else { - /* We have no guarantee that bases is a real tuple */ - Py_ssize_t i, n; - n = PySequence_Size(bases); /* This better be right */ - if (n < 0) - PyErr_Clear(); - else { - for (i = 0; i < n; i++) { - int status; - PyObject *base = PySequence_GetItem(bases, i); - if (base == NULL) { - Py_DECREF(bases); - return -1; - } - status = merge_class_dict(dict, base); - Py_DECREF(base); - if (status < 0) { - Py_DECREF(bases); - return -1; - } - } - } - Py_DECREF(bases); - } - return 0; -} /* Helper for PyObject_Dir without arguments: returns the local scope. */ static PyObject * @@ -1267,140 +1199,43 @@ _dir_locals(void) Py_DECREF(names); return NULL; } + if (PyList_Sort(names)) { + Py_DECREF(names); + return NULL; + } /* the locals don't need to be DECREF'd */ return names; } -/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. - We deliberately don't suck up its __class__, as methods belonging to the - metaclass would probably be more confusing than helpful. -*/ -static PyObject * -_specialized_dir_type(PyObject *obj) -{ - PyObject *result = NULL; - PyObject *dict = PyDict_New(); - - if (dict != NULL && merge_class_dict(dict, obj) == 0) - result = PyDict_Keys(dict); - - Py_XDECREF(dict); - return result; -} - -/* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ -static PyObject * -_specialized_dir_module(PyObject *obj) -{ - PyObject *result = NULL; - PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); - - if (dict != NULL) { - if (PyDict_Check(dict)) - result = PyDict_Keys(dict); - else { - const char *name = PyModule_GetName(obj); - if (name) - PyErr_Format(PyExc_TypeError, - "%.200s.__dict__ is not a dictionary", - name); - } - } - - Py_XDECREF(dict); - return result; -} - -/* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, - and recursively up the __class__.__bases__ chain. -*/ -static PyObject * -_generic_dir(PyObject *obj) -{ - PyObject *result = NULL; - PyObject *dict = NULL; - PyObject *itsclass = NULL; - - /* Get __dict__ (which may or may not be a real dict...) */ - dict = PyObject_GetAttrString(obj, "__dict__"); - if (dict == NULL) { - PyErr_Clear(); - dict = PyDict_New(); - } - else if (!PyDict_Check(dict)) { - Py_DECREF(dict); - dict = PyDict_New(); - } - else { - /* Copy __dict__ to avoid mutating it. */ - PyObject *temp = PyDict_Copy(dict); - Py_DECREF(dict); - dict = temp; - } - - if (dict == NULL) - goto error; - - /* Merge in attrs reachable from its class. */ - itsclass = PyObject_GetAttrString(obj, "__class__"); - if (itsclass == NULL) - /* XXX(tomer): Perhaps fall back to obj->ob_type if no - __class__ exists? */ - PyErr_Clear(); - else { - if (merge_class_dict(dict, itsclass) != 0) - goto error; - } - - result = PyDict_Keys(dict); - /* fall through */ -error: - Py_XDECREF(itsclass); - Py_XDECREF(dict); - return result; -} - -/* Helper for PyObject_Dir: object introspection. - This calls one of the above specialized versions if no __dir__ method - exists. */ +/* Helper for PyObject_Dir: object introspection. */ static PyObject * _dir_object(PyObject *obj) { - PyObject *result = NULL; + PyObject *result, *sorted; static PyObject *dir_str = NULL; PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str); assert(obj); if (dirfunc == NULL) { - if (PyErr_Occurred()) - return NULL; - /* use default implementation */ - if (PyModule_Check(obj)) - result = _specialized_dir_module(obj); - else if (PyType_Check(obj)) - result = _specialized_dir_type(obj); - else - result = _generic_dir(obj); + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); + return NULL; } - else { - /* use __dir__ */ - result = PyObject_CallFunctionObjArgs(dirfunc, NULL); - Py_DECREF(dirfunc); - if (result == NULL) - return NULL; - - /* result must be a list */ - /* XXX(gbrandl): could also check if all items are strings */ - if (!PyList_Check(result)) { - PyErr_Format(PyExc_TypeError, - "__dir__() must return a list, not %.200s", - Py_TYPE(result)->tp_name); - Py_DECREF(result); - result = NULL; - } + /* use __dir__ */ + result = PyObject_CallFunctionObjArgs(dirfunc, NULL); + Py_DECREF(dirfunc); + if (result == NULL) + return NULL; + /* return sorted(result) */ + sorted = PySequence_List(result); + Py_DECREF(result); + if (sorted == NULL) + return NULL; + if (PyList_Sort(sorted)) { + Py_DECREF(sorted); + return NULL; } - - return result; + return sorted; } /* Implementation of dir() -- if obj is NULL, returns the names in the current @@ -1410,31 +1245,13 @@ _dir_object(PyObject *obj) PyObject * PyObject_Dir(PyObject *obj) { - PyObject * result; - - if (obj == NULL) - /* no object -- introspect the locals */ - result = _dir_locals(); - else - /* object -- introspect the object */ - result = _dir_object(obj); - - assert(result == NULL || PyList_Check(result)); - - if (result != NULL && PyList_Sort(result) != 0) { - /* sorting the list failed */ - Py_DECREF(result); - result = NULL; - } - - return result; + return (obj == NULL) ? _dir_locals() : _dir_object(obj); } /* -NoObject is usable as a non-NULL undefined value, used by the macro None. +None is a non-NULL undefined value. There is (and should be!) no way to create other objects of this type, so there is exactly one (which is indestructible, by the way). -(XXX This type and the type of NotImplemented below should be unified.) */ /* ARGSUSED */ @@ -1454,6 +1271,58 @@ none_dealloc(PyObject* ignore) Py_FatalError("deallocating None"); } +static PyObject * +none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { + PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); + return NULL; + } + Py_RETURN_NONE; +} + +static int +none_bool(PyObject *v) +{ + return 0; +} + +static PyNumberMethods none_as_number = { + 0, /* nb_add */ + 0, /* nb_subtract */ + 0, /* nb_multiply */ + 0, /* nb_remainder */ + 0, /* nb_divmod */ + 0, /* nb_power */ + 0, /* nb_negative */ + 0, /* nb_positive */ + 0, /* nb_absolute */ + (inquiry)none_bool, /* nb_bool */ + 0, /* nb_invert */ + 0, /* nb_lshift */ + 0, /* nb_rshift */ + 0, /* nb_and */ + 0, /* nb_xor */ + 0, /* nb_or */ + 0, /* nb_int */ + 0, /* nb_reserved */ + 0, /* nb_float */ + 0, /* nb_inplace_add */ + 0, /* nb_inplace_subtract */ + 0, /* nb_inplace_multiply */ + 0, /* nb_inplace_remainder */ + 0, /* nb_inplace_power */ + 0, /* nb_inplace_lshift */ + 0, /* nb_inplace_rshift */ + 0, /* nb_inplace_and */ + 0, /* nb_inplace_xor */ + 0, /* nb_inplace_or */ + 0, /* nb_floor_divide */ + 0, /* nb_true_divide */ + 0, /* nb_inplace_floor_divide */ + 0, /* nb_inplace_true_divide */ + 0, /* nb_index */ +}; static PyTypeObject PyNone_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) @@ -1466,10 +1335,34 @@ static PyTypeObject PyNone_Type = { 0, /*tp_setattr*/ 0, /*tp_reserved*/ none_repr, /*tp_repr*/ - 0, /*tp_as_number*/ + &none_as_number, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ + 0, /*tp_call */ + 0, /*tp_str */ + 0, /*tp_getattro */ + 0, /*tp_setattro */ + 0, /*tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /*tp_flags */ + 0, /*tp_doc */ + 0, /*tp_traverse */ + 0, /*tp_clear */ + 0, /*tp_richcompare */ + 0, /*tp_weaklistoffset */ + 0, /*tp_iter */ + 0, /*tp_iternext */ + 0, /*tp_methods */ + 0, /*tp_members */ + 0, /*tp_getset */ + 0, /*tp_base */ + 0, /*tp_dict */ + 0, /*tp_descr_get */ + 0, /*tp_descr_set */ + 0, /*tp_dictoffset */ + 0, /*tp_init */ + 0, /*tp_alloc */ + none_new, /*tp_new */ }; PyObject _Py_NoneStruct = { @@ -1486,6 +1379,16 @@ NotImplemented_repr(PyObject *op) return PyUnicode_FromString("NotImplemented"); } +static PyObject * +notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { + PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); + return NULL; + } + Py_RETURN_NOTIMPLEMENTED; +} + static PyTypeObject PyNotImplemented_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "NotImplementedType", @@ -1501,6 +1404,30 @@ static PyTypeObject PyNotImplemented_Type = { 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ + 0, /*tp_call */ + 0, /*tp_str */ + 0, /*tp_getattro */ + 0, /*tp_setattro */ + 0, /*tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /*tp_flags */ + 0, /*tp_doc */ + 0, /*tp_traverse */ + 0, /*tp_clear */ + 0, /*tp_richcompare */ + 0, /*tp_weaklistoffset */ + 0, /*tp_iter */ + 0, /*tp_iternext */ + 0, /*tp_methods */ + 0, /*tp_members */ + 0, /*tp_getset */ + 0, /*tp_base */ + 0, /*tp_dict */ + 0, /*tp_descr_get */ + 0, /*tp_descr_set */ + 0, /*tp_dictoffset */ + 0, /*tp_init */ + 0, /*tp_alloc */ + notimplemented_new, /*tp_new */ }; PyObject _Py_NotImplementedStruct = { diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 3916262..9a7c7e7 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -2,6 +2,10 @@ #ifdef WITH_PYMALLOC +#ifdef HAVE_MALLOPT_MMAP_THRESHOLD + #include <malloc.h> +#endif + #ifdef WITH_VALGRIND #include <valgrind/valgrind.h> @@ -75,7 +79,8 @@ static int running_on_valgrind = -1; * Allocation strategy abstract: * * For small requests, the allocator sub-allocates <Big> blocks of memory. - * Requests greater than 256 bytes are routed to the system's allocator. + * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the + * system's allocator. * * Small requests are grouped in size classes spaced 8 bytes apart, due * to the required valid alignment of the returned address. Requests of @@ -107,10 +112,11 @@ static int running_on_valgrind = -1; * 57-64 64 7 * 65-72 72 8 * ... ... ... - * 241-248 248 30 - * 249-256 256 31 + * 497-504 504 62 + * 505-512 512 63 * - * 0, 257 and up: routed to the underlying allocator. + * 0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying + * allocator. */ /*==========================================================================*/ @@ -139,14 +145,17 @@ static int running_on_valgrind = -1; * small enough in order to use preallocated memory pools. You can tune * this value according to your application behaviour and memory needs. * + * Note: a size threshold of 512 guarantees that newly created dictionaries + * will be allocated from preallocated memory pools on 64-bit. + * * The following invariants must hold: - * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256 + * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 512 * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT * * Although not required, for better performance and space efficiency, * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. */ -#define SMALL_REQUEST_THRESHOLD 256 +#define SMALL_REQUEST_THRESHOLD 512 #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) /* @@ -440,6 +449,9 @@ static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = { , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) #if NB_SMALL_SIZE_CLASSES > 56 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) +#if NB_SMALL_SIZE_CLASSES > 64 +#error "NB_SMALL_SIZE_CLASSES should be less than 64" +#endif /* NB_SMALL_SIZE_CLASSES > 64 */ #endif /* NB_SMALL_SIZE_CLASSES > 56 */ #endif /* NB_SMALL_SIZE_CLASSES > 48 */ #endif /* NB_SMALL_SIZE_CLASSES > 40 */ @@ -545,6 +557,11 @@ new_arena(void) if (numarenas > PY_SIZE_MAX / sizeof(*arenas)) return NULL; /* overflow */ #endif +#ifdef HAVE_MALLOPT_MMAP_THRESHOLD + /* Ensure arenas are allocated by mmap to avoid heap fragmentation. */ + if (numarenas == INITIAL_ARENA_OBJECTS) + mallopt(M_MMAP_THRESHOLD, ARENA_SIZE); +#endif nbytes = numarenas * sizeof(*arenas); arenaobj = (struct arena_object *)realloc(arenas, nbytes); if (arenaobj == NULL) @@ -1775,7 +1792,6 @@ _PyObject_DebugMallocStats(void) * will be living in full pools -- would be a shame to miss them. */ for (i = 0; i < maxarenas; ++i) { - uint poolsinarena; uint j; uptr base = arenas[i].address; @@ -1784,7 +1800,6 @@ _PyObject_DebugMallocStats(void) continue; narenas += 1; - poolsinarena = arenas[i].ntotalpools; numfreepools += arenas[i].nfreepools; /* round up to pool alignment */ diff --git a/Objects/setobject.c b/Objects/setobject.c index 48edad8..34d8204 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -77,7 +77,7 @@ NULL if the rich comparison returns an error. static setentry * set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash) { - register Py_ssize_t i; + register size_t i; register size_t perturb; register setentry *freeslot; register size_t mask = so->mask; @@ -86,7 +86,7 @@ set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash) register int cmp; PyObject *startkey; - i = hash & mask; + i = (size_t)hash & mask; entry = &table[i]; if (entry->key == NULL || entry->key == key) return entry; @@ -159,7 +159,7 @@ set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash) static setentry * set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash) { - register Py_ssize_t i; + register size_t i; register size_t perturb; register setentry *freeslot; register size_t mask = so->mask; @@ -174,7 +174,7 @@ set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash) so->lookup = set_lookkey; return set_lookkey(so, key, hash); } - i = hash & mask; + i = (size_t)hash & mask; entry = &table[i]; if (entry->key == NULL || entry->key == key) return entry; @@ -256,7 +256,7 @@ set_insert_clean(register PySetObject *so, PyObject *key, Py_hash_t hash) setentry *table = so->table; register setentry *entry; - i = hash & mask; + i = (size_t)hash & mask; entry = &table[i]; for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) { i = (i << 2) + i + perturb + 1; @@ -386,7 +386,7 @@ set_add_key(register PySetObject *so, PyObject *key) register Py_ssize_t n_used; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return -1; @@ -434,7 +434,7 @@ set_discard_key(PySetObject *so, PyObject *key) assert (PyAnySet_Check(so)); if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return -1; @@ -579,11 +579,8 @@ set_dealloc(PySetObject *so) static PyObject * set_repr(PySetObject *so) { - PyObject *keys, *result=NULL; - Py_UNICODE *u; + PyObject *result=NULL, *keys, *listrepr, *tmp; int status = Py_ReprEnter((PyObject*)so); - PyObject *listrepr; - Py_ssize_t newsize; if (status != 0) { if (status < 0) @@ -601,29 +598,24 @@ set_repr(PySetObject *so) if (keys == NULL) goto done; + /* repr(keys)[1:-1] */ listrepr = PyObject_Repr(keys); Py_DECREF(keys); if (listrepr == NULL) goto done; - newsize = PyUnicode_GET_SIZE(listrepr); - result = PyUnicode_FromUnicode(NULL, newsize); - if (result) { - u = PyUnicode_AS_UNICODE(result); - *u++ = '{'; - /* Omit the brackets from the listrepr */ - Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1, - PyUnicode_GET_SIZE(listrepr)-2); - u += newsize-2; - *u++ = '}'; - } + tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1); + Py_DECREF(listrepr); + if (tmp == NULL) + goto done; + listrepr = tmp; + + if (Py_TYPE(so) != &PySet_Type) + result = PyUnicode_FromFormat("%s({%U})", + Py_TYPE(so)->tp_name, + listrepr); + else + result = PyUnicode_FromFormat("{%U}", listrepr); Py_DECREF(listrepr); - if (Py_TYPE(so) != &PySet_Type) { - PyObject *tmp = PyUnicode_FromFormat("%s(%U)", - Py_TYPE(so)->tp_name, - result); - Py_DECREF(result); - result = tmp; - } done: Py_ReprLeave((PyObject*)so); return result; @@ -682,7 +674,7 @@ set_contains_key(PySetObject *so, PyObject *key) setentry *entry; if (!PyUnicode_CheckExact(key) || - (hash = ((PyUnicodeObject *) key)->hash) == -1) { + (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return -1; @@ -768,14 +760,14 @@ static Py_hash_t frozenset_hash(PyObject *self) { PySetObject *so = (PySetObject *)self; - Py_hash_t h, hash = 1927868237L; + Py_uhash_t h, hash = 1927868237U; setentry *entry; Py_ssize_t pos = 0; if (so->hash != -1) return so->hash; - hash *= PySet_GET_SIZE(self) + 1; + hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1; while (set_next(so, &pos, &entry)) { /* Work to increase the bit dispersion for closely spaced hash values. The is important because some use cases have many @@ -783,11 +775,11 @@ frozenset_hash(PyObject *self) hashes so that many distinct combinations collapse to only a handful of distinct hash values. */ h = entry->hash; - hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; + hash ^= (h ^ (h << 16) ^ 89869747U) * 3644798167U; } - hash = hash * 69069L + 907133923L; + hash = hash * 69069U + 907133923U; if (hash == -1) - hash = 590923713L; + hash = 590923713U; so->hash = hash; return hash; } @@ -1210,10 +1202,8 @@ set_or(PySetObject *so, PyObject *other) { PySetObject *result; - if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; result = (PySetObject *)set_copy(so); if (result == NULL) @@ -1230,10 +1220,9 @@ set_or(PySetObject *so, PyObject *other) static PyObject * set_ior(PySetObject *so, PyObject *other) { - if (!PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; + if (set_update_internal(so, other) == -1) return NULL; Py_INCREF(so); @@ -1383,10 +1372,8 @@ PyDoc_STRVAR(intersection_update_doc, static PyObject * set_and(PySetObject *so, PyObject *other) { - if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; return set_intersection(so, other); } @@ -1395,10 +1382,8 @@ set_iand(PySetObject *so, PyObject *other) { PyObject *result; - if (!PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; result = set_intersection_update(so, other); if (result == NULL) return NULL; @@ -1625,20 +1610,16 @@ PyDoc_STRVAR(difference_doc, static PyObject * set_sub(PySetObject *so, PyObject *other) { - if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; return set_difference(so, other); } static PyObject * set_isub(PySetObject *so, PyObject *other) { - if (!PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; if (set_difference_update_internal(so, other) == -1) return NULL; Py_INCREF(so); @@ -1736,10 +1717,8 @@ PyDoc_STRVAR(symmetric_difference_doc, static PyObject * set_xor(PySetObject *so, PyObject *other) { - if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; return set_symmetric_difference(so, other); } @@ -1748,10 +1727,8 @@ set_ixor(PySetObject *so, PyObject *other) { PyObject *result; - if (!PyAnySet_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyAnySet_Check(other)) + Py_RETURN_NOTIMPLEMENTED; result = set_symmetric_difference_update(so, other); if (result == NULL) return NULL; @@ -1813,10 +1790,9 @@ set_richcompare(PySetObject *v, PyObject *w, int op) { PyObject *r1, *r2; - if(!PyAnySet_Check(w)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if(!PyAnySet_Check(w)) + Py_RETURN_NOTIMPLEMENTED; + switch (op) { case Py_EQ: if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w)) @@ -1846,8 +1822,7 @@ set_richcompare(PySetObject *v, PyObject *w, int op) Py_RETURN_FALSE; return set_issuperset(v, w); } - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } static PyObject * diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 51c53a8..92464ab 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -17,6 +17,17 @@ this type and there is exactly one in existence. #include "structmember.h" static PyObject * +ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { + PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); + return NULL; + } + Py_INCREF(Py_Ellipsis); + return Py_Ellipsis; +} + +static PyObject * ellipsis_repr(PyObject *op) { return PyUnicode_FromString("Ellipsis"); @@ -43,6 +54,24 @@ PyTypeObject PyEllipsis_Type = { 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + ellipsis_new, /* tp_new */ }; PyObject _Py_EllipsisObject = { @@ -297,10 +326,8 @@ slice_richcompare(PyObject *v, PyObject *w, int op) PyObject *t2; PyObject *res; - if (!PySlice_Check(v) || !PySlice_Check(w)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PySlice_Check(v) || !PySlice_Check(w)) + Py_RETURN_NOTIMPLEMENTED; if (v == w) { /* XXX Do we really need this shortcut? diff --git a/Objects/stringlib/asciilib.h b/Objects/stringlib/asciilib.h new file mode 100644 index 0000000..935a9c7 --- /dev/null +++ b/Objects/stringlib/asciilib.h @@ -0,0 +1,34 @@ +/* this is sort of a hack. there's at least one place (formatting + floats) where some stringlib code takes a different path if it's + compiled as unicode. */ +#define STRINGLIB_IS_UNICODE 1 + +#define FASTSEARCH asciilib_fastsearch +#define STRINGLIB(F) asciilib_##F +#define STRINGLIB_OBJECT PyUnicodeObject +#define STRINGLIB_CHAR Py_UCS1 +#define STRINGLIB_TYPE_NAME "unicode" +#define STRINGLIB_PARSE_CODE "U" +#define STRINGLIB_EMPTY unicode_empty +#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE +#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK +#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL +#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL +#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER +#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER +#define STRINGLIB_FILL Py_UNICODE_FILL +#define STRINGLIB_STR PyUnicode_1BYTE_DATA +#define STRINGLIB_LEN PyUnicode_GET_LENGTH +#define STRINGLIB_NEW unicode_fromascii +#define STRINGLIB_RESIZE not_supported +#define STRINGLIB_CHECK PyUnicode_Check +#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact +#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping +#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale + +#define STRINGLIB_TOSTR PyObject_Str +#define STRINGLIB_TOASCII PyObject_ASCII + +#define _Py_InsertThousandsGrouping _PyUnicode_ascii_InsertThousandsGrouping +#define _Py_InsertThousandsGroupingLocale _PyUnicode_ascii_InsertThousandsGroupingLocale + diff --git a/Objects/stringlib/count.h b/Objects/stringlib/count.h index de34f96..f48500b 100644 --- a/Objects/stringlib/count.h +++ b/Objects/stringlib/count.h @@ -1,14 +1,11 @@ /* stringlib: count implementation */ -#ifndef STRINGLIB_COUNT_H -#define STRINGLIB_COUNT_H - #ifndef STRINGLIB_FASTSEARCH_H #error must include "stringlib/fastsearch.h" before including this module #endif Py_LOCAL_INLINE(Py_ssize_t) -stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len, +STRINGLIB(count)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t maxcount) { @@ -19,7 +16,7 @@ stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len, if (sub_len == 0) return (str_len < maxcount) ? str_len + 1 : maxcount; - count = fastsearch(str, str_len, sub, sub_len, maxcount, FAST_COUNT); + count = FASTSEARCH(str, str_len, sub, sub_len, maxcount, FAST_COUNT); if (count < 0) return 0; /* no match */ @@ -27,4 +24,4 @@ stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len, return count; } -#endif + diff --git a/Objects/stringlib/eq.h b/Objects/stringlib/eq.h index 3e7f5e8..8e79a43 100644 --- a/Objects/stringlib/eq.h +++ b/Objects/stringlib/eq.h @@ -9,13 +9,26 @@ unicode_eq(PyObject *aa, PyObject *bb) register PyUnicodeObject *a = (PyUnicodeObject *)aa; register PyUnicodeObject *b = (PyUnicodeObject *)bb; - if (a->length != b->length) + if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) { + assert(0 && "unicode_eq ready fail"); return 0; - if (a->length == 0) + } + + if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b)) + return 0; + if (PyUnicode_GET_LENGTH(a) == 0) return 1; - if (a->str[0] != b->str[0]) + if (PyUnicode_KIND(a) != PyUnicode_KIND(b)) + return 0; + /* Just comparing the first byte is enough to see if a and b differ. + * If they are 2 byte or 4 byte character most differences will happen in + * the lower bytes anyways. + */ + if (PyUnicode_1BYTE_DATA(a)[0] != PyUnicode_1BYTE_DATA(b)[0]) return 0; - if (a->length == 1) + if (PyUnicode_KIND(a) == PyUnicode_1BYTE_KIND && + PyUnicode_GET_LENGTH(a) == 1) return 1; - return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0; + return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b), + PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0; } diff --git a/Objects/stringlib/fastsearch.h b/Objects/stringlib/fastsearch.h index e231c58..d35cba3 100644 --- a/Objects/stringlib/fastsearch.h +++ b/Objects/stringlib/fastsearch.h @@ -1,6 +1,5 @@ /* stringlib: fastsearch implementation */ -#ifndef STRINGLIB_FASTSEARCH_H #define STRINGLIB_FASTSEARCH_H /* fast search/count implementation, based on a mix between boyer- @@ -34,7 +33,7 @@ ((mask & (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1))))) Py_LOCAL_INLINE(Py_ssize_t) -fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n, +FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n, const STRINGLIB_CHAR* p, Py_ssize_t m, Py_ssize_t maxcount, int mode) { @@ -157,4 +156,3 @@ fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n, return count; } -#endif diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h index ce615dc..7cce156 100644 --- a/Objects/stringlib/find.h +++ b/Objects/stringlib/find.h @@ -1,14 +1,11 @@ /* stringlib: find/index implementation */ -#ifndef STRINGLIB_FIND_H -#define STRINGLIB_FIND_H - #ifndef STRINGLIB_FASTSEARCH_H #error must include "stringlib/fastsearch.h" before including this module #endif Py_LOCAL_INLINE(Py_ssize_t) -stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, +STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t offset) { @@ -19,7 +16,7 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, if (sub_len == 0) return offset; - pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_SEARCH); + pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH); if (pos >= 0) pos += offset; @@ -28,7 +25,7 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, } Py_LOCAL_INLINE(Py_ssize_t) -stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, +STRINGLIB(rfind)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t offset) { @@ -39,7 +36,7 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, if (sub_len == 0) return str_len + offset; - pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_RSEARCH); + pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH); if (pos >= 0) pos += offset; @@ -63,29 +60,29 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, } Py_LOCAL_INLINE(Py_ssize_t) -stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len, +STRINGLIB(find_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t start, Py_ssize_t end) { ADJUST_INDICES(start, end, str_len); - return stringlib_find(str + start, end - start, sub, sub_len, start); + return STRINGLIB(find)(str + start, end - start, sub, sub_len, start); } Py_LOCAL_INLINE(Py_ssize_t) -stringlib_rfind_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len, +STRINGLIB(rfind_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t start, Py_ssize_t end) { ADJUST_INDICES(start, end, str_len); - return stringlib_rfind(str + start, end - start, sub, sub_len, start); + return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start); } #ifdef STRINGLIB_WANT_CONTAINS_OBJ Py_LOCAL_INLINE(int) -stringlib_contains_obj(PyObject* str, PyObject* sub) +STRINGLIB(contains_obj)(PyObject* str, PyObject* sub) { - return stringlib_find( + return STRINGLIB(find)( STRINGLIB_STR(str), STRINGLIB_LEN(str), STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0 ) != -1; @@ -105,7 +102,7 @@ is ok. #define FORMAT_BUFFER_SIZE 50 Py_LOCAL_INLINE(int) -stringlib_parse_args_finds(const char * function_name, PyObject *args, +STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args, PyObject **subobj, Py_ssize_t *start, Py_ssize_t *end) { @@ -153,13 +150,13 @@ after finishing using the substring, must DECREF it). */ Py_LOCAL_INLINE(int) -stringlib_parse_args_finds_unicode(const char * function_name, PyObject *args, +STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args, PyUnicodeObject **substring, Py_ssize_t *start, Py_ssize_t *end) { PyObject *tmp_substring; - if(stringlib_parse_args_finds(function_name, args, &tmp_substring, + if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring, start, end)) { tmp_substring = PyUnicode_FromObject(tmp_substring); if (!tmp_substring) @@ -171,5 +168,3 @@ stringlib_parse_args_finds_unicode(const char * function_name, PyObject *args, } #endif /* STRINGLIB_IS_UNICODE */ - -#endif /* STRINGLIB_FIND_H */ diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h deleted file mode 100644 index 4fdc62d..0000000 --- a/Objects/stringlib/formatter.h +++ /dev/null @@ -1,1518 +0,0 @@ -/* implements the string, long, and float formatters. that is, - string.__format__, etc. */ - -#include <locale.h> - -/* Before including this, you must include either: - stringlib/unicodedefs.h - stringlib/stringdefs.h - - Also, you should define the names: - FORMAT_STRING - FORMAT_LONG - FORMAT_FLOAT - FORMAT_COMPLEX - to be whatever you want the public names of these functions to - be. These are the only non-static functions defined here. -*/ - -/* Raises an exception about an unknown presentation type for this - * type. */ - -static void -unknown_presentation_type(STRINGLIB_CHAR presentation_type, - const char* type_name) -{ -#if STRINGLIB_IS_UNICODE - /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range, - hence the two cases. If it is char, gcc complains that the - condition below is always true, hence the ifdef. */ - if (presentation_type > 32 && presentation_type < 128) -#endif - PyErr_Format(PyExc_ValueError, - "Unknown format code '%c' " - "for object of type '%.200s'", - (char)presentation_type, - type_name); -#if STRINGLIB_IS_UNICODE - else - PyErr_Format(PyExc_ValueError, - "Unknown format code '\\x%x' " - "for object of type '%.200s'", - (unsigned int)presentation_type, - type_name); -#endif -} - -static void -invalid_comma_type(STRINGLIB_CHAR presentation_type) -{ -#if STRINGLIB_IS_UNICODE - /* See comment in unknown_presentation_type */ - if (presentation_type > 32 && presentation_type < 128) -#endif - PyErr_Format(PyExc_ValueError, - "Cannot specify ',' with '%c'.", - (char)presentation_type); -#if STRINGLIB_IS_UNICODE - else - PyErr_Format(PyExc_ValueError, - "Cannot specify ',' with '\\x%x'.", - (unsigned int)presentation_type); -#endif -} - -/* - get_integer consumes 0 or more decimal digit characters from an - input string, updates *result with the corresponding positive - integer, and returns the number of digits consumed. - - returns -1 on error. -*/ -static int -get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end, - Py_ssize_t *result) -{ - Py_ssize_t accumulator, digitval, oldaccumulator; - int numdigits; - accumulator = numdigits = 0; - for (;;(*ptr)++, numdigits++) { - if (*ptr >= end) - break; - digitval = STRINGLIB_TODECIMAL(**ptr); - if (digitval < 0) - break; - /* - This trick was copied from old Unicode format code. It's cute, - but would really suck on an old machine with a slow divide - implementation. Fortunately, in the normal case we do not - expect too many digits. - */ - oldaccumulator = accumulator; - accumulator *= 10; - if ((accumulator+10)/10 != oldaccumulator+1) { - PyErr_Format(PyExc_ValueError, - "Too many decimal digits in format string"); - return -1; - } - accumulator += digitval; - } - *result = accumulator; - return numdigits; -} - -/************************************************************************/ -/*********** standard format specifier parsing **************************/ -/************************************************************************/ - -/* returns true if this character is a specifier alignment token */ -Py_LOCAL_INLINE(int) -is_alignment_token(STRINGLIB_CHAR c) -{ - switch (c) { - case '<': case '>': case '=': case '^': - return 1; - default: - return 0; - } -} - -/* returns true if this character is a sign element */ -Py_LOCAL_INLINE(int) -is_sign_element(STRINGLIB_CHAR c) -{ - switch (c) { - case ' ': case '+': case '-': - return 1; - default: - return 0; - } -} - - -typedef struct { - STRINGLIB_CHAR fill_char; - STRINGLIB_CHAR align; - int alternate; - STRINGLIB_CHAR sign; - Py_ssize_t width; - int thousands_separators; - Py_ssize_t precision; - STRINGLIB_CHAR type; -} InternalFormatSpec; - - -#if 0 -/* Occassionally useful for debugging. Should normally be commented out. */ -static void -DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format) -{ - printf("internal format spec: fill_char %d\n", format->fill_char); - printf("internal format spec: align %d\n", format->align); - printf("internal format spec: alternate %d\n", format->alternate); - printf("internal format spec: sign %d\n", format->sign); - printf("internal format spec: width %zd\n", format->width); - printf("internal format spec: thousands_separators %d\n", - format->thousands_separators); - printf("internal format spec: precision %zd\n", format->precision); - printf("internal format spec: type %c\n", format->type); - printf("\n"); -} -#endif - - -/* - ptr points to the start of the format_spec, end points just past its end. - fills in format with the parsed information. - returns 1 on success, 0 on failure. - if failure, sets the exception -*/ -static int -parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len, - InternalFormatSpec *format, - char default_type, - char default_align) -{ - STRINGLIB_CHAR *ptr = format_spec; - STRINGLIB_CHAR *end = format_spec + format_spec_len; - - /* end-ptr is used throughout this code to specify the length of - the input string */ - - Py_ssize_t consumed; - int align_specified = 0; - - format->fill_char = '\0'; - format->align = default_align; - format->alternate = 0; - format->sign = '\0'; - format->width = -1; - format->thousands_separators = 0; - format->precision = -1; - format->type = default_type; - - /* If the second char is an alignment token, - then parse the fill char */ - if (end-ptr >= 2 && is_alignment_token(ptr[1])) { - format->align = ptr[1]; - format->fill_char = ptr[0]; - align_specified = 1; - ptr += 2; - } - else if (end-ptr >= 1 && is_alignment_token(ptr[0])) { - format->align = ptr[0]; - align_specified = 1; - ++ptr; - } - - /* Parse the various sign options */ - if (end-ptr >= 1 && is_sign_element(ptr[0])) { - format->sign = ptr[0]; - ++ptr; - } - - /* If the next character is #, we're in alternate mode. This only - applies to integers. */ - if (end-ptr >= 1 && ptr[0] == '#') { - format->alternate = 1; - ++ptr; - } - - /* The special case for 0-padding (backwards compat) */ - if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') { - format->fill_char = '0'; - if (!align_specified) { - format->align = '='; - } - ++ptr; - } - - consumed = get_integer(&ptr, end, &format->width); - if (consumed == -1) - /* Overflow error. Exception already set. */ - return 0; - - /* If consumed is 0, we didn't consume any characters for the - width. In that case, reset the width to -1, because - get_integer() will have set it to zero. -1 is how we record - that the width wasn't specified. */ - if (consumed == 0) - format->width = -1; - - /* Comma signifies add thousands separators */ - if (end-ptr && ptr[0] == ',') { - format->thousands_separators = 1; - ++ptr; - } - - /* Parse field precision */ - if (end-ptr && ptr[0] == '.') { - ++ptr; - - consumed = get_integer(&ptr, end, &format->precision); - if (consumed == -1) - /* Overflow error. Exception already set. */ - return 0; - - /* Not having a precision after a dot is an error. */ - if (consumed == 0) { - PyErr_Format(PyExc_ValueError, - "Format specifier missing precision"); - return 0; - } - - } - - /* Finally, parse the type field. */ - - if (end-ptr > 1) { - /* More than one char remain, invalid conversion spec. */ - PyErr_Format(PyExc_ValueError, "Invalid conversion specification"); - return 0; - } - - if (end-ptr == 1) { - format->type = ptr[0]; - ++ptr; - } - - /* Do as much validating as we can, just by looking at the format - specifier. Do not take into account what type of formatting - we're doing (int, float, string). */ - - if (format->thousands_separators) { - switch (format->type) { - case 'd': - case 'e': - case 'f': - case 'g': - case 'E': - case 'G': - case '%': - case 'F': - case '\0': - /* These are allowed. See PEP 378.*/ - break; - default: - invalid_comma_type(format->type); - return 0; - } - } - - return 1; -} - -/* Calculate the padding needed. */ -static void -calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align, - Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding, - Py_ssize_t *n_total) -{ - if (width >= 0) { - if (nchars > width) - *n_total = nchars; - else - *n_total = width; - } - else { - /* not specified, use all of the chars and no more */ - *n_total = nchars; - } - - /* Figure out how much leading space we need, based on the - aligning */ - if (align == '>') - *n_lpadding = *n_total - nchars; - else if (align == '^') - *n_lpadding = (*n_total - nchars) / 2; - else if (align == '<' || align == '=') - *n_lpadding = 0; - else { - /* We should never have an unspecified alignment. */ - *n_lpadding = 0; - assert(0); - } - - *n_rpadding = *n_total - nchars - *n_lpadding; -} - -/* Do the padding, and return a pointer to where the caller-supplied - content goes. */ -static STRINGLIB_CHAR * -fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char, - Py_ssize_t n_lpadding, Py_ssize_t n_rpadding) -{ - /* Pad on left. */ - if (n_lpadding) - STRINGLIB_FILL(p, fill_char, n_lpadding); - - /* Pad on right. */ - if (n_rpadding) - STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding); - - /* Pointer to the user content. */ - return p + n_lpadding; -} - -#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX -/************************************************************************/ -/*********** common routines for numeric formatting *********************/ -/************************************************************************/ - -/* Locale type codes. */ -#define LT_CURRENT_LOCALE 0 -#define LT_DEFAULT_LOCALE 1 -#define LT_NO_LOCALE 2 - -/* Locale info needed for formatting integers and the part of floats - before and including the decimal. Note that locales only support - 8-bit chars, not unicode. */ -typedef struct { - char *decimal_point; - char *thousands_sep; - char *grouping; -} LocaleInfo; - -/* describes the layout for an integer, see the comment in - calc_number_widths() for details */ -typedef struct { - Py_ssize_t n_lpadding; - Py_ssize_t n_prefix; - Py_ssize_t n_spadding; - Py_ssize_t n_rpadding; - char sign; - Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */ - Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including - any grouping chars. */ - Py_ssize_t n_decimal; /* 0 if only an integer */ - Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part, - excluding the decimal itself, if - present. */ - - /* These 2 are not the widths of fields, but are needed by - STRINGLIB_GROUPING. */ - Py_ssize_t n_digits; /* The number of digits before a decimal - or exponent. */ - Py_ssize_t n_min_width; /* The min_width we used when we computed - the n_grouped_digits width. */ -} NumberFieldWidths; - - -/* Given a number of the form: - digits[remainder] - where ptr points to the start and end points to the end, find where - the integer part ends. This could be a decimal, an exponent, both, - or neither. - If a decimal point is present, set *has_decimal and increment - remainder beyond it. - Results are undefined (but shouldn't crash) for improperly - formatted strings. -*/ -static void -parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len, - Py_ssize_t *n_remainder, int *has_decimal) -{ - STRINGLIB_CHAR *end = ptr + len; - STRINGLIB_CHAR *remainder; - - while (ptr<end && isdigit(*ptr)) - ++ptr; - remainder = ptr; - - /* Does remainder start with a decimal point? */ - *has_decimal = ptr<end && *remainder == '.'; - - /* Skip the decimal point. */ - if (*has_decimal) - remainder++; - - *n_remainder = end - remainder; -} - -/* not all fields of format are used. for example, precision is - unused. should this take discrete params in order to be more clear - about what it does? or is passing a single format parameter easier - and more efficient enough to justify a little obfuscation? */ -static Py_ssize_t -calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix, - STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number, - Py_ssize_t n_number, Py_ssize_t n_remainder, - int has_decimal, const LocaleInfo *locale, - const InternalFormatSpec *format) -{ - Py_ssize_t n_non_digit_non_padding; - Py_ssize_t n_padding; - - spec->n_digits = n_number - n_remainder - (has_decimal?1:0); - spec->n_lpadding = 0; - spec->n_prefix = n_prefix; - spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0; - spec->n_remainder = n_remainder; - spec->n_spadding = 0; - spec->n_rpadding = 0; - spec->sign = '\0'; - spec->n_sign = 0; - - /* the output will look like: - | | - | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> | - | | - - sign is computed from format->sign and the actual - sign of the number - - prefix is given (it's for the '0x' prefix) - - digits is already known - - the total width is either given, or computed from the - actual digits - - only one of lpadding, spadding, and rpadding can be non-zero, - and it's calculated from the width and other fields - */ - - /* compute the various parts we're going to write */ - switch (format->sign) { - case '+': - /* always put a + or - */ - spec->n_sign = 1; - spec->sign = (sign_char == '-' ? '-' : '+'); - break; - case ' ': - spec->n_sign = 1; - spec->sign = (sign_char == '-' ? '-' : ' '); - break; - default: - /* Not specified, or the default (-) */ - if (sign_char == '-') { - spec->n_sign = 1; - spec->sign = '-'; - } - } - - /* The number of chars used for non-digits and non-padding. */ - n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal + - spec->n_remainder; - - /* min_width can go negative, that's okay. format->width == -1 means - we don't care. */ - if (format->fill_char == '0' && format->align == '=') - spec->n_min_width = format->width - n_non_digit_non_padding; - else - spec->n_min_width = 0; - - if (spec->n_digits == 0) - /* This case only occurs when using 'c' formatting, we need - to special case it because the grouping code always wants - to have at least one character. */ - spec->n_grouped_digits = 0; - else - spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL, - spec->n_digits, - spec->n_min_width, - locale->grouping, - locale->thousands_sep); - - /* Given the desired width and the total of digit and non-digit - space we consume, see if we need any padding. format->width can - be negative (meaning no padding), but this code still works in - that case. */ - n_padding = format->width - - (n_non_digit_non_padding + spec->n_grouped_digits); - if (n_padding > 0) { - /* Some padding is needed. Determine if it's left, space, or right. */ - switch (format->align) { - case '<': - spec->n_rpadding = n_padding; - break; - case '^': - spec->n_lpadding = n_padding / 2; - spec->n_rpadding = n_padding - spec->n_lpadding; - break; - case '=': - spec->n_spadding = n_padding; - break; - case '>': - spec->n_lpadding = n_padding; - break; - default: - /* Shouldn't get here, but treat it as '>' */ - spec->n_lpadding = n_padding; - assert(0); - break; - } - } - return spec->n_lpadding + spec->n_sign + spec->n_prefix + - spec->n_spadding + spec->n_grouped_digits + spec->n_decimal + - spec->n_remainder + spec->n_rpadding; -} - -/* Fill in the digit parts of a numbers's string representation, - as determined in calc_number_widths(). - No error checking, since we know the buffer is the correct size. */ -static void -fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec, - STRINGLIB_CHAR *digits, Py_ssize_t n_digits, - STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char, - LocaleInfo *locale, int toupper) -{ - /* Used to keep track of digits, decimal, and remainder. */ - STRINGLIB_CHAR *p = digits; - -#ifndef NDEBUG - Py_ssize_t r; -#endif - - if (spec->n_lpadding) { - STRINGLIB_FILL(buf, fill_char, spec->n_lpadding); - buf += spec->n_lpadding; - } - if (spec->n_sign == 1) { - *buf++ = spec->sign; - } - if (spec->n_prefix) { - memmove(buf, - prefix, - spec->n_prefix * sizeof(STRINGLIB_CHAR)); - if (toupper) { - Py_ssize_t t; - for (t = 0; t < spec->n_prefix; ++t) - buf[t] = STRINGLIB_TOUPPER(buf[t]); - } - buf += spec->n_prefix; - } - if (spec->n_spadding) { - STRINGLIB_FILL(buf, fill_char, spec->n_spadding); - buf += spec->n_spadding; - } - - /* Only for type 'c' special case, it has no digits. */ - if (spec->n_digits != 0) { - /* Fill the digits with InsertThousandsGrouping. */ -#ifndef NDEBUG - r = -#endif - STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits, - spec->n_digits, spec->n_min_width, - locale->grouping, locale->thousands_sep); -#ifndef NDEBUG - assert(r == spec->n_grouped_digits); -#endif - p += spec->n_digits; - } - if (toupper) { - Py_ssize_t t; - for (t = 0; t < spec->n_grouped_digits; ++t) - buf[t] = STRINGLIB_TOUPPER(buf[t]); - } - buf += spec->n_grouped_digits; - - if (spec->n_decimal) { - Py_ssize_t t; - for (t = 0; t < spec->n_decimal; ++t) - buf[t] = locale->decimal_point[t]; - buf += spec->n_decimal; - p += 1; - } - - if (spec->n_remainder) { - memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR)); - buf += spec->n_remainder; - p += spec->n_remainder; - } - - if (spec->n_rpadding) { - STRINGLIB_FILL(buf, fill_char, spec->n_rpadding); - buf += spec->n_rpadding; - } -} - -static char no_grouping[1] = {CHAR_MAX}; - -/* Find the decimal point character(s?), thousands_separator(s?), and - grouping description, either for the current locale if type is - LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or - none if LT_NO_LOCALE. */ -static void -get_locale_info(int type, LocaleInfo *locale_info) -{ - switch (type) { - case LT_CURRENT_LOCALE: { - struct lconv *locale_data = localeconv(); - locale_info->decimal_point = locale_data->decimal_point; - locale_info->thousands_sep = locale_data->thousands_sep; - locale_info->grouping = locale_data->grouping; - break; - } - case LT_DEFAULT_LOCALE: - locale_info->decimal_point = "."; - locale_info->thousands_sep = ","; - locale_info->grouping = "\3"; /* Group every 3 characters. The - (implicit) trailing 0 means repeat - infinitely. */ - break; - case LT_NO_LOCALE: - locale_info->decimal_point = "."; - locale_info->thousands_sep = ""; - locale_info->grouping = no_grouping; - break; - default: - assert(0); - } -} - -#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */ - -/************************************************************************/ -/*********** string formatting ******************************************/ -/************************************************************************/ - -static PyObject * -format_string_internal(PyObject *value, const InternalFormatSpec *format) -{ - Py_ssize_t lpad; - Py_ssize_t rpad; - Py_ssize_t total; - STRINGLIB_CHAR *p; - Py_ssize_t len = STRINGLIB_LEN(value); - PyObject *result = NULL; - - /* sign is not allowed on strings */ - if (format->sign != '\0') { - PyErr_SetString(PyExc_ValueError, - "Sign not allowed in string format specifier"); - goto done; - } - - /* alternate is not allowed on strings */ - if (format->alternate) { - PyErr_SetString(PyExc_ValueError, - "Alternate form (#) not allowed in string format " - "specifier"); - goto done; - } - - /* '=' alignment not allowed on strings */ - if (format->align == '=') { - PyErr_SetString(PyExc_ValueError, - "'=' alignment not allowed " - "in string format specifier"); - goto done; - } - - /* if precision is specified, output no more that format.precision - characters */ - if (format->precision >= 0 && len >= format->precision) { - len = format->precision; - } - - calc_padding(len, format->width, format->align, &lpad, &rpad, &total); - - /* allocate the resulting string */ - result = STRINGLIB_NEW(NULL, total); - if (result == NULL) - goto done; - - /* Write into that space. First the padding. */ - p = fill_padding(STRINGLIB_STR(result), len, - format->fill_char=='\0'?' ':format->fill_char, - lpad, rpad); - - /* Then the source string. */ - memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR)); - -done: - return result; -} - - -/************************************************************************/ -/*********** long formatting ********************************************/ -/************************************************************************/ - -#if defined FORMAT_LONG || defined FORMAT_INT -typedef PyObject* -(*IntOrLongToString)(PyObject *value, int base); - -static PyObject * -format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format, - IntOrLongToString tostring) -{ - PyObject *result = NULL; - PyObject *tmp = NULL; - STRINGLIB_CHAR *pnumeric_chars; - STRINGLIB_CHAR numeric_char; - STRINGLIB_CHAR sign_char = '\0'; - Py_ssize_t n_digits; /* count of digits need from the computed - string */ - Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which - produces non-digits */ - Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */ - Py_ssize_t n_total; - STRINGLIB_CHAR *prefix = NULL; - NumberFieldWidths spec; - long x; - - /* Locale settings, either from the actual locale or - from a hard-code pseudo-locale */ - LocaleInfo locale; - - /* no precision allowed on integers */ - if (format->precision != -1) { - PyErr_SetString(PyExc_ValueError, - "Precision not allowed in integer format specifier"); - goto done; - } - - /* special case for character formatting */ - if (format->type == 'c') { - /* error to specify a sign */ - if (format->sign != '\0') { - PyErr_SetString(PyExc_ValueError, - "Sign not allowed with integer" - " format specifier 'c'"); - goto done; - } - - /* taken from unicodeobject.c formatchar() */ - /* Integer input truncated to a character */ -/* XXX: won't work for int */ - x = PyLong_AsLong(value); - if (x == -1 && PyErr_Occurred()) - goto done; -#ifdef Py_UNICODE_WIDE - if (x < 0 || x > 0x10ffff) { - PyErr_SetString(PyExc_OverflowError, - "%c arg not in range(0x110000) " - "(wide Python build)"); - goto done; - } -#else - if (x < 0 || x > 0xffff) { - PyErr_SetString(PyExc_OverflowError, - "%c arg not in range(0x10000) " - "(narrow Python build)"); - goto done; - } -#endif - numeric_char = (STRINGLIB_CHAR)x; - pnumeric_chars = &numeric_char; - n_digits = 1; - - /* As a sort-of hack, we tell calc_number_widths that we only - have "remainder" characters. calc_number_widths thinks - these are characters that don't get formatted, only copied - into the output string. We do this for 'c' formatting, - because the characters are likely to be non-digits. */ - n_remainder = 1; - } - else { - int base; - int leading_chars_to_skip = 0; /* Number of characters added by - PyNumber_ToBase that we want to - skip over. */ - - /* Compute the base and how many characters will be added by - PyNumber_ToBase */ - switch (format->type) { - case 'b': - base = 2; - leading_chars_to_skip = 2; /* 0b */ - break; - case 'o': - base = 8; - leading_chars_to_skip = 2; /* 0o */ - break; - case 'x': - case 'X': - base = 16; - leading_chars_to_skip = 2; /* 0x */ - break; - default: /* shouldn't be needed, but stops a compiler warning */ - case 'd': - case 'n': - base = 10; - break; - } - - /* The number of prefix chars is the same as the leading - chars to skip */ - if (format->alternate) - n_prefix = leading_chars_to_skip; - - /* Do the hard part, converting to a string in a given base */ - tmp = tostring(value, base); - if (tmp == NULL) - goto done; - - pnumeric_chars = STRINGLIB_STR(tmp); - n_digits = STRINGLIB_LEN(tmp); - - prefix = pnumeric_chars; - - /* Remember not to modify what pnumeric_chars points to. it - might be interned. Only modify it after we copy it into a - newly allocated output buffer. */ - - /* Is a sign character present in the output? If so, remember it - and skip it */ - if (pnumeric_chars[0] == '-') { - sign_char = pnumeric_chars[0]; - ++prefix; - ++leading_chars_to_skip; - } - - /* Skip over the leading chars (0x, 0b, etc.) */ - n_digits -= leading_chars_to_skip; - pnumeric_chars += leading_chars_to_skip; - } - - /* Determine the grouping, separator, and decimal point, if any. */ - get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : - (format->thousands_separators ? - LT_DEFAULT_LOCALE : - LT_NO_LOCALE), - &locale); - - /* Calculate how much memory we'll need. */ - n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars, - n_digits, n_remainder, 0, &locale, format); - - /* Allocate the memory. */ - result = STRINGLIB_NEW(NULL, n_total); - if (!result) - goto done; - - /* Populate the memory. */ - fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits, - prefix, format->fill_char == '\0' ? ' ' : format->fill_char, - &locale, format->type == 'X'); - -done: - Py_XDECREF(tmp); - return result; -} -#endif /* defined FORMAT_LONG || defined FORMAT_INT */ - -/************************************************************************/ -/*********** float formatting *******************************************/ -/************************************************************************/ - -#ifdef FORMAT_FLOAT -#if STRINGLIB_IS_UNICODE -static void -strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len) -{ - Py_ssize_t i; - for (i = 0; i < len; ++i) - buffer[i] = (Py_UNICODE)charbuffer[i]; -} -#endif - -/* much of this is taken from unicodeobject.c */ -static PyObject * -format_float_internal(PyObject *value, - const InternalFormatSpec *format) -{ - char *buf = NULL; /* buffer returned from PyOS_double_to_string */ - Py_ssize_t n_digits; - Py_ssize_t n_remainder; - Py_ssize_t n_total; - int has_decimal; - double val; - Py_ssize_t precision = format->precision; - Py_ssize_t default_precision = 6; - STRINGLIB_CHAR type = format->type; - int add_pct = 0; - STRINGLIB_CHAR *p; - NumberFieldWidths spec; - int flags = 0; - PyObject *result = NULL; - STRINGLIB_CHAR sign_char = '\0'; - int float_type; /* Used to see if we have a nan, inf, or regular float. */ - -#if STRINGLIB_IS_UNICODE - Py_UNICODE *unicode_tmp = NULL; -#endif - - /* Locale settings, either from the actual locale or - from a hard-code pseudo-locale */ - LocaleInfo locale; - - if (format->alternate) - flags |= Py_DTSF_ALT; - - if (type == '\0') { - /* Omitted type specifier. Behaves in the same way as repr(x) - and str(x) if no precision is given, else like 'g', but with - at least one digit after the decimal point. */ - flags |= Py_DTSF_ADD_DOT_0; - type = 'r'; - default_precision = 0; - } - - if (type == 'n') - /* 'n' is the same as 'g', except for the locale used to - format the result. We take care of that later. */ - type = 'g'; - - val = PyFloat_AsDouble(value); - if (val == -1.0 && PyErr_Occurred()) - goto done; - - if (type == '%') { - type = 'f'; - val *= 100; - add_pct = 1; - } - - if (precision < 0) - precision = default_precision; - else if (type == 'r') - type = 'g'; - - /* Cast "type", because if we're in unicode we need to pass a - 8-bit char. This is safe, because we've restricted what "type" - can be. */ - buf = PyOS_double_to_string(val, (char)type, precision, flags, - &float_type); - if (buf == NULL) - goto done; - n_digits = strlen(buf); - - if (add_pct) { - /* We know that buf has a trailing zero (since we just called - strlen() on it), and we don't use that fact any more. So we - can just write over the trailing zero. */ - buf[n_digits] = '%'; - n_digits += 1; - } - - /* Since there is no unicode version of PyOS_double_to_string, - just use the 8 bit version and then convert to unicode. */ -#if STRINGLIB_IS_UNICODE - unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE)); - if (unicode_tmp == NULL) { - PyErr_NoMemory(); - goto done; - } - strtounicode(unicode_tmp, buf, n_digits); - p = unicode_tmp; -#else - p = buf; -#endif - - /* Is a sign character present in the output? If so, remember it - and skip it */ - if (*p == '-') { - sign_char = *p; - ++p; - --n_digits; - } - - /* Determine if we have any "remainder" (after the digits, might include - decimal or exponent or both (or neither)) */ - parse_number(p, n_digits, &n_remainder, &has_decimal); - - /* Determine the grouping, separator, and decimal point, if any. */ - get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : - (format->thousands_separators ? - LT_DEFAULT_LOCALE : - LT_NO_LOCALE), - &locale); - - /* Calculate how much memory we'll need. */ - n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits, - n_remainder, has_decimal, &locale, format); - - /* Allocate the memory. */ - result = STRINGLIB_NEW(NULL, n_total); - if (result == NULL) - goto done; - - /* Populate the memory. */ - fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL, - format->fill_char == '\0' ? ' ' : format->fill_char, &locale, - 0); - -done: - PyMem_Free(buf); -#if STRINGLIB_IS_UNICODE - PyMem_Free(unicode_tmp); -#endif - return result; -} -#endif /* FORMAT_FLOAT */ - -/************************************************************************/ -/*********** complex formatting *****************************************/ -/************************************************************************/ - -#ifdef FORMAT_COMPLEX - -static PyObject * -format_complex_internal(PyObject *value, - const InternalFormatSpec *format) -{ - double re; - double im; - char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */ - char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */ - - InternalFormatSpec tmp_format = *format; - Py_ssize_t n_re_digits; - Py_ssize_t n_im_digits; - Py_ssize_t n_re_remainder; - Py_ssize_t n_im_remainder; - Py_ssize_t n_re_total; - Py_ssize_t n_im_total; - int re_has_decimal; - int im_has_decimal; - Py_ssize_t precision = format->precision; - Py_ssize_t default_precision = 6; - STRINGLIB_CHAR type = format->type; - STRINGLIB_CHAR *p_re; - STRINGLIB_CHAR *p_im; - NumberFieldWidths re_spec; - NumberFieldWidths im_spec; - int flags = 0; - PyObject *result = NULL; - STRINGLIB_CHAR *p; - STRINGLIB_CHAR re_sign_char = '\0'; - STRINGLIB_CHAR im_sign_char = '\0'; - int re_float_type; /* Used to see if we have a nan, inf, or regular float. */ - int im_float_type; - int add_parens = 0; - int skip_re = 0; - Py_ssize_t lpad; - Py_ssize_t rpad; - Py_ssize_t total; - -#if STRINGLIB_IS_UNICODE - Py_UNICODE *re_unicode_tmp = NULL; - Py_UNICODE *im_unicode_tmp = NULL; -#endif - - /* Locale settings, either from the actual locale or - from a hard-code pseudo-locale */ - LocaleInfo locale; - - /* Zero padding is not allowed. */ - if (format->fill_char == '0') { - PyErr_SetString(PyExc_ValueError, - "Zero padding is not allowed in complex format " - "specifier"); - goto done; - } - - /* Neither is '=' alignment . */ - if (format->align == '=') { - PyErr_SetString(PyExc_ValueError, - "'=' alignment flag is not allowed in complex format " - "specifier"); - goto done; - } - - re = PyComplex_RealAsDouble(value); - if (re == -1.0 && PyErr_Occurred()) - goto done; - im = PyComplex_ImagAsDouble(value); - if (im == -1.0 && PyErr_Occurred()) - goto done; - - if (format->alternate) - flags |= Py_DTSF_ALT; - - if (type == '\0') { - /* Omitted type specifier. Should be like str(self). */ - type = 'r'; - default_precision = 0; - if (re == 0.0 && copysign(1.0, re) == 1.0) - skip_re = 1; - else - add_parens = 1; - } - - if (type == 'n') - /* 'n' is the same as 'g', except for the locale used to - format the result. We take care of that later. */ - type = 'g'; - - if (precision < 0) - precision = default_precision; - else if (type == 'r') - type = 'g'; - - /* Cast "type", because if we're in unicode we need to pass a - 8-bit char. This is safe, because we've restricted what "type" - can be. */ - re_buf = PyOS_double_to_string(re, (char)type, precision, flags, - &re_float_type); - if (re_buf == NULL) - goto done; - im_buf = PyOS_double_to_string(im, (char)type, precision, flags, - &im_float_type); - if (im_buf == NULL) - goto done; - - n_re_digits = strlen(re_buf); - n_im_digits = strlen(im_buf); - - /* Since there is no unicode version of PyOS_double_to_string, - just use the 8 bit version and then convert to unicode. */ -#if STRINGLIB_IS_UNICODE - re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE)); - if (re_unicode_tmp == NULL) { - PyErr_NoMemory(); - goto done; - } - strtounicode(re_unicode_tmp, re_buf, n_re_digits); - p_re = re_unicode_tmp; - - im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE)); - if (im_unicode_tmp == NULL) { - PyErr_NoMemory(); - goto done; - } - strtounicode(im_unicode_tmp, im_buf, n_im_digits); - p_im = im_unicode_tmp; -#else - p_re = re_buf; - p_im = im_buf; -#endif - - /* Is a sign character present in the output? If so, remember it - and skip it */ - if (*p_re == '-') { - re_sign_char = *p_re; - ++p_re; - --n_re_digits; - } - if (*p_im == '-') { - im_sign_char = *p_im; - ++p_im; - --n_im_digits; - } - - /* Determine if we have any "remainder" (after the digits, might include - decimal or exponent or both (or neither)) */ - parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal); - parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal); - - /* Determine the grouping, separator, and decimal point, if any. */ - get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : - (format->thousands_separators ? - LT_DEFAULT_LOCALE : - LT_NO_LOCALE), - &locale); - - /* Turn off any padding. We'll do it later after we've composed - the numbers without padding. */ - tmp_format.fill_char = '\0'; - tmp_format.align = '<'; - tmp_format.width = -1; - - /* Calculate how much memory we'll need. */ - n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re, - n_re_digits, n_re_remainder, - re_has_decimal, &locale, &tmp_format); - - /* Same formatting, but always include a sign, unless the real part is - * going to be omitted, in which case we use whatever sign convention was - * requested by the original format. */ - if (!skip_re) - tmp_format.sign = '+'; - n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im, - n_im_digits, n_im_remainder, - im_has_decimal, &locale, &tmp_format); - - if (skip_re) - n_re_total = 0; - - /* Add 1 for the 'j', and optionally 2 for parens. */ - calc_padding(n_re_total + n_im_total + 1 + add_parens * 2, - format->width, format->align, &lpad, &rpad, &total); - - result = STRINGLIB_NEW(NULL, total); - if (result == NULL) - goto done; - - /* Populate the memory. First, the padding. */ - p = fill_padding(STRINGLIB_STR(result), - n_re_total + n_im_total + 1 + add_parens * 2, - format->fill_char=='\0' ? ' ' : format->fill_char, - lpad, rpad); - - if (add_parens) - *p++ = '('; - - if (!skip_re) { - fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0); - p += n_re_total; - } - fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0); - p += n_im_total; - *p++ = 'j'; - - if (add_parens) - *p++ = ')'; - -done: - PyMem_Free(re_buf); - PyMem_Free(im_buf); -#if STRINGLIB_IS_UNICODE - PyMem_Free(re_unicode_tmp); - PyMem_Free(im_unicode_tmp); -#endif - return result; -} -#endif /* FORMAT_COMPLEX */ - -/************************************************************************/ -/*********** built in formatters ****************************************/ -/************************************************************************/ -PyObject * -FORMAT_STRING(PyObject *obj, - STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len) -{ - InternalFormatSpec format; - PyObject *result = NULL; - - /* check for the special case of zero length format spec, make - it equivalent to str(obj) */ - if (format_spec_len == 0) { - result = STRINGLIB_TOSTR(obj); - goto done; - } - - /* parse the format_spec */ - if (!parse_internal_render_format_spec(format_spec, format_spec_len, - &format, 's', '<')) - goto done; - - /* type conversion? */ - switch (format.type) { - case 's': - /* no type conversion needed, already a string. do the formatting */ - result = format_string_internal(obj, &format); - break; - default: - /* unknown */ - unknown_presentation_type(format.type, obj->ob_type->tp_name); - goto done; - } - -done: - return result; -} - -#if defined FORMAT_LONG || defined FORMAT_INT -static PyObject* -format_int_or_long(PyObject* obj, - STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len, - IntOrLongToString tostring) -{ - PyObject *result = NULL; - PyObject *tmp = NULL; - InternalFormatSpec format; - - /* check for the special case of zero length format spec, make - it equivalent to str(obj) */ - if (format_spec_len == 0) { - result = STRINGLIB_TOSTR(obj); - goto done; - } - - /* parse the format_spec */ - if (!parse_internal_render_format_spec(format_spec, - format_spec_len, - &format, 'd', '>')) - goto done; - - /* type conversion? */ - switch (format.type) { - case 'b': - case 'c': - case 'd': - case 'o': - case 'x': - case 'X': - case 'n': - /* no type conversion needed, already an int (or long). do - the formatting */ - result = format_int_or_long_internal(obj, &format, tostring); - break; - - case 'e': - case 'E': - case 'f': - case 'F': - case 'g': - case 'G': - case '%': - /* convert to float */ - tmp = PyNumber_Float(obj); - if (tmp == NULL) - goto done; - result = format_float_internal(tmp, &format); - break; - - default: - /* unknown */ - unknown_presentation_type(format.type, obj->ob_type->tp_name); - goto done; - } - -done: - Py_XDECREF(tmp); - return result; -} -#endif /* FORMAT_LONG || defined FORMAT_INT */ - -#ifdef FORMAT_LONG -/* Need to define long_format as a function that will convert a long - to a string. In 3.0, _PyLong_Format has the correct signature. In - 2.x, we need to fudge a few parameters */ -#if PY_VERSION_HEX >= 0x03000000 -#define long_format _PyLong_Format -#else -static PyObject* -long_format(PyObject* value, int base) -{ - /* Convert to base, don't add trailing 'L', and use the new octal - format. We already know this is a long object */ - assert(PyLong_Check(value)); - /* convert to base, don't add 'L', and use the new octal format */ - return _PyLong_Format(value, base, 0, 1); -} -#endif - -PyObject * -FORMAT_LONG(PyObject *obj, - STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len) -{ - return format_int_or_long(obj, format_spec, format_spec_len, - long_format); -} -#endif /* FORMAT_LONG */ - -#ifdef FORMAT_INT -/* this is only used for 2.x, not 3.0 */ -static PyObject* -int_format(PyObject* value, int base) -{ - /* Convert to base, and use the new octal format. We already - know this is an int object */ - assert(PyInt_Check(value)); - return _PyInt_Format((PyIntObject*)value, base, 1); -} - -PyObject * -FORMAT_INT(PyObject *obj, - STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len) -{ - return format_int_or_long(obj, format_spec, format_spec_len, - int_format); -} -#endif /* FORMAT_INT */ - -#ifdef FORMAT_FLOAT -PyObject * -FORMAT_FLOAT(PyObject *obj, - STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len) -{ - PyObject *result = NULL; - InternalFormatSpec format; - - /* check for the special case of zero length format spec, make - it equivalent to str(obj) */ - if (format_spec_len == 0) { - result = STRINGLIB_TOSTR(obj); - goto done; - } - - /* parse the format_spec */ - if (!parse_internal_render_format_spec(format_spec, - format_spec_len, - &format, '\0', '>')) - goto done; - - /* type conversion? */ - switch (format.type) { - case '\0': /* No format code: like 'g', but with at least one decimal. */ - case 'e': - case 'E': - case 'f': - case 'F': - case 'g': - case 'G': - case 'n': - case '%': - /* no conversion, already a float. do the formatting */ - result = format_float_internal(obj, &format); - break; - - default: - /* unknown */ - unknown_presentation_type(format.type, obj->ob_type->tp_name); - goto done; - } - -done: - return result; -} -#endif /* FORMAT_FLOAT */ - -#ifdef FORMAT_COMPLEX -PyObject * -FORMAT_COMPLEX(PyObject *obj, - STRINGLIB_CHAR *format_spec, - Py_ssize_t format_spec_len) -{ - PyObject *result = NULL; - InternalFormatSpec format; - - /* check for the special case of zero length format spec, make - it equivalent to str(obj) */ - if (format_spec_len == 0) { - result = STRINGLIB_TOSTR(obj); - goto done; - } - - /* parse the format_spec */ - if (!parse_internal_render_format_spec(format_spec, - format_spec_len, - &format, '\0', '>')) - goto done; - - /* type conversion? */ - switch (format.type) { - case '\0': /* No format code: like 'g', but with at least one decimal. */ - case 'e': - case 'E': - case 'f': - case 'F': - case 'g': - case 'G': - case 'n': - /* no conversion, already a complex. do the formatting */ - result = format_complex_internal(obj, &format); - break; - - default: - /* unknown */ - unknown_presentation_type(format.type, obj->ob_type->tp_name); - goto done; - } - -done: - return result; -} -#endif /* FORMAT_COMPLEX */ diff --git a/Objects/stringlib/localeutil.h b/Objects/stringlib/localeutil.h index f548133..ddce69d 100644 --- a/Objects/stringlib/localeutil.h +++ b/Objects/stringlib/localeutil.h @@ -1,8 +1,5 @@ /* stringlib: locale related helpers implementation */ -#ifndef STRINGLIB_LOCALEUTIL_H -#define STRINGLIB_LOCALEUTIL_H - #include <locale.h> #define MAX(x, y) ((x) < (y) ? (y) : (x)) @@ -12,10 +9,10 @@ typedef struct { const char *grouping; char previous; Py_ssize_t i; /* Where we're currently pointing in grouping. */ -} GroupGenerator; +} STRINGLIB(GroupGenerator); static void -_GroupGenerator_init(GroupGenerator *self, const char *grouping) +STRINGLIB(GroupGenerator_init)(STRINGLIB(GroupGenerator) *self, const char *grouping) { self->grouping = grouping; self->i = 0; @@ -24,7 +21,7 @@ _GroupGenerator_init(GroupGenerator *self, const char *grouping) /* Returns the next grouping, or 0 to signify end. */ static Py_ssize_t -_GroupGenerator_next(GroupGenerator *self) +STRINGLIB(GroupGenerator_next)(STRINGLIB(GroupGenerator) *self) { /* Note that we don't really do much error checking here. If a grouping string contains just CHAR_MAX, for example, then just @@ -48,13 +45,11 @@ _GroupGenerator_next(GroupGenerator *self) /* Fill in some digits, leading zeros, and thousands separator. All are optional, depending on when we're called. */ static void -fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end, +STRINGLIB(fill)(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end, Py_ssize_t n_chars, Py_ssize_t n_zeros, const char* thousands_sep, Py_ssize_t thousands_sep_len) { -#if STRINGLIB_IS_UNICODE Py_ssize_t i; -#endif if (thousands_sep) { *buffer_end -= thousands_sep_len; @@ -76,7 +71,8 @@ fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end, memcpy(*buffer_end, *digits_end, n_chars * sizeof(STRINGLIB_CHAR)); *buffer_end -= n_zeros; - STRINGLIB_FILL(*buffer_end, '0', n_zeros); + for (i = 0; i < n_zeros; i++) + (*buffer_end)[i] = '0'; } /** @@ -133,15 +129,15 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer, be looked at */ /* A generator that returns all of the grouping widths, until it returns 0. */ - GroupGenerator groupgen; - _GroupGenerator_init(&groupgen, grouping); + STRINGLIB(GroupGenerator) groupgen; + STRINGLIB(GroupGenerator_init)(&groupgen, grouping); if (buffer) { buffer_end = buffer + n_buffer; digits_end = digits + n_digits; } - while ((l = _GroupGenerator_next(&groupgen)) > 0) { + while ((l = STRINGLIB(GroupGenerator_next)(&groupgen)) > 0) { l = MIN(l, MAX(MAX(remaining, min_width), 1)); n_zeros = MAX(0, l - remaining); n_chars = MAX(0, MIN(remaining, l)); @@ -153,7 +149,7 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer, if (buffer) { /* Copy into the output buffer. */ - fill(&digits_end, &buffer_end, n_chars, n_zeros, + STRINGLIB(fill)(&digits_end, &buffer_end, n_chars, n_zeros, use_separator ? thousands_sep : NULL, thousands_sep_len); } @@ -180,7 +176,7 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer, count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars; if (buffer) { /* Copy into the output buffer. */ - fill(&digits_end, &buffer_end, n_chars, n_zeros, + STRINGLIB(fill)(&digits_end, &buffer_end, n_chars, n_zeros, use_separator ? thousands_sep : NULL, thousands_sep_len); } } @@ -209,4 +205,3 @@ _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer, return _Py_InsertThousandsGrouping(buffer, n_buffer, digits, n_digits, min_width, grouping, thousands_sep); } -#endif /* STRINGLIB_LOCALEUTIL_H */ diff --git a/Objects/stringlib/partition.h b/Objects/stringlib/partition.h index 0170bdd..40cb512 100644 --- a/Objects/stringlib/partition.h +++ b/Objects/stringlib/partition.h @@ -1,14 +1,11 @@ /* stringlib: partition implementation */ -#ifndef STRINGLIB_PARTITION_H -#define STRINGLIB_PARTITION_H - #ifndef STRINGLIB_FASTSEARCH_H #error must include "stringlib/fastsearch.h" before including this module #endif Py_LOCAL_INLINE(PyObject*) -stringlib_partition(PyObject* str_obj, +STRINGLIB(partition)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len) @@ -25,7 +22,7 @@ stringlib_partition(PyObject* str_obj, if (!out) return NULL; - pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_SEARCH); + pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH); if (pos < 0) { #if STRINGLIB_MUTABLE @@ -58,7 +55,7 @@ stringlib_partition(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject*) -stringlib_rpartition(PyObject* str_obj, +STRINGLIB(rpartition)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len) @@ -75,7 +72,7 @@ stringlib_rpartition(PyObject* str_obj, if (!out) return NULL; - pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_RSEARCH); + pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH); if (pos < 0) { #if STRINGLIB_MUTABLE @@ -107,4 +104,3 @@ stringlib_rpartition(PyObject* str_obj, return out; } -#endif diff --git a/Objects/stringlib/split.h b/Objects/stringlib/split.h index 60e7767..947dd28 100644 --- a/Objects/stringlib/split.h +++ b/Objects/stringlib/split.h @@ -1,8 +1,5 @@ /* stringlib: split implementation */ -#ifndef STRINGLIB_SPLIT_H -#define STRINGLIB_SPLIT_H - #ifndef STRINGLIB_FASTSEARCH_H #error must include "stringlib/fastsearch.h" before including this module #endif @@ -54,7 +51,7 @@ #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count Py_LOCAL_INLINE(PyObject *) -stringlib_split_whitespace(PyObject* str_obj, +STRINGLIB(split_whitespace)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, Py_ssize_t maxcount) { @@ -102,7 +99,7 @@ stringlib_split_whitespace(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject *) -stringlib_split_char(PyObject* str_obj, +STRINGLIB(split_char)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR ch, Py_ssize_t maxcount) @@ -145,7 +142,7 @@ stringlib_split_char(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject *) -stringlib_split(PyObject* str_obj, +STRINGLIB(split)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len, Py_ssize_t maxcount) @@ -158,7 +155,7 @@ stringlib_split(PyObject* str_obj, return NULL; } else if (sep_len == 1) - return stringlib_split_char(str_obj, str, str_len, sep[0], maxcount); + return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount); list = PyList_New(PREALLOC_SIZE(maxcount)); if (list == NULL) @@ -166,7 +163,7 @@ stringlib_split(PyObject* str_obj, i = j = 0; while (maxcount-- > 0) { - pos = fastsearch(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH); + pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH); if (pos < 0) break; j = i + pos; @@ -193,7 +190,7 @@ stringlib_split(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject *) -stringlib_rsplit_whitespace(PyObject* str_obj, +STRINGLIB(rsplit_whitespace)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, Py_ssize_t maxcount) { @@ -243,7 +240,7 @@ stringlib_rsplit_whitespace(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject *) -stringlib_rsplit_char(PyObject* str_obj, +STRINGLIB(rsplit_char)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR ch, Py_ssize_t maxcount) @@ -287,7 +284,7 @@ stringlib_rsplit_char(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject *) -stringlib_rsplit(PyObject* str_obj, +STRINGLIB(rsplit)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len, Py_ssize_t maxcount) @@ -300,7 +297,7 @@ stringlib_rsplit(PyObject* str_obj, return NULL; } else if (sep_len == 1) - return stringlib_rsplit_char(str_obj, str, str_len, sep[0], maxcount); + return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount); list = PyList_New(PREALLOC_SIZE(maxcount)); if (list == NULL) @@ -308,7 +305,7 @@ stringlib_rsplit(PyObject* str_obj, j = str_len; while (maxcount-- > 0) { - pos = fastsearch(str, j, sep, sep_len, -1, FAST_RSEARCH); + pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH); if (pos < 0) break; SPLIT_ADD(str, pos + sep_len, j); @@ -336,7 +333,7 @@ stringlib_rsplit(PyObject* str_obj, } Py_LOCAL_INLINE(PyObject *) -stringlib_splitlines(PyObject* str_obj, +STRINGLIB(splitlines)(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, int keepends) { @@ -391,4 +388,3 @@ stringlib_splitlines(PyObject* str_obj, return NULL; } -#endif diff --git a/Objects/stringlib/stringdefs.h b/Objects/stringlib/stringdefs.h index 1c49426..9619332 100644 --- a/Objects/stringlib/stringdefs.h +++ b/Objects/stringlib/stringdefs.h @@ -6,6 +6,8 @@ compiled as unicode. */ #define STRINGLIB_IS_UNICODE 0 +#define FASTSEARCH fastsearch +#define STRINGLIB(F) stringlib_##F #define STRINGLIB_OBJECT PyBytesObject #define STRINGLIB_CHAR char #define STRINGLIB_TYPE_NAME "string" diff --git a/Objects/stringlib/ucs1lib.h b/Objects/stringlib/ucs1lib.h new file mode 100644 index 0000000..c0f7adf --- /dev/null +++ b/Objects/stringlib/ucs1lib.h @@ -0,0 +1,35 @@ +/* this is sort of a hack. there's at least one place (formatting + floats) where some stringlib code takes a different path if it's + compiled as unicode. */ +#define STRINGLIB_IS_UNICODE 1 + +#define FASTSEARCH ucs1lib_fastsearch +#define STRINGLIB(F) ucs1lib_##F +#define STRINGLIB_OBJECT PyUnicodeObject +#define STRINGLIB_CHAR Py_UCS1 +#define STRINGLIB_TYPE_NAME "unicode" +#define STRINGLIB_PARSE_CODE "U" +#define STRINGLIB_EMPTY unicode_empty +#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE +#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK +#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL +#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL +#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER +#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER +#define STRINGLIB_FILL Py_UNICODE_FILL +#define STRINGLIB_STR PyUnicode_1BYTE_DATA +#define STRINGLIB_LEN PyUnicode_GET_LENGTH +#define STRINGLIB_NEW _PyUnicode_FromUCS1 +#define STRINGLIB_RESIZE not_supported +#define STRINGLIB_CHECK PyUnicode_Check +#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact +#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping +#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale + +#define STRINGLIB_TOSTR PyObject_Str +#define STRINGLIB_TOASCII PyObject_ASCII + +#define _Py_InsertThousandsGrouping _PyUnicode_ucs1_InsertThousandsGrouping +#define _Py_InsertThousandsGroupingLocale _PyUnicode_ucs1_InsertThousandsGroupingLocale + + diff --git a/Objects/stringlib/ucs2lib.h b/Objects/stringlib/ucs2lib.h new file mode 100644 index 0000000..fc62fb7 --- /dev/null +++ b/Objects/stringlib/ucs2lib.h @@ -0,0 +1,34 @@ +/* this is sort of a hack. there's at least one place (formatting + floats) where some stringlib code takes a different path if it's + compiled as unicode. */ +#define STRINGLIB_IS_UNICODE 1 + +#define FASTSEARCH ucs2lib_fastsearch +#define STRINGLIB(F) ucs2lib_##F +#define STRINGLIB_OBJECT PyUnicodeObject +#define STRINGLIB_CHAR Py_UCS2 +#define STRINGLIB_TYPE_NAME "unicode" +#define STRINGLIB_PARSE_CODE "U" +#define STRINGLIB_EMPTY unicode_empty +#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE +#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK +#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL +#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL +#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER +#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER +#define STRINGLIB_FILL Py_UNICODE_FILL +#define STRINGLIB_STR PyUnicode_1BYTE_DATA +#define STRINGLIB_LEN PyUnicode_GET_LENGTH +#define STRINGLIB_NEW _PyUnicode_FromUCS2 +#define STRINGLIB_RESIZE not_supported +#define STRINGLIB_CHECK PyUnicode_Check +#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact +#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping +#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale + +#define STRINGLIB_TOSTR PyObject_Str +#define STRINGLIB_TOASCII PyObject_ASCII + +#define _Py_InsertThousandsGrouping _PyUnicode_ucs2_InsertThousandsGrouping +#define _Py_InsertThousandsGroupingLocale _PyUnicode_ucs2_InsertThousandsGroupingLocale + diff --git a/Objects/stringlib/ucs4lib.h b/Objects/stringlib/ucs4lib.h new file mode 100644 index 0000000..be1206a --- /dev/null +++ b/Objects/stringlib/ucs4lib.h @@ -0,0 +1,34 @@ +/* this is sort of a hack. there's at least one place (formatting + floats) where some stringlib code takes a different path if it's + compiled as unicode. */ +#define STRINGLIB_IS_UNICODE 1 + +#define FASTSEARCH ucs4lib_fastsearch +#define STRINGLIB(F) ucs4lib_##F +#define STRINGLIB_OBJECT PyUnicodeObject +#define STRINGLIB_CHAR Py_UCS4 +#define STRINGLIB_TYPE_NAME "unicode" +#define STRINGLIB_PARSE_CODE "U" +#define STRINGLIB_EMPTY unicode_empty +#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE +#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK +#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL +#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL +#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER +#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER +#define STRINGLIB_FILL Py_UNICODE_FILL +#define STRINGLIB_STR PyUnicode_1BYTE_DATA +#define STRINGLIB_LEN PyUnicode_GET_LENGTH +#define STRINGLIB_NEW _PyUnicode_FromUCS4 +#define STRINGLIB_RESIZE not_supported +#define STRINGLIB_CHECK PyUnicode_Check +#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact +#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping +#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale + +#define STRINGLIB_TOSTR PyObject_Str +#define STRINGLIB_TOASCII PyObject_ASCII + +#define _Py_InsertThousandsGrouping _PyUnicode_ucs4_InsertThousandsGrouping +#define _Py_InsertThousandsGroupingLocale _PyUnicode_ucs4_InsertThousandsGroupingLocale + diff --git a/Objects/stringlib/undef.h b/Objects/stringlib/undef.h new file mode 100644 index 0000000..40b4391 --- /dev/null +++ b/Objects/stringlib/undef.h @@ -0,0 +1,10 @@ +#undef FASTSEARCH +#undef STRINGLIB +#undef STRINGLIB_CHAR +#undef STRINGLIB_STR +#undef STRINGLIB_LEN +#undef STRINGLIB_NEW +#undef STRINGLIB_RESIZE +#undef _Py_InsertThousandsGrouping +#undef _Py_InsertThousandsGroupingLocale + diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/unicode_format.h index 6c7adcb..6281af0 100644 --- a/Objects/stringlib/string_format.h +++ b/Objects/stringlib/unicode_format.h @@ -1,16 +1,8 @@ /* - string_format.h -- implementation of string.format(). - - It uses the Objects/stringlib conventions, so that it can be - compiled for both unicode and string objects. + unicode_format.h -- implementation of str.format(). */ -/* Defines for Python 2.6 compatibility */ -#if PY_VERSION_HEX < 0x03000000 -#define PyLong_FromSsize_t _PyLong_FromSsize_t -#endif - /* Defines for more efficiently reallocating the string buffer */ #define INITIAL_SIZE_INCREMENT 100 #define SIZE_MULTIPLIER 2 @@ -26,8 +18,8 @@ unicode pointers. */ typedef struct { - STRINGLIB_CHAR *ptr; - STRINGLIB_CHAR *end; + PyObject *str; /* borrowed reference */ + Py_ssize_t start, end; } SubString; @@ -64,34 +56,32 @@ AutoNumber_Init(AutoNumber *auto_number) /* fill in a SubString from a pointer and length */ Py_LOCAL_INLINE(void) -SubString_init(SubString *str, STRINGLIB_CHAR *p, Py_ssize_t len) +SubString_init(SubString *str, PyObject *s, Py_ssize_t start, Py_ssize_t end) { - str->ptr = p; - if (p == NULL) - str->end = NULL; - else - str->end = str->ptr + len; + str->str = s; + str->start = start; + str->end = end; } -/* return a new string. if str->ptr is NULL, return None */ +/* return a new string. if str->str is NULL, return None */ Py_LOCAL_INLINE(PyObject *) SubString_new_object(SubString *str) { - if (str->ptr == NULL) { + if (str->str == NULL) { Py_INCREF(Py_None); return Py_None; } - return STRINGLIB_NEW(str->ptr, str->end - str->ptr); + return PyUnicode_Substring(str->str, str->start, str->end); } -/* return a new string. if str->ptr is NULL, return None */ +/* return a new string. if str->str is NULL, return None */ Py_LOCAL_INLINE(PyObject *) SubString_new_object_or_empty(SubString *str) { - if (str->ptr == NULL) { - return STRINGLIB_NEW(NULL, 0); + if (str->str == NULL) { + return PyUnicode_FromUnicode(NULL, 0); } - return STRINGLIB_NEW(str->ptr, str->end - str->ptr); + return SubString_new_object(str); } /* Return 1 if an error has been detected switching between automatic @@ -124,51 +114,6 @@ autonumber_state_error(AutoNumberState state, int field_name_is_empty) /*********** Output string management functions ****************/ /************************************************************************/ -typedef struct { - STRINGLIB_CHAR *ptr; - STRINGLIB_CHAR *end; - PyObject *obj; - Py_ssize_t size_increment; -} OutputString; - -/* initialize an OutputString object, reserving size characters */ -static int -output_initialize(OutputString *output, Py_ssize_t size) -{ - output->obj = STRINGLIB_NEW(NULL, size); - if (output->obj == NULL) - return 0; - - output->ptr = STRINGLIB_STR(output->obj); - output->end = STRINGLIB_LEN(output->obj) + output->ptr; - output->size_increment = INITIAL_SIZE_INCREMENT; - - return 1; -} - -/* - output_extend reallocates the output string buffer. - It returns a status: 0 for a failed reallocation, - 1 for success. -*/ - -static int -output_extend(OutputString *output, Py_ssize_t count) -{ - STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj); - Py_ssize_t curlen = output->ptr - startptr; - Py_ssize_t maxlen = curlen + count + output->size_increment; - - if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0) - return 0; - startptr = STRINGLIB_STR(output->obj); - output->ptr = startptr + curlen; - output->end = startptr + maxlen; - if (output->size_increment < MAX_SIZE_INCREMENT) - output->size_increment *= SIZE_MULTIPLIER; - return 1; -} - /* output_data dumps characters into our output string buffer. @@ -179,13 +124,17 @@ output_extend(OutputString *output, Py_ssize_t count) 1 for success. */ static int -output_data(OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count) +output_data(_PyAccu *acc, PyObject *s, Py_ssize_t start, Py_ssize_t end) { - if ((count > output->end - output->ptr) && !output_extend(output, count)) + PyObject *substring; + int r; + + substring = PyUnicode_Substring(s, start, end); + if (substring == NULL) return 0; - memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR)); - output->ptr += count; - return 1; + r = _PyAccu_Accumulate(acc, substring); + Py_DECREF(substring); + return r == 0; } /************************************************************************/ @@ -197,31 +146,28 @@ get_integer(const SubString *str) { Py_ssize_t accumulator = 0; Py_ssize_t digitval; - Py_ssize_t oldaccumulator; - STRINGLIB_CHAR *p; + Py_ssize_t i; /* empty string is an error */ - if (str->ptr >= str->end) + if (str->start >= str->end) return -1; - for (p = str->ptr; p < str->end; p++) { - digitval = STRINGLIB_TODECIMAL(*p); + for (i = str->start; i < str->end; i++) { + digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str->str, i)); if (digitval < 0) return -1; /* - This trick was copied from old Unicode format code. It's cute, - but would really suck on an old machine with a slow divide - implementation. Fortunately, in the normal case we do not - expect too many digits. + Detect possible overflow before it happens: + + accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if + accumulator > (PY_SSIZE_T_MAX - digitval) / 10. */ - oldaccumulator = accumulator; - accumulator *= 10; - if ((accumulator+10)/10 != oldaccumulator+1) { + if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { PyErr_Format(PyExc_ValueError, "Too many decimal digits in format string"); return -1; } - accumulator += digitval; + accumulator = accumulator * 10 + digitval; } return accumulator; } @@ -282,34 +228,36 @@ typedef struct { lifetime of the iterator. can be empty */ SubString str; - /* pointer to where we are inside field_name */ - STRINGLIB_CHAR *ptr; + /* index to where we are inside field_name */ + Py_ssize_t index; } FieldNameIterator; static int -FieldNameIterator_init(FieldNameIterator *self, STRINGLIB_CHAR *ptr, - Py_ssize_t len) +FieldNameIterator_init(FieldNameIterator *self, PyObject *s, + Py_ssize_t start, Py_ssize_t end) { - SubString_init(&self->str, ptr, len); - self->ptr = self->str.ptr; + SubString_init(&self->str, s, start, end); + self->index = start; return 1; } static int _FieldNameIterator_attr(FieldNameIterator *self, SubString *name) { - STRINGLIB_CHAR c; + Py_UCS4 c; - name->ptr = self->ptr; + name->str = self->str.str; + name->start = self->index; /* return everything until '.' or '[' */ - while (self->ptr < self->str.end) { - switch (c = *self->ptr++) { + while (self->index < self->str.end) { + c = PyUnicode_READ_CHAR(self->str.str, self->index++); + switch (c) { case '[': case '.': /* backup so that we this character will be seen next time */ - self->ptr--; + self->index--; break; default: continue; @@ -317,7 +265,7 @@ _FieldNameIterator_attr(FieldNameIterator *self, SubString *name) break; } /* end of string is okay */ - name->end = self->ptr; + name->end = self->index; return 1; } @@ -325,13 +273,15 @@ static int _FieldNameIterator_item(FieldNameIterator *self, SubString *name) { int bracket_seen = 0; - STRINGLIB_CHAR c; + Py_UCS4 c; - name->ptr = self->ptr; + name->str = self->str.str; + name->start = self->index; /* return everything until ']' */ - while (self->ptr < self->str.end) { - switch (c = *self->ptr++) { + while (self->index < self->str.end) { + c = PyUnicode_READ_CHAR(self->str.str, self->index++); + switch (c) { case ']': bracket_seen = 1; break; @@ -348,7 +298,7 @@ _FieldNameIterator_item(FieldNameIterator *self, SubString *name) /* end of string is okay */ /* don't include the ']' */ - name->end = self->ptr-1; + name->end = self->index-1; return 1; } @@ -358,10 +308,10 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute, Py_ssize_t *name_idx, SubString *name) { /* check at end of input */ - if (self->ptr >= self->str.end) + if (self->index >= self->str.end) return 1; - switch (*self->ptr++) { + switch (PyUnicode_READ_CHAR(self->str.str, self->index++)) { case '.': *is_attribute = 1; if (_FieldNameIterator_attr(self, name) == 0) @@ -384,7 +334,7 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute, } /* empty string is an error */ - if (name->ptr == name->end) { + if (name->start == name->end) { PyErr_SetString(PyExc_ValueError, "Empty attribute in format string"); return 0; } @@ -400,24 +350,23 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute, 'rest' is an iterator to return the rest */ static int -field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first, +field_name_split(PyObject *str, Py_ssize_t start, Py_ssize_t end, SubString *first, Py_ssize_t *first_idx, FieldNameIterator *rest, AutoNumber *auto_number) { - STRINGLIB_CHAR c; - STRINGLIB_CHAR *p = ptr; - STRINGLIB_CHAR *end = ptr + len; + Py_UCS4 c; + Py_ssize_t i = start; int field_name_is_empty; int using_numeric_index; /* find the part up until the first '.' or '[' */ - while (p < end) { - switch (c = *p++) { + while (i < end) { + switch (c = PyUnicode_READ_CHAR(str, i++)) { case '[': case '.': /* backup so that we this character is available to the "rest" iterator */ - p--; + i--; break; default: continue; @@ -426,15 +375,15 @@ field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first, } /* set up the return values */ - SubString_init(first, ptr, p - ptr); - FieldNameIterator_init(rest, p, end - p); + SubString_init(first, str, start, i); + FieldNameIterator_init(rest, str, i, end); /* see if "first" is an integer, in which case it's used as an index */ *first_idx = get_integer(first); if (*first_idx == -1 && PyErr_Occurred()) return 0; - field_name_is_empty = first->ptr >= first->end; + field_name_is_empty = first->start >= first->end; /* If the field name is omitted or if we have a numeric index specified, then we're doing numeric indexing into args. */ @@ -489,7 +438,7 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs, Py_ssize_t index; FieldNameIterator rest; - if (!field_name_split(input->ptr, input->end - input->ptr, &first, + if (!field_name_split(input->str, input->start, input->end, &first, &index, &rest, auto_number)) { goto error; } @@ -573,17 +522,13 @@ error: appends to the output. */ static int -render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output) +render_field(PyObject *fieldobj, SubString *format_spec, _PyAccu *acc) { int ok = 0; PyObject *result = NULL; PyObject *format_spec_object = NULL; - PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL; - STRINGLIB_CHAR* format_spec_start = format_spec->ptr ? - format_spec->ptr : NULL; - Py_ssize_t format_spec_len = format_spec->ptr ? - format_spec->end - format_spec->ptr : 0; - + PyObject *(*formatter)(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL; + /* If we know the type exactly, skip the lookup of __format__ and just call the formatter directly. */ if (PyUnicode_CheckExact(fieldobj)) @@ -599,39 +544,28 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output) if (formatter) { /* we know exactly which formatter will be called when __format__ is looked up, so call it directly, instead. */ - result = formatter(fieldobj, format_spec_start, format_spec_len); + result = formatter(fieldobj, format_spec->str, + format_spec->start, format_spec->end); } else { /* We need to create an object out of the pointers we have, because __format__ takes a string/unicode object for format_spec. */ - format_spec_object = STRINGLIB_NEW(format_spec_start, - format_spec_len); + if (format_spec->str) + format_spec_object = PyUnicode_Substring(format_spec->str, + format_spec->start, + format_spec->end); + else + format_spec_object = PyUnicode_New(0, 0); if (format_spec_object == NULL) goto done; result = PyObject_Format(fieldobj, format_spec_object); } - if (result == NULL) + if (result == NULL || PyUnicode_READY(result) == -1) goto done; -#if PY_VERSION_HEX >= 0x03000000 assert(PyUnicode_Check(result)); -#else - assert(PyBytes_Check(result) || PyUnicode_Check(result)); - - /* Convert result to our type. We could be str, and result could - be unicode */ - { - PyObject *tmp = STRINGLIB_TOSTR(result); - if (tmp == NULL) - goto done; - Py_DECREF(result); - result = tmp; - } -#endif - - ok = output_data(output, - STRINGLIB_STR(result), STRINGLIB_LEN(result)); + ok = output_data(acc, result, 0, PyUnicode_GET_LENGTH(result)); done: Py_XDECREF(format_spec_object); Py_XDECREF(result); @@ -640,23 +574,24 @@ done: static int parse_field(SubString *str, SubString *field_name, SubString *format_spec, - STRINGLIB_CHAR *conversion) + Py_UCS4 *conversion) { /* Note this function works if the field name is zero length, which is good. Zero length field names are handled later, in field_name_split. */ - STRINGLIB_CHAR c = 0; + Py_UCS4 c = 0; /* initialize these, as they may be empty */ *conversion = '\0'; - SubString_init(format_spec, NULL, 0); + SubString_init(format_spec, NULL, 0, 0); /* Search for the field name. it's terminated by the end of the string, or a ':' or '!' */ - field_name->ptr = str->ptr; - while (str->ptr < str->end) { - switch (c = *(str->ptr++)) { + field_name->str = str->str; + field_name->start = str->start; + while (str->start < str->end) { + switch ((c = PyUnicode_READ_CHAR(str->str, str->start++))) { case ':': case '!': break; @@ -669,26 +604,27 @@ parse_field(SubString *str, SubString *field_name, SubString *format_spec, if (c == '!' || c == ':') { /* we have a format specifier and/or a conversion */ /* don't include the last character */ - field_name->end = str->ptr-1; + field_name->end = str->start-1; /* the format specifier is the rest of the string */ - format_spec->ptr = str->ptr; + format_spec->str = str->str; + format_spec->start = str->start; format_spec->end = str->end; /* see if there's a conversion specifier */ if (c == '!') { /* there must be another character present */ - if (format_spec->ptr >= format_spec->end) { + if (format_spec->start >= format_spec->end) { PyErr_SetString(PyExc_ValueError, "end of format while looking for conversion " "specifier"); return 0; } - *conversion = *(format_spec->ptr++); + *conversion = PyUnicode_READ_CHAR(format_spec->str, format_spec->start++); /* if there is another character, it must be a colon */ - if (format_spec->ptr < format_spec->end) { - c = *(format_spec->ptr++); + if (format_spec->start < format_spec->end) { + c = PyUnicode_READ_CHAR(format_spec->str, format_spec->start++); if (c != ':') { PyErr_SetString(PyExc_ValueError, "expected ':' after format specifier"); @@ -699,7 +635,7 @@ parse_field(SubString *str, SubString *field_name, SubString *format_spec, } else /* end of string, there's no format_spec or conversion */ - field_name->end = str->ptr; + field_name->end = str->start; return 1; } @@ -718,9 +654,10 @@ typedef struct { } MarkupIterator; static int -MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len) +MarkupIterator_init(MarkupIterator *self, PyObject *str, + Py_ssize_t start, Py_ssize_t end) { - SubString_init(&self->str, ptr, len); + SubString_init(&self->str, str, start, end); return 1; } @@ -729,30 +666,30 @@ MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len) static int MarkupIterator_next(MarkupIterator *self, SubString *literal, int *field_present, SubString *field_name, - SubString *format_spec, STRINGLIB_CHAR *conversion, + SubString *format_spec, Py_UCS4 *conversion, int *format_spec_needs_expanding) { int at_end; - STRINGLIB_CHAR c = 0; - STRINGLIB_CHAR *start; + Py_UCS4 c = 0; + Py_ssize_t start; int count; Py_ssize_t len; int markup_follows = 0; /* initialize all of the output variables */ - SubString_init(literal, NULL, 0); - SubString_init(field_name, NULL, 0); - SubString_init(format_spec, NULL, 0); + SubString_init(literal, NULL, 0, 0); + SubString_init(field_name, NULL, 0, 0); + SubString_init(format_spec, NULL, 0, 0); *conversion = '\0'; *format_spec_needs_expanding = 0; *field_present = 0; /* No more input, end of iterator. This is the normal exit path. */ - if (self->str.ptr >= self->str.end) + if (self->str.start >= self->str.end) return 1; - start = self->str.ptr; + start = self->str.start; /* First read any literal text. Read until the end of string, an escaped '{' or '}', or an unescaped '{'. In order to never @@ -761,8 +698,8 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, including the brace, but no format object. The next time through, we'll return the rest of the literal, skipping past the second consecutive brace. */ - while (self->str.ptr < self->str.end) { - switch (c = *(self->str.ptr++)) { + while (self->str.start < self->str.end) { + switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) { case '{': case '}': markup_follows = 1; @@ -773,10 +710,12 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, break; } - at_end = self->str.ptr >= self->str.end; - len = self->str.ptr - start; + at_end = self->str.start >= self->str.end; + len = self->str.start - start; - if ((c == '}') && (at_end || (c != *self->str.ptr))) { + if ((c == '}') && (at_end || + (c != PyUnicode_READ_CHAR(self->str.str, + self->str.start)))) { PyErr_SetString(PyExc_ValueError, "Single '}' encountered " "in format string"); return 0; @@ -787,10 +726,10 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, return 0; } if (!at_end) { - if (c == *self->str.ptr) { + if (c == PyUnicode_READ_CHAR(self->str.str, self->str.start)) { /* escaped } or {, skip it in the input. there is no markup object following us, just this literal text */ - self->str.ptr++; + self->str.start++; markup_follows = 0; } else @@ -798,7 +737,8 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, } /* record the literal text */ - literal->ptr = start; + literal->str = self->str.str; + literal->start = start; literal->end = start + len; if (!markup_follows) @@ -810,12 +750,12 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, *field_present = 1; count = 1; - start = self->str.ptr; + start = self->str.start; /* we know we can't have a zero length string, so don't worry about that case */ - while (self->str.ptr < self->str.end) { - switch (c = *(self->str.ptr++)) { + while (self->str.start < self->str.end) { + switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) { case '{': /* the format spec needs to be recursively expanded. this is an optimization, and not strictly needed */ @@ -828,7 +768,7 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, /* we're done. parse and get out */ SubString s; - SubString_init(&s, start, self->str.ptr - 1 - start); + SubString_init(&s, self->str.str, start, self->str.start - 1); if (parse_field(&s, field_name, format_spec, conversion) == 0) return 0; @@ -847,7 +787,7 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal, /* do the !r or !s conversion on obj */ static PyObject * -do_conversion(PyObject *obj, STRINGLIB_CHAR conversion) +do_conversion(PyObject *obj, Py_UCS4 conversion) { /* XXX in pre-3.0, do we need to convert this to unicode, since it might have returned a string? */ @@ -855,11 +795,9 @@ do_conversion(PyObject *obj, STRINGLIB_CHAR conversion) case 'r': return PyObject_Repr(obj); case 's': - return STRINGLIB_TOSTR(obj); -#if PY_VERSION_HEX >= 0x03000000 + return PyObject_Str(obj); case 'a': - return STRINGLIB_TOASCII(obj); -#endif + return PyObject_ASCII(obj); default: if (conversion > 32 && conversion < 127) { /* It's the ASCII subrange; casting to char is safe @@ -891,8 +829,8 @@ do_conversion(PyObject *obj, STRINGLIB_CHAR conversion) static int output_markup(SubString *field_name, SubString *format_spec, - int format_spec_needs_expanding, STRINGLIB_CHAR conversion, - OutputString *output, PyObject *args, PyObject *kwargs, + int format_spec_needs_expanding, Py_UCS4 conversion, + _PyAccu *acc, PyObject *args, PyObject *kwargs, int recursion_depth, AutoNumber *auto_number) { PyObject *tmp = NULL; @@ -908,7 +846,7 @@ output_markup(SubString *field_name, SubString *format_spec, if (conversion != '\0') { tmp = do_conversion(fieldobj, conversion); - if (tmp == NULL) + if (tmp == NULL || PyUnicode_READY(tmp) == -1) goto done; /* do the assignment, transferring ownership: fieldobj = tmp */ @@ -921,20 +859,19 @@ output_markup(SubString *field_name, SubString *format_spec, if (format_spec_needs_expanding) { tmp = build_string(format_spec, args, kwargs, recursion_depth-1, auto_number); - if (tmp == NULL) + if (tmp == NULL || PyUnicode_READY(tmp) == -1) goto done; /* note that in the case we're expanding the format string, tmp must be kept around until after the call to render_field. */ - SubString_init(&expanded_format_spec, - STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp)); + SubString_init(&expanded_format_spec, tmp, 0, PyUnicode_GET_LENGTH(tmp)); actual_format_spec = &expanded_format_spec; } else actual_format_spec = format_spec; - if (render_field(fieldobj, actual_format_spec, output) == 0) + if (render_field(fieldobj, actual_format_spec, acc) == 0) goto done; result = 1; @@ -954,7 +891,7 @@ done: */ static int do_markup(SubString *input, PyObject *args, PyObject *kwargs, - OutputString *output, int recursion_depth, AutoNumber *auto_number) + _PyAccu *acc, int recursion_depth, AutoNumber *auto_number) { MarkupIterator iter; int format_spec_needs_expanding; @@ -963,18 +900,18 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs, SubString literal; SubString field_name; SubString format_spec; - STRINGLIB_CHAR conversion; + Py_UCS4 conversion; - MarkupIterator_init(&iter, input->ptr, input->end - input->ptr); + MarkupIterator_init(&iter, input->str, input->start, input->end); while ((result = MarkupIterator_next(&iter, &literal, &field_present, &field_name, &format_spec, &conversion, &format_spec_needs_expanding)) == 2) { - if (!output_data(output, literal.ptr, literal.end - literal.ptr)) + if (!output_data(acc, literal.str, literal.start, literal.end)) return 0; if (field_present) if (!output_markup(&field_name, &format_spec, - format_spec_needs_expanding, conversion, output, + format_spec_needs_expanding, conversion, acc, args, kwargs, recursion_depth, auto_number)) return 0; } @@ -990,43 +927,25 @@ static PyObject * build_string(SubString *input, PyObject *args, PyObject *kwargs, int recursion_depth, AutoNumber *auto_number) { - OutputString output; - PyObject *result = NULL; - Py_ssize_t count; - - output.obj = NULL; /* needed so cleanup code always works */ + _PyAccu acc; /* check the recursion level */ if (recursion_depth <= 0) { PyErr_SetString(PyExc_ValueError, "Max string recursion exceeded"); - goto done; + return NULL; } - /* initial size is the length of the format string, plus the size - increment. seems like a reasonable default */ - if (!output_initialize(&output, - input->end - input->ptr + - INITIAL_SIZE_INCREMENT)) - goto done; + if (_PyAccu_Init(&acc)) + return NULL; - if (!do_markup(input, args, kwargs, &output, recursion_depth, + if (!do_markup(input, args, kwargs, &acc, recursion_depth, auto_number)) { - goto done; - } - - count = output.ptr - STRINGLIB_STR(output.obj); - if (STRINGLIB_RESIZE(&output.obj, count) < 0) { - goto done; + _PyAccu_Destroy(&acc); + return NULL; } - /* transfer ownership to result */ - result = output.obj; - output.obj = NULL; - -done: - Py_XDECREF(output.obj); - return result; + return _PyAccu_Finish(&acc); } /************************************************************************/ @@ -1047,8 +966,11 @@ do_string_format(PyObject *self, PyObject *args, PyObject *kwargs) AutoNumber auto_number; + if (PyUnicode_READY(self) == -1) + return NULL; + AutoNumber_Init(&auto_number); - SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self)); + SubString_init(&input, self, 0, PyUnicode_GET_LENGTH(self)); return build_string(&input, args, kwargs, recursion_depth, &auto_number); } @@ -1071,7 +993,7 @@ do_string_format_map(PyObject *self, PyObject *obj) typedef struct { PyObject_HEAD - STRINGLIB_OBJECT *str; + PyUnicodeObject *str; MarkupIterator it_markup; } formatteriterobject; @@ -1097,7 +1019,7 @@ formatteriter_next(formatteriterobject *it) SubString literal; SubString field_name; SubString format_spec; - STRINGLIB_CHAR conversion; + Py_UCS4 conversion; int format_spec_needs_expanding; int field_present; int result = MarkupIterator_next(&it->it_markup, &literal, &field_present, @@ -1141,7 +1063,8 @@ formatteriter_next(formatteriterobject *it) Py_INCREF(conversion_str); } else - conversion_str = STRINGLIB_NEW(&conversion, 1); + conversion_str = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, + &conversion, 1); if (conversion_str == NULL) goto done; @@ -1198,7 +1121,7 @@ static PyTypeObject PyFormatterIter_Type = { describing the parsed elements. It's a wrapper around stringlib/string_format.h's MarkupIterator */ static PyObject * -formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self) +formatter_parser(PyObject *ignored, PyUnicodeObject *self) { formatteriterobject *it; @@ -1207,6 +1130,9 @@ formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self) return NULL; } + if (PyUnicode_READY(self) == -1) + return NULL; + it = PyObject_New(formatteriterobject, &PyFormatterIter_Type); if (it == NULL) return NULL; @@ -1216,10 +1142,7 @@ formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self) it->str = self; /* initialize the contained MarkupIterator */ - MarkupIterator_init(&it->it_markup, - STRINGLIB_STR(self), - STRINGLIB_LEN(self)); - + MarkupIterator_init(&it->it_markup, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self)); return (PyObject *)it; } @@ -1236,7 +1159,7 @@ formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self) typedef struct { PyObject_HEAD - STRINGLIB_OBJECT *str; + PyUnicodeObject *str; FieldNameIterator it_field; } fieldnameiterobject; @@ -1338,7 +1261,7 @@ static PyTypeObject PyFieldNameIter_Type = { field_name_split. The iterator it returns is a FieldNameIterator */ static PyObject * -formatter_field_name_split(PyObject *ignored, STRINGLIB_OBJECT *self) +formatter_field_name_split(PyObject *ignored, PyUnicodeObject *self) { SubString first; Py_ssize_t first_idx; @@ -1352,6 +1275,9 @@ formatter_field_name_split(PyObject *ignored, STRINGLIB_OBJECT *self) return NULL; } + if (PyUnicode_READY(self) == -1) + return NULL; + it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type); if (it == NULL) return NULL; @@ -1363,8 +1289,7 @@ formatter_field_name_split(PyObject *ignored, STRINGLIB_OBJECT *self) /* Pass in auto_number = NULL. We'll return an empty string for first_obj in that case. */ - if (!field_name_split(STRINGLIB_STR(self), - STRINGLIB_LEN(self), + if (!field_name_split((PyObject*)self, 0, PyUnicode_GET_LENGTH(self), &first, &first_idx, &it->it_field, NULL)) goto done; diff --git a/Objects/stringlib/unicodedefs.h b/Objects/stringlib/unicodedefs.h index 09dae6d..0c40b80 100644 --- a/Objects/stringlib/unicodedefs.h +++ b/Objects/stringlib/unicodedefs.h @@ -6,6 +6,8 @@ compiled as unicode. */ #define STRINGLIB_IS_UNICODE 1 +#define FASTSEARCH fastsearch +#define STRINGLIB(F) stringlib_##F #define STRINGLIB_OBJECT PyUnicodeObject #define STRINGLIB_CHAR Py_UNICODE #define STRINGLIB_TYPE_NAME "unicode" diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 8aacd12..54a580d 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -86,7 +86,7 @@ PyTuple_New(register Py_ssize_t size) { return PyErr_NoMemory(); } - nbytes += sizeof(PyTupleObject) - sizeof(PyObject *); + /* nbytes += sizeof(PyTupleObject) - sizeof(PyObject *); */ op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); if (op == NULL) @@ -312,11 +312,12 @@ error: static Py_hash_t tuplehash(PyTupleObject *v) { - register Py_hash_t x, y; + register Py_uhash_t x; + register Py_hash_t y; register Py_ssize_t len = Py_SIZE(v); register PyObject **p; - Py_hash_t mult = 1000003L; - x = 0x345678L; + Py_uhash_t mult = 1000003; + x = 0x345678; p = v->ob_item; while (--len >= 0) { y = PyObject_Hash(*p++); @@ -327,7 +328,7 @@ tuplehash(PyTupleObject *v) mult += (Py_hash_t)(82520L + len + len); } x += 97531L; - if (x == -1) + if (x == (Py_uhash_t)-1) x = -2; return x; } @@ -543,10 +544,8 @@ tuplerichcompare(PyObject *v, PyObject *w, int op) Py_ssize_t i; Py_ssize_t vlen, wlen; - if (!PyTuple_Check(v) || !PyTuple_Check(w)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyTuple_Check(v) || !PyTuple_Check(w)) + Py_RETURN_NOTIMPLEMENTED; vt = (PyTupleObject *)v; wt = (PyTupleObject *)w; diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 856a4a5..2e6eb0a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -20,10 +20,11 @@ >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP)) #define MCACHE_HASH_METHOD(type, name) \ MCACHE_HASH((type)->tp_version_tag, \ - ((PyUnicodeObject *)(name))->hash) + ((PyASCIIObject *)(name))->hash) #define MCACHE_CACHEABLE_NAME(name) \ PyUnicode_CheckExact(name) && \ - PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE + PyUnicode_READY(name) != -1 && \ + PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE struct method_cache_entry { unsigned int version; @@ -201,6 +202,22 @@ static PyMemberDef type_members[] = { {0} }; +static int +check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) +{ + if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { + PyErr_Format(PyExc_TypeError, + "can't set %s.%s", type->tp_name, name); + return 0; + } + if (!value) { + PyErr_Format(PyExc_TypeError, + "can't delete %s.%s", type->tp_name, name); + return 0; + } + return 1; +} + static PyObject * type_name(PyTypeObject *type, void *context) { @@ -229,16 +246,8 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context) char *tp_name; PyObject *tmp; - if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { - PyErr_Format(PyExc_TypeError, - "can't set %s.__name__", type->tp_name); - return -1; - } - if (!value) { - PyErr_Format(PyExc_TypeError, - "can't delete %s.__name__", type->tp_name); + if (!check_set_special_type_attr(type, value, "__name__")) return -1; - } if (!PyUnicode_Check(value)) { PyErr_Format(PyExc_TypeError, "can only assign string to %s.__name__, not '%s'", @@ -301,16 +310,8 @@ type_module(PyTypeObject *type, void *context) static int type_set_module(PyTypeObject *type, PyObject *value, void *context) { - if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { - PyErr_Format(PyExc_TypeError, - "can't set %s.__module__", type->tp_name); - return -1; - } - if (!value) { - PyErr_Format(PyExc_TypeError, - "can't delete %s.__module__", type->tp_name); + if (!check_set_special_type_attr(type, value, "__module__")) return -1; - } PyType_Modified(type); @@ -433,16 +434,8 @@ type_set_bases(PyTypeObject *type, PyObject *value, void *context) PyTypeObject *new_base, *old_base; PyObject *old_bases, *old_mro; - if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { - PyErr_Format(PyExc_TypeError, - "can't set %s.__bases__", type->tp_name); - return -1; - } - if (!value) { - PyErr_Format(PyExc_TypeError, - "can't delete %s.__bases__", type->tp_name); + if (!check_set_special_type_attr(type, value, "__bases__")) return -1; - } if (!PyTuple_Check(value)) { PyErr_Format(PyExc_TypeError, "can only assign tuple to %s.__bases__, not %s", @@ -596,6 +589,15 @@ type_get_doc(PyTypeObject *type, void *context) return result; } +static int +type_set_doc(PyTypeObject *type, PyObject *value, void *context) +{ + if (!check_set_special_type_attr(type, value, "__doc__")) + return -1; + PyType_Modified(type); + return PyDict_SetItemString(type->tp_dict, "__doc__", value); +} + static PyObject * type___instancecheck__(PyObject *type, PyObject *inst) { @@ -631,7 +633,7 @@ static PyGetSetDef type_getsets[] = { {"__abstractmethods__", (getter)type_abstractmethods, (setter)type_set_abstractmethods, NULL}, {"__dict__", (getter)type_dict, NULL, NULL}, - {"__doc__", (getter)type_get_doc, NULL, NULL}, + {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, {0} }; @@ -902,7 +904,7 @@ subtype_dealloc(PyObject *self) /* Find the nearest base with a different tp_dealloc */ base = type; - while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { + while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { base = base->tp_base; assert(base); } @@ -1212,10 +1214,8 @@ call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) func = lookup_maybe(o, name, nameobj); if (func == NULL) { va_end(va); - if (!PyErr_Occurred()) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyErr_Occurred()) + Py_RETURN_NOTIMPLEMENTED; return NULL; } @@ -2098,6 +2098,13 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) goto bad_slots; } PyList_SET_ITEM(newslots, j, tmp); + if (PyDict_GetItem(dict, tmp)) { + PyErr_Format(PyExc_ValueError, + "%R in __slots__ conflicts with class variable", + tmp); + Py_DECREF(newslots); + goto bad_slots; + } j++; } assert(j == nslots - add_dict - add_weak); @@ -2338,21 +2345,21 @@ PyObject* PyType_FromSpec(PyType_Spec *spec) return NULL; res->ht_name = PyUnicode_FromString(spec->name); if (!res->ht_name) - goto fail; + goto fail; res->ht_type.tp_name = _PyUnicode_AsString(res->ht_name); if (!res->ht_type.tp_name) - goto fail; + goto fail; res->ht_type.tp_basicsize = spec->basicsize; res->ht_type.tp_itemsize = spec->itemsize; res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; for (slot = spec->slots; slot->slot; slot++) { - if (slot->slot >= sizeof(slotoffsets)/sizeof(slotoffsets[0])) { - PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); - goto fail; - } - *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; + if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { + PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); + goto fail; + } + *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; /* need to make a copy of the docstring slot, which usually points to a static string literal */ @@ -2360,7 +2367,7 @@ PyObject* PyType_FromSpec(PyType_Spec *spec) ssize_t len = strlen(slot->pfunc)+1; char *tp_doc = PyObject_MALLOC(len); if (tp_doc == NULL) - goto fail; + goto fail; memcpy(tp_doc, slot->pfunc, len); res->ht_type.tp_doc = tp_doc; } @@ -2576,6 +2583,82 @@ type_prepare(PyObject *self, PyObject *args, PyObject *kwds) return PyDict_New(); } +/* + Merge the __dict__ of aclass into dict, and recursively also all + the __dict__s of aclass's base classes. The order of merging isn't + defined, as it's expected that only the final set of dict keys is + interesting. + Return 0 on success, -1 on error. +*/ + +static int +merge_class_dict(PyObject *dict, PyObject *aclass) +{ + PyObject *classdict; + PyObject *bases; + + assert(PyDict_Check(dict)); + assert(aclass); + + /* Merge in the type's dict (if any). */ + classdict = PyObject_GetAttrString(aclass, "__dict__"); + if (classdict == NULL) + PyErr_Clear(); + else { + int status = PyDict_Update(dict, classdict); + Py_DECREF(classdict); + if (status < 0) + return -1; + } + + /* Recursively merge in the base types' (if any) dicts. */ + bases = PyObject_GetAttrString(aclass, "__bases__"); + if (bases == NULL) + PyErr_Clear(); + else { + /* We have no guarantee that bases is a real tuple */ + Py_ssize_t i, n; + n = PySequence_Size(bases); /* This better be right */ + if (n < 0) + PyErr_Clear(); + else { + for (i = 0; i < n; i++) { + int status; + PyObject *base = PySequence_GetItem(bases, i); + if (base == NULL) { + Py_DECREF(bases); + return -1; + } + status = merge_class_dict(dict, base); + Py_DECREF(base); + if (status < 0) { + Py_DECREF(bases); + return -1; + } + } + } + Py_DECREF(bases); + } + return 0; +} + +/* __dir__ for type objects: returns __dict__ and __bases__. + We deliberately don't suck up its __class__, as methods belonging to the + metaclass would probably be more confusing than helpful. +*/ +static PyObject * +type_dir(PyObject *self, PyObject *args) +{ + PyObject *result = NULL; + PyObject *dict = PyDict_New(); + + if (dict != NULL && merge_class_dict(dict, self) == 0) + result = PyDict_Keys(dict); + + Py_XDECREF(dict); + return result; +} + static PyMethodDef type_methods[] = { {"mro", (PyCFunction)mro_external, METH_NOARGS, PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, @@ -2589,6 +2672,8 @@ static PyMethodDef type_methods[] = { PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")}, {"__subclasscheck__", type___subclasscheck__, METH_O, PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, + {"__dir__", type_dir, METH_NOARGS, + PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")}, {0} }; @@ -3077,14 +3162,19 @@ static PyObject * import_copyreg(void) { static PyObject *copyreg_str; + static PyObject *mod_copyreg = NULL; if (!copyreg_str) { copyreg_str = PyUnicode_InternFromString("copyreg"); if (copyreg_str == NULL) return NULL; } + if (!mod_copyreg) { + mod_copyreg = PyImport_Import(copyreg_str); + } - return PyImport_Import(copyreg_str); + Py_XINCREF(mod_copyreg); + return mod_copyreg; } static PyObject * @@ -3093,14 +3183,16 @@ slotnames(PyObject *cls) PyObject *clsdict; PyObject *copyreg; PyObject *slotnames; + static PyObject *str_slotnames; - if (!PyType_Check(cls)) { - Py_INCREF(Py_None); - return Py_None; + if (str_slotnames == NULL) { + str_slotnames = PyUnicode_InternFromString("__slotnames__"); + if (str_slotnames == NULL) + return NULL; } clsdict = ((PyTypeObject *)cls)->tp_dict; - slotnames = PyDict_GetItemString(clsdict, "__slotnames__"); + slotnames = PyDict_GetItem(clsdict, str_slotnames); if (slotnames != NULL && PyList_Check(slotnames)) { Py_INCREF(slotnames); return slotnames; @@ -3134,12 +3226,20 @@ reduce_2(PyObject *obj) PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; PyObject *copyreg = NULL, *newobj = NULL, *res = NULL; Py_ssize_t i, n; + static PyObject *str_getnewargs = NULL, *str_getstate = NULL, + *str_newobj = NULL; + + if (str_getnewargs == NULL) { + str_getnewargs = PyUnicode_InternFromString("__getnewargs__"); + str_getstate = PyUnicode_InternFromString("__getstate__"); + str_newobj = PyUnicode_InternFromString("__newobj__"); + if (!str_getnewargs || !str_getstate || !str_newobj) + return NULL; + } - cls = PyObject_GetAttrString(obj, "__class__"); - if (cls == NULL) - return NULL; + cls = (PyObject *) Py_TYPE(obj); - getnewargs = PyObject_GetAttrString(obj, "__getnewargs__"); + getnewargs = PyObject_GetAttr(obj, str_getnewargs); if (getnewargs != NULL) { args = PyObject_CallObject(getnewargs, NULL); Py_DECREF(getnewargs); @@ -3157,7 +3257,7 @@ reduce_2(PyObject *obj) if (args == NULL) goto end; - getstate = PyObject_GetAttrString(obj, "__getstate__"); + getstate = PyObject_GetAttr(obj, str_getstate); if (getstate != NULL) { state = PyObject_CallObject(getstate, NULL); Py_DECREF(getstate); @@ -3165,17 +3265,18 @@ reduce_2(PyObject *obj) goto end; } else { + PyObject **dict; PyErr_Clear(); - state = PyObject_GetAttrString(obj, "__dict__"); - if (state == NULL) { - PyErr_Clear(); + dict = _PyObject_GetDictPtr(obj); + if (dict && *dict) + state = *dict; + else state = Py_None; - Py_INCREF(state); - } + Py_INCREF(state); names = slotnames(cls); if (names == NULL) goto end; - if (names != Py_None) { + if (names != Py_None && PyList_GET_SIZE(names) > 0) { assert(PyList_Check(names)); slots = PyDict_New(); if (slots == NULL) @@ -3234,7 +3335,7 @@ reduce_2(PyObject *obj) copyreg = import_copyreg(); if (copyreg == NULL) goto end; - newobj = PyObject_GetAttrString(copyreg, "__newobj__"); + newobj = PyObject_GetAttr(copyreg, str_newobj); if (newobj == NULL) goto end; @@ -3242,8 +3343,8 @@ reduce_2(PyObject *obj) args2 = PyTuple_New(n+1); if (args2 == NULL) goto end; + Py_INCREF(cls); PyTuple_SET_ITEM(args2, 0, cls); - cls = NULL; for (i = 0; i < n; i++) { PyObject *v = PyTuple_GET_ITEM(args, i); Py_INCREF(v); @@ -3253,7 +3354,6 @@ reduce_2(PyObject *obj) res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems); end: - Py_XDECREF(cls); Py_XDECREF(args); Py_XDECREF(args2); Py_XDECREF(slots); @@ -3313,31 +3413,34 @@ object_reduce(PyObject *self, PyObject *args) static PyObject * object_reduce_ex(PyObject *self, PyObject *args) { + static PyObject *str_reduce = NULL, *objreduce; PyObject *reduce, *res; int proto = 0; if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) return NULL; - reduce = PyObject_GetAttrString(self, "__reduce__"); + if (str_reduce == NULL) { + str_reduce = PyUnicode_InternFromString("__reduce__"); + objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, + "__reduce__"); + if (str_reduce == NULL || objreduce == NULL) + return NULL; + } + + reduce = PyObject_GetAttr(self, str_reduce); if (reduce == NULL) PyErr_Clear(); else { - PyObject *cls, *clsreduce, *objreduce; + PyObject *cls, *clsreduce; int override; - cls = PyObject_GetAttrString(self, "__class__"); - if (cls == NULL) { - Py_DECREF(reduce); - return NULL; - } - clsreduce = PyObject_GetAttrString(cls, "__reduce__"); - Py_DECREF(cls); + + cls = (PyObject *) Py_TYPE(self); + clsreduce = PyObject_GetAttr(cls, str_reduce); if (clsreduce == NULL) { Py_DECREF(reduce); return NULL; } - objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict, - "__reduce__"); override = (clsreduce != objreduce); Py_DECREF(clsreduce); if (override) { @@ -3355,8 +3458,7 @@ object_reduce_ex(PyObject *self, PyObject *args) static PyObject * object_subclasshook(PyObject *cls, PyObject *args) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } PyDoc_STRVAR(object_subclasshook_doc, @@ -3387,21 +3489,21 @@ object_format(PyObject *self, PyObject *args) self_as_str = PyObject_Str(self); if (self_as_str != NULL) { /* Issue 7994: If we're converting to a string, we - should reject format specifications */ - if (PyUnicode_GET_SIZE(format_spec) > 0) { - if (PyErr_WarnEx(PyExc_PendingDeprecationWarning, - "object.__format__ with a non-empty format " - "string is deprecated", 1) < 0) { - goto done; - } - /* Eventually this will become an error: - PyErr_Format(PyExc_TypeError, - "non-empty format string passed to object.__format__"); - goto done; - */ - } - - result = PyObject_Format(self_as_str, format_spec); + should reject format specifications */ + if (PyUnicode_GET_LENGTH(format_spec) > 0) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "object.__format__ with a non-empty format " + "string is deprecated", 1) < 0) { + goto done; + } + /* Eventually this will become an error: + PyErr_Format(PyExc_TypeError, + "non-empty format string passed to object.__format__"); + goto done; + */ + } + + result = PyObject_Format(self_as_str, format_spec); } done: @@ -3424,6 +3526,53 @@ object_sizeof(PyObject *self, PyObject *args) return PyLong_FromSsize_t(res); } +/* __dir__ for generic objects: returns __dict__, __class__, + and recursively up the __class__.__bases__ chain. +*/ +static PyObject * +object_dir(PyObject *self, PyObject *args) +{ + PyObject *result = NULL; + PyObject *dict = NULL; + PyObject *itsclass = NULL; + + /* Get __dict__ (which may or may not be a real dict...) */ + dict = PyObject_GetAttrString(self, "__dict__"); + if (dict == NULL) { + PyErr_Clear(); + dict = PyDict_New(); + } + else if (!PyDict_Check(dict)) { + Py_DECREF(dict); + dict = PyDict_New(); + } + else { + /* Copy __dict__ to avoid mutating it. */ + PyObject *temp = PyDict_Copy(dict); + Py_DECREF(dict); + dict = temp; + } + + if (dict == NULL) + goto error; + + /* Merge in attrs reachable from its class. */ + itsclass = PyObject_GetAttrString(self, "__class__"); + if (itsclass == NULL) + /* XXX(tomer): Perhaps fall back to obj->ob_type if no + __class__ exists? */ + PyErr_Clear(); + else if (merge_class_dict(dict, itsclass) != 0) + goto error; + + result = PyDict_Keys(dict); + /* fall through */ +error: + Py_XDECREF(itsclass); + Py_XDECREF(dict); + return result; +} + static PyMethodDef object_methods[] = { {"__reduce_ex__", object_reduce_ex, METH_VARARGS, PyDoc_STR("helper for pickle")}, @@ -3435,6 +3584,8 @@ static PyMethodDef object_methods[] = { PyDoc_STR("default object formatter")}, {"__sizeof__", object_sizeof, METH_NOARGS, PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")}, + {"__dir__", object_dir, METH_NOARGS, + PyDoc_STR("__dir__() -> list\ndefault dir() implementation")}, {0} }; @@ -4675,8 +4826,7 @@ FUNCNAME(PyObject *self, PyObject *other) \ return call_maybe( \ other, ROPSTR, &rcache_str, "(O)", self); \ } \ - Py_INCREF(Py_NotImplemented); \ - return Py_NotImplemented; \ + Py_RETURN_NOTIMPLEMENTED; \ } #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ @@ -4853,8 +5003,7 @@ slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) return call_method(self, "__pow__", &pow_str, "(OO)", other, modulus); } - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } SLOT0(slot_nb_negative, "__neg__") @@ -4974,14 +5123,21 @@ slot_tp_str(PyObject *self) return res; } else { - PyObject *ress; + /* PyObject *ress; */ PyErr_Clear(); res = slot_tp_repr(self); if (!res) return NULL; - ress = _PyUnicode_AsDefaultEncodedString(res, NULL); + /* XXX this is non-sensical. Why should we return + a bytes object from __str__. Is this code even + used? - mvl */ + assert(0); + return res; + /* + ress = _PyUnicode_AsDefaultEncodedString(res); Py_DECREF(res); return ress; + */ } } @@ -5177,8 +5333,7 @@ slot_tp_richcompare(PyObject *self, PyObject *other, int op) func = lookup_method(self, name_op[op], &op_str[op]); if (func == NULL) { PyErr_Clear(); - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } args = PyTuple_Pack(1, other); if (args == NULL) @@ -6059,7 +6214,7 @@ super_getattro(PyObject *self, PyObject *name) /* We want __class__ to return the class of the super object (i.e. super, or a subclass), not the class of su->obj. */ skip = (PyUnicode_Check(name) && - PyUnicode_GET_SIZE(name) == 9 && + PyUnicode_GET_LENGTH(name) == 9 && PyUnicode_CompareWithASCIIString(name, "__class__") == 0); } @@ -6258,7 +6413,7 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds) PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); assert(PyUnicode_Check(name)); if (!PyUnicode_CompareWithASCIIString(name, - "__class__")) { + "@__class__")) { Py_ssize_t index = co->co_nlocals + PyTuple_GET_SIZE(co->co_cellvars) + i; PyObject *cell = f->f_localsplus[index]; diff --git a/Objects/typeslots.inc b/Objects/typeslots.inc index 5186dcf..caa1e03 100644 --- a/Objects/typeslots.inc +++ b/Objects/typeslots.inc @@ -1,4 +1,4 @@ -/* Generated by typeslots.py $Revision$ */ +/* Generated by typeslots.py */ 0, 0, offsetof(PyHeapTypeObject, as_mapping.mp_ass_subscript), diff --git a/Objects/typeslots.py b/Objects/typeslots.py index 2e00c80..b24c7f4 100644 --- a/Objects/typeslots.py +++ b/Objects/typeslots.py @@ -3,7 +3,7 @@ import sys, re -print("/* Generated by typeslots.py $Revision$ */") +print("/* Generated by typeslots.py */") res = {} for line in sys.stdin: m = re.match("#define Py_([a-z_]+) ([0-9]+)", line) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 7316abf..e904b6e 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1,8 +1,7 @@ /* Unicode implementation based on original code by Fredrik Lundh, -modified by Marc-Andre Lemburg <mal@lemburg.com> according to the -Unicode Integration Proposal (see file Misc/unicode.txt). +modified by Marc-Andre Lemburg <mal@lemburg.com>. Major speed upgrades to the method implementations at the Reykjavik NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. @@ -47,6 +46,10 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include <windows.h> #endif +#ifdef Py_DEBUG +# define DONT_MAKE_RESULT_READY +#endif + /* Limit for the Unicode object free list */ #define PyUnicode_MAXFREELIST 1024 @@ -90,6 +93,101 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif +#ifdef Py_DEBUG +# define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) +#else +# define _PyUnicode_CHECK(op) PyUnicode_Check(op) +#endif + +#define _PyUnicode_UTF8(op) \ + (((PyCompactUnicodeObject*)(op))->utf8) +#define PyUnicode_UTF8(op) \ + (assert(_PyUnicode_CHECK(op)), \ + assert(PyUnicode_IS_READY(op)), \ + PyUnicode_IS_COMPACT_ASCII(op) ? \ + ((char*)((PyASCIIObject*)(op) + 1)) : \ + _PyUnicode_UTF8(op)) +#define _PyUnicode_UTF8_LENGTH(op) \ + (((PyCompactUnicodeObject*)(op))->utf8_length) +#define PyUnicode_UTF8_LENGTH(op) \ + (assert(_PyUnicode_CHECK(op)), \ + assert(PyUnicode_IS_READY(op)), \ + PyUnicode_IS_COMPACT_ASCII(op) ? \ + ((PyASCIIObject*)(op))->length : \ + _PyUnicode_UTF8_LENGTH(op)) +#define _PyUnicode_WSTR(op) \ + (((PyASCIIObject*)(op))->wstr) +#define _PyUnicode_WSTR_LENGTH(op) \ + (((PyCompactUnicodeObject*)(op))->wstr_length) +#define _PyUnicode_LENGTH(op) \ + (((PyASCIIObject *)(op))->length) +#define _PyUnicode_STATE(op) \ + (((PyASCIIObject *)(op))->state) +#define _PyUnicode_HASH(op) \ + (((PyASCIIObject *)(op))->hash) +#define _PyUnicode_KIND(op) \ + (assert(_PyUnicode_CHECK(op)), \ + ((PyASCIIObject *)(op))->state.kind) +#define _PyUnicode_GET_LENGTH(op) \ + (assert(_PyUnicode_CHECK(op)), \ + ((PyASCIIObject *)(op))->length) +#define _PyUnicode_DATA_ANY(op) \ + (((PyUnicodeObject*)(op))->data.any) + +#undef PyUnicode_READY +#define PyUnicode_READY(op) \ + (assert(_PyUnicode_CHECK(op)), \ + (PyUnicode_IS_READY(op) ? \ + 0 : \ + _PyUnicode_Ready((PyObject *)(op)))) + +#define _PyUnicode_READY_REPLACE(p_obj) \ + (assert(_PyUnicode_CHECK(*p_obj)), \ + (PyUnicode_IS_READY(*p_obj) ? \ + 0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) + +#define _PyUnicode_SHARE_UTF8(op) \ + (assert(_PyUnicode_CHECK(op)), \ + assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ + (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) +#define _PyUnicode_SHARE_WSTR(op) \ + (assert(_PyUnicode_CHECK(op)), \ + (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) + +/* true if the Unicode object has an allocated UTF-8 memory block + (not shared with other data) */ +#define _PyUnicode_HAS_UTF8_MEMORY(op) \ + (assert(_PyUnicode_CHECK(op)), \ + (!PyUnicode_IS_COMPACT_ASCII(op) \ + && _PyUnicode_UTF8(op) \ + && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) + +/* true if the Unicode object has an allocated wstr memory block + (not shared with other data) */ +#define _PyUnicode_HAS_WSTR_MEMORY(op) \ + (assert(_PyUnicode_CHECK(op)), \ + (_PyUnicode_WSTR(op) && \ + (!PyUnicode_IS_READY(op) || \ + _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) + +/* Generic helper macro to convert characters of different types. + from_type and to_type have to be valid type names, begin and end + are pointers to the source characters which should be of type + "from_type *". to is a pointer of type "to_type *" and points to the + buffer where the result characters are written to. */ +#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ + do { \ + const from_type *iter_; to_type *to_; \ + for (iter_ = (begin), to_ = (to_type *)(to); \ + iter_ < (end); \ + ++iter_, ++to_) { \ + *to_ = (to_type)*iter_; \ + } \ + } while (0) + +/* The Unicode string has been modified: reset the hash */ +#define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) + /* This dictionary holds all interned unicode strings. Note that references to strings in this dictionary are *not* counted in the string's ob_refcnt. When the interned string reaches a refcnt of 0 the string deallocation @@ -100,16 +198,12 @@ extern "C" { */ static PyObject *interned; -/* Free list for Unicode objects */ -static PyUnicodeObject *free_list; -static int numfree; - /* The empty Unicode object is shared to improve performance. */ -static PyUnicodeObject *unicode_empty; +static PyObject *unicode_empty; /* Single character Unicode strings in the Latin-1 range are being shared as well. */ -static PyUnicodeObject *unicode_latin1[256]; +static PyObject *unicode_latin1[256]; /* Fast detection of the most frequent whitespace characters */ const unsigned char _Py_ascii_whitespace[] = { @@ -142,16 +236,29 @@ const unsigned char _Py_ascii_whitespace[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; -static PyObject *unicode_encode_call_errorhandler(const char *errors, +/* forward */ +static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); +static PyObject* get_latin1_char(unsigned char ch); +static void copy_characters( + PyObject *to, Py_ssize_t to_start, + PyObject *from, Py_ssize_t from_start, + Py_ssize_t how_many); +#ifdef Py_DEBUG +static int unicode_is_singleton(PyObject *unicode); +#endif + +static PyObject * +unicode_encode_call_errorhandler(const char *errors, PyObject **errorHandler,const char *encoding, const char *reason, const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); -static void raise_encode_exception(PyObject **exceptionObject, - const char *encoding, - const Py_UNICODE *unicode, Py_ssize_t size, - Py_ssize_t startpos, Py_ssize_t endpos, - const char *reason); +static void +raise_encode_exception(PyObject **exceptionObject, + const char *encoding, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason); /* Same for linebreaks */ static unsigned char ascii_linebreak[] = { @@ -181,7 +288,8 @@ static unsigned char ascii_linebreak[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - +/* The max unicode value is always 0x10FFFF while using the PEP-393 API. + This function is kept for backward compatibility with the old API. */ Py_UNICODE PyUnicode_GetMax(void) { @@ -194,6 +302,112 @@ PyUnicode_GetMax(void) #endif } +#ifdef Py_DEBUG +int +/* FIXME: use PyObject* type for op */ +_PyUnicode_CheckConsistency(void *op, int check_content) +{ + PyASCIIObject *ascii; + unsigned int kind; + + assert(PyUnicode_Check(op)); + + ascii = (PyASCIIObject *)op; + kind = ascii->state.kind; + + if (ascii->state.ascii == 1 && ascii->state.compact == 1) { + assert(kind == PyUnicode_1BYTE_KIND); + assert(ascii->state.ready == 1); + } + else { + PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; + void *data; + + if (ascii->state.compact == 1) { + data = compact + 1; + assert(kind == PyUnicode_1BYTE_KIND + || kind == PyUnicode_2BYTE_KIND + || kind == PyUnicode_4BYTE_KIND); + assert(ascii->state.ascii == 0); + assert(ascii->state.ready == 1); + assert (compact->utf8 != data); + } else { + PyUnicodeObject *unicode = (PyUnicodeObject *)op; + + data = unicode->data.any; + if (kind == PyUnicode_WCHAR_KIND) { + assert(ascii->state.compact == 0); + assert(ascii->state.ascii == 0); + assert(ascii->state.ready == 0); + assert(ascii->wstr != NULL); + assert(data == NULL); + assert(compact->utf8 == NULL); + assert(ascii->state.interned == SSTATE_NOT_INTERNED); + } + else { + assert(kind == PyUnicode_1BYTE_KIND + || kind == PyUnicode_2BYTE_KIND + || kind == PyUnicode_4BYTE_KIND); + assert(ascii->state.compact == 0); + assert(ascii->state.ready == 1); + assert(data != NULL); + if (ascii->state.ascii) { + assert (compact->utf8 == data); + assert (compact->utf8_length == ascii->length); + } + else + assert (compact->utf8 != data); + } + } + if (kind != PyUnicode_WCHAR_KIND) { + if ( +#if SIZEOF_WCHAR_T == 2 + kind == PyUnicode_2BYTE_KIND +#else + kind == PyUnicode_4BYTE_KIND +#endif + ) + { + assert(ascii->wstr == data); + assert(compact->wstr_length == ascii->length); + } else + assert(ascii->wstr != data); + } + + if (compact->utf8 == NULL) + assert(compact->utf8_length == 0); + if (ascii->wstr == NULL) + assert(compact->wstr_length == 0); + } + /* check that the best kind is used */ + if (check_content && kind != PyUnicode_WCHAR_KIND) + { + Py_ssize_t i; + Py_UCS4 maxchar = 0; + void *data = PyUnicode_DATA(ascii); + for (i=0; i < ascii->length; i++) + { + Py_UCS4 ch = PyUnicode_READ(kind, data, i); + if (ch > maxchar) + maxchar = ch; + } + if (kind == PyUnicode_1BYTE_KIND) { + if (ascii->state.ascii == 0) + assert(maxchar >= 128); + else + assert(maxchar < 128); + } + else if (kind == PyUnicode_2BYTE_KIND) + assert(maxchar >= 0x100); + else + assert(maxchar >= 0x10000); + } + if (check_content && !unicode_is_singleton((PyObject*)ascii)) + assert(ascii->hash == -1); + return 1; +} +#endif + /* --- Bloom Filters ----------------------------------------------------- */ /* stuff to implement simple "bloom filters" for Unicode characters. @@ -223,7 +437,8 @@ static BLOOM_MASK bloom_linebreak; ((ch) < 128U ? ascii_linebreak[(ch)] : \ (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) -Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) +Py_LOCAL_INLINE(BLOOM_MASK) +make_bloom_mask(int kind, void* ptr, Py_ssize_t len) { /* calculate simple bloom-style bitmask for a given unicode string */ @@ -232,76 +447,187 @@ Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) mask = 0; for (i = 0; i < len; i++) - BLOOM_ADD(mask, ptr[i]); + BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); return mask; } -Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) +#define BLOOM_MEMBER(mask, chr, str) \ + (BLOOM(mask, chr) \ + && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) + +/* --- Unicode Object ----------------------------------------------------- */ + +static PyObject * +fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); + +Py_LOCAL_INLINE(char *) findchar(void *s, int kind, + Py_ssize_t size, Py_UCS4 ch, + int direction) { + /* like wcschr, but doesn't stop at NULL characters */ Py_ssize_t i; + if (direction == 1) { + for(i = 0; i < size; i++) + if (PyUnicode_READ(kind, s, i) == ch) + return (char*)s + kind * i; + } + else { + for(i = size-1; i >= 0; i--) + if (PyUnicode_READ(kind, s, i) == ch) + return (char*)s + kind * i; + } + return NULL; +} - for (i = 0; i < setlen; i++) - if (set[i] == chr) - return 1; +static PyObject* +resize_compact(PyObject *unicode, Py_ssize_t length) +{ + Py_ssize_t char_size; + Py_ssize_t struct_size; + Py_ssize_t new_size; + int share_wstr; - return 0; -} + assert(PyUnicode_IS_READY(unicode)); + char_size = PyUnicode_KIND(unicode); + if (PyUnicode_IS_COMPACT_ASCII(unicode)) + struct_size = sizeof(PyASCIIObject); + else + struct_size = sizeof(PyCompactUnicodeObject); + share_wstr = _PyUnicode_SHARE_WSTR(unicode); -#define BLOOM_MEMBER(mask, chr, set, setlen) \ - BLOOM(mask, chr) && unicode_member(chr, set, setlen) + _Py_DEC_REFTOTAL; + _Py_ForgetReference(unicode); -/* --- Unicode Object ----------------------------------------------------- */ + if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { + PyErr_NoMemory(); + return NULL; + } + new_size = (struct_size + (length + 1) * char_size); -static -int unicode_resize(register PyUnicodeObject *unicode, - Py_ssize_t length) -{ - void *oldstr; + unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); + if (unicode == NULL) { + PyObject_Del(unicode); + PyErr_NoMemory(); + return NULL; + } + _Py_NewReference(unicode); + _PyUnicode_LENGTH(unicode) = length; + if (share_wstr) { + _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); + if (!PyUnicode_IS_COMPACT_ASCII(unicode)) + _PyUnicode_WSTR_LENGTH(unicode) = length; + } + PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), + length, 0); + return unicode; +} - /* Shortcut if there's nothing much to do. */ - if (unicode->length == length) - goto reset; +static int +resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) +{ + wchar_t *wstr; + assert(!PyUnicode_IS_COMPACT(unicode)); + assert(Py_REFCNT(unicode) == 1); + + _PyUnicode_DIRTY(unicode); + + if (PyUnicode_IS_READY(unicode)) { + Py_ssize_t char_size; + Py_ssize_t new_size; + int share_wstr, share_utf8; + void *data; + + data = _PyUnicode_DATA_ANY(unicode); + assert(data != NULL); + char_size = PyUnicode_KIND(unicode); + share_wstr = _PyUnicode_SHARE_WSTR(unicode); + share_utf8 = _PyUnicode_SHARE_UTF8(unicode); + if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) + { + PyObject_DEL(_PyUnicode_UTF8(unicode)); + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + } - /* Resizing shared object (unicode_empty or single character - objects) in-place is not allowed. Use PyUnicode_Resize() - instead ! */ + if (length > (PY_SSIZE_T_MAX / char_size - 1)) { + PyErr_NoMemory(); + return -1; + } + new_size = (length + 1) * char_size; - if (unicode == unicode_empty || - (unicode->length == 1 && - unicode->str[0] < 256U && - unicode_latin1[unicode->str[0]] == unicode)) { - PyErr_SetString(PyExc_SystemError, - "can't resize shared str objects"); - return -1; + data = (PyObject *)PyObject_REALLOC(data, new_size); + if (data == NULL) { + PyErr_NoMemory(); + return -1; + } + _PyUnicode_DATA_ANY(unicode) = data; + if (share_wstr) { + _PyUnicode_WSTR(unicode) = data; + _PyUnicode_WSTR_LENGTH(unicode) = length; + } + if (share_utf8) { + _PyUnicode_UTF8(unicode) = data; + _PyUnicode_UTF8_LENGTH(unicode) = length; + } + _PyUnicode_LENGTH(unicode) = length; + PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); + if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { + assert(_PyUnicode_CheckConsistency(unicode, 0)); + return 0; + } } + assert(_PyUnicode_WSTR(unicode) != NULL); - /* We allocate one more byte to make sure the string is Ux0000 terminated. - The overallocation is also used by fastsearch, which assumes that it's - safe to look at str[length] (without making any assumptions about what - it contains). */ - - oldstr = unicode->str; - unicode->str = PyObject_REALLOC(unicode->str, - sizeof(Py_UNICODE) * (length + 1)); - if (!unicode->str) { - unicode->str = (Py_UNICODE *)oldstr; + /* check for integer overflow */ + if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { PyErr_NoMemory(); return -1; } - unicode->str[length] = 0; - unicode->length = length; - - reset: - /* Reset the object caches */ - if (unicode->defenc) { - Py_CLEAR(unicode->defenc); + wstr = _PyUnicode_WSTR(unicode); + wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); + if (!wstr) { + PyErr_NoMemory(); + return -1; } - unicode->hash = -1; - + _PyUnicode_WSTR(unicode) = wstr; + _PyUnicode_WSTR(unicode)[length] = 0; + _PyUnicode_WSTR_LENGTH(unicode) = length; + assert(_PyUnicode_CheckConsistency(unicode, 0)); return 0; } +static PyObject* +resize_copy(PyObject *unicode, Py_ssize_t length) +{ + Py_ssize_t copy_length; + if (PyUnicode_IS_COMPACT(unicode)) { + PyObject *copy; + assert(PyUnicode_IS_READY(unicode)); + + copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); + if (copy == NULL) + return NULL; + + copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); + copy_characters(copy, 0, unicode, 0, copy_length); + return copy; + } + else { + PyUnicodeObject *w; + assert(_PyUnicode_WSTR(unicode) != NULL); + assert(_PyUnicode_DATA_ANY(unicode) == NULL); + w = _PyUnicode_New(length); + if (w == NULL) + return NULL; + copy_length = _PyUnicode_WSTR_LENGTH(unicode); + copy_length = Py_MIN(copy_length, length); + Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), + copy_length); + return (PyObject*)w; + } +} + /* We allocate one more byte to make sure the string is Ux0000 terminated; some code (e.g. new_identifier) relies on that. @@ -311,55 +637,46 @@ int unicode_resize(register PyUnicodeObject *unicode, */ -static -PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) +#ifdef Py_DEBUG +int unicode_old_new_calls = 0; +#endif + +static PyUnicodeObject * +_PyUnicode_New(Py_ssize_t length) { register PyUnicodeObject *unicode; + size_t new_size; /* Optimization for empty strings */ if (length == 0 && unicode_empty != NULL) { Py_INCREF(unicode_empty); - return unicode_empty; + return (PyUnicodeObject*)unicode_empty; } /* Ensure we won't overflow the size. */ if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { return (PyUnicodeObject *)PyErr_NoMemory(); } - - /* Unicode freelist & memory allocation */ - if (free_list) { - unicode = free_list; - free_list = *(PyUnicodeObject **)unicode; - numfree--; - if (unicode->str) { - /* Keep-Alive optimization: we only upsize the buffer, - never downsize it. */ - if ((unicode->length < length) && - unicode_resize(unicode, length) < 0) { - PyObject_DEL(unicode->str); - unicode->str = NULL; - } - } - else { - size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); - unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); - } - PyObject_INIT(unicode, &PyUnicode_Type); - } - else { - size_t new_size; - unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); - if (unicode == NULL) - return NULL; - new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); - unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); + if (length < 0) { + PyErr_SetString(PyExc_SystemError, + "Negative size passed to _PyUnicode_New"); + return NULL; } - if (!unicode->str) { +#ifdef Py_DEBUG + ++unicode_old_new_calls; +#endif + + unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); + if (unicode == NULL) + return NULL; + new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); + _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); + if (!_PyUnicode_WSTR(unicode)) { PyErr_NoMemory(); goto onError; } + /* Initialize the first element to guard against cases where * the caller fails before initializing str -- unicode_resize() * reads str[0], and the Keep-Alive optimization can keep memory @@ -367,12 +684,19 @@ PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) * We don't want unicode_resize to read uninitialized memory in * that case. */ - unicode->str[0] = 0; - unicode->str[length] = 0; - unicode->length = length; - unicode->hash = -1; - unicode->state = 0; - unicode->defenc = NULL; + _PyUnicode_WSTR(unicode)[0] = 0; + _PyUnicode_WSTR(unicode)[length] = 0; + _PyUnicode_WSTR_LENGTH(unicode) = length; + _PyUnicode_HASH(unicode) = -1; + _PyUnicode_STATE(unicode).interned = 0; + _PyUnicode_STATE(unicode).kind = 0; + _PyUnicode_STATE(unicode).compact = 0; + _PyUnicode_STATE(unicode).ready = 0; + _PyUnicode_STATE(unicode).ascii = 0; + _PyUnicode_DATA_ANY(unicode) = NULL; + _PyUnicode_LENGTH(unicode) = 0; + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; return unicode; onError: @@ -383,8 +707,653 @@ PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) return NULL; } -static -void unicode_dealloc(register PyUnicodeObject *unicode) +static const char* +unicode_kind_name(PyObject *unicode) +{ + /* don't check consistency: unicode_kind_name() is called from + _PyUnicode_Dump() */ + if (!PyUnicode_IS_COMPACT(unicode)) + { + if (!PyUnicode_IS_READY(unicode)) + return "wstr"; + switch(PyUnicode_KIND(unicode)) + { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(unicode)) + return "legacy ascii"; + else + return "legacy latin1"; + case PyUnicode_2BYTE_KIND: + return "legacy UCS2"; + case PyUnicode_4BYTE_KIND: + return "legacy UCS4"; + default: + return "<legacy invalid kind>"; + } + } + assert(PyUnicode_IS_READY(unicode)); + switch(PyUnicode_KIND(unicode)) + { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(unicode)) + return "ascii"; + else + return "latin1"; + case PyUnicode_2BYTE_KIND: + return "UCS2"; + case PyUnicode_4BYTE_KIND: + return "UCS4"; + default: + return "<invalid compact kind>"; + } +} + +#ifdef Py_DEBUG +int unicode_new_new_calls = 0; + +/* Functions wrapping macros for use in debugger */ +char *_PyUnicode_utf8(void *unicode){ + return PyUnicode_UTF8(unicode); +} + +void *_PyUnicode_compact_data(void *unicode) { + return _PyUnicode_COMPACT_DATA(unicode); +} +void *_PyUnicode_data(void *unicode){ + printf("obj %p\n", unicode); + printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); + printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); + printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); + printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); + printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); + return PyUnicode_DATA(unicode); +} + +void +_PyUnicode_Dump(PyObject *op) +{ + PyASCIIObject *ascii = (PyASCIIObject *)op; + PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; + PyUnicodeObject *unicode = (PyUnicodeObject *)op; + void *data; + printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); + if (ascii->state.compact) + data = (compact + 1); + else + data = unicode->data.any; + if (ascii->wstr == data) + printf("shared "); + printf("wstr=%p", ascii->wstr); + if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { + printf(" (%zu), ", compact->wstr_length); + if (!ascii->state.compact && compact->utf8 == unicode->data.any) + printf("shared "); + printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); + } + printf(", data=%p\n", data); +} +#endif + +PyObject * +PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) +{ + PyObject *obj; + PyCompactUnicodeObject *unicode; + void *data; + int kind_state; + int is_sharing, is_ascii; + Py_ssize_t char_size; + Py_ssize_t struct_size; + + /* Optimization for empty strings */ + if (size == 0 && unicode_empty != NULL) { + Py_INCREF(unicode_empty); + return unicode_empty; + } + +#ifdef Py_DEBUG + ++unicode_new_new_calls; +#endif + + is_ascii = 0; + is_sharing = 0; + struct_size = sizeof(PyCompactUnicodeObject); + if (maxchar < 128) { + kind_state = PyUnicode_1BYTE_KIND; + char_size = 1; + is_ascii = 1; + struct_size = sizeof(PyASCIIObject); + } + else if (maxchar < 256) { + kind_state = PyUnicode_1BYTE_KIND; + char_size = 1; + } + else if (maxchar < 65536) { + kind_state = PyUnicode_2BYTE_KIND; + char_size = 2; + if (sizeof(wchar_t) == 2) + is_sharing = 1; + } + else { + kind_state = PyUnicode_4BYTE_KIND; + char_size = 4; + if (sizeof(wchar_t) == 4) + is_sharing = 1; + } + + /* Ensure we won't overflow the size. */ + if (size < 0) { + PyErr_SetString(PyExc_SystemError, + "Negative size passed to PyUnicode_New"); + return NULL; + } + if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) + return PyErr_NoMemory(); + + /* Duplicated allocation code from _PyObject_New() instead of a call to + * PyObject_New() so we are able to allocate space for the object and + * it's data buffer. + */ + obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); + if (obj == NULL) + return PyErr_NoMemory(); + obj = PyObject_INIT(obj, &PyUnicode_Type); + if (obj == NULL) + return NULL; + + unicode = (PyCompactUnicodeObject *)obj; + if (is_ascii) + data = ((PyASCIIObject*)obj) + 1; + else + data = unicode + 1; + _PyUnicode_LENGTH(unicode) = size; + _PyUnicode_HASH(unicode) = -1; + _PyUnicode_STATE(unicode).interned = 0; + _PyUnicode_STATE(unicode).kind = kind_state; + _PyUnicode_STATE(unicode).compact = 1; + _PyUnicode_STATE(unicode).ready = 1; + _PyUnicode_STATE(unicode).ascii = is_ascii; + if (is_ascii) { + ((char*)data)[size] = 0; + _PyUnicode_WSTR(unicode) = NULL; + } + else if (kind_state == PyUnicode_1BYTE_KIND) { + ((char*)data)[size] = 0; + _PyUnicode_WSTR(unicode) = NULL; + _PyUnicode_WSTR_LENGTH(unicode) = 0; + unicode->utf8 = NULL; + unicode->utf8_length = 0; + } + else { + unicode->utf8 = NULL; + unicode->utf8_length = 0; + if (kind_state == PyUnicode_2BYTE_KIND) + ((Py_UCS2*)data)[size] = 0; + else /* kind_state == PyUnicode_4BYTE_KIND */ + ((Py_UCS4*)data)[size] = 0; + if (is_sharing) { + _PyUnicode_WSTR_LENGTH(unicode) = size; + _PyUnicode_WSTR(unicode) = (wchar_t *)data; + } + else { + _PyUnicode_WSTR_LENGTH(unicode) = 0; + _PyUnicode_WSTR(unicode) = NULL; + } + } + assert(_PyUnicode_CheckConsistency(unicode, 0)); + return obj; +} + +#if SIZEOF_WCHAR_T == 2 +/* Helper function to convert a 16-bits wchar_t representation to UCS4, this + will decode surrogate pairs, the other conversions are implemented as macros + for efficiency. + + This function assumes that unicode can hold one more code point than wstr + characters for a terminating null character. */ +static void +unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, + PyUnicodeObject *unicode) +{ + const wchar_t *iter; + Py_UCS4 *ucs4_out; + + assert(unicode != NULL); + assert(_PyUnicode_CHECK(unicode)); + assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); + ucs4_out = PyUnicode_4BYTE_DATA(unicode); + + for (iter = begin; iter < end; ) { + assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + + _PyUnicode_GET_LENGTH(unicode))); + if (*iter >= 0xD800 && *iter <= 0xDBFF + && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) + { + *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; + iter += 2; + } + else { + *ucs4_out++ = *iter; + iter++; + } + } + assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + + _PyUnicode_GET_LENGTH(unicode))); + +} +#endif + +static int +_PyUnicode_Dirty(PyObject *unicode) +{ + assert(_PyUnicode_CHECK(unicode)); + if (Py_REFCNT(unicode) != 1) { + PyErr_SetString(PyExc_SystemError, + "Cannot modify a string having more than 1 reference"); + return -1; + } + _PyUnicode_DIRTY(unicode); + return 0; +} + +static int +_copy_characters(PyObject *to, Py_ssize_t to_start, + PyObject *from, Py_ssize_t from_start, + Py_ssize_t how_many, int check_maxchar) +{ + unsigned int from_kind, to_kind; + void *from_data, *to_data; + int fast; + + assert(PyUnicode_Check(from)); + assert(PyUnicode_Check(to)); + assert(PyUnicode_IS_READY(from)); + assert(PyUnicode_IS_READY(to)); + + assert(PyUnicode_GET_LENGTH(from) >= how_many); + assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); + assert(0 <= how_many); + + if (how_many == 0) + return 0; + + from_kind = PyUnicode_KIND(from); + from_data = PyUnicode_DATA(from); + to_kind = PyUnicode_KIND(to); + to_data = PyUnicode_DATA(to); + +#ifdef Py_DEBUG + if (!check_maxchar + && (from_kind > to_kind + || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) + { + const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); + Py_UCS4 ch; + Py_ssize_t i; + for (i=0; i < how_many; i++) { + ch = PyUnicode_READ(from_kind, from_data, from_start + i); + assert(ch <= to_maxchar); + } + } +#endif + fast = (from_kind == to_kind); + if (check_maxchar + && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) + { + /* deny latin1 => ascii */ + fast = 0; + } + + if (fast) { + Py_MEMCPY((char*)to_data + to_kind * to_start, + (char*)from_data + from_kind * from_start, + to_kind * how_many); + } + else if (from_kind == PyUnicode_1BYTE_KIND + && to_kind == PyUnicode_2BYTE_KIND) + { + _PyUnicode_CONVERT_BYTES( + Py_UCS1, Py_UCS2, + PyUnicode_1BYTE_DATA(from) + from_start, + PyUnicode_1BYTE_DATA(from) + from_start + how_many, + PyUnicode_2BYTE_DATA(to) + to_start + ); + } + else if (from_kind == PyUnicode_1BYTE_KIND + && to_kind == PyUnicode_4BYTE_KIND) + { + _PyUnicode_CONVERT_BYTES( + Py_UCS1, Py_UCS4, + PyUnicode_1BYTE_DATA(from) + from_start, + PyUnicode_1BYTE_DATA(from) + from_start + how_many, + PyUnicode_4BYTE_DATA(to) + to_start + ); + } + else if (from_kind == PyUnicode_2BYTE_KIND + && to_kind == PyUnicode_4BYTE_KIND) + { + _PyUnicode_CONVERT_BYTES( + Py_UCS2, Py_UCS4, + PyUnicode_2BYTE_DATA(from) + from_start, + PyUnicode_2BYTE_DATA(from) + from_start + how_many, + PyUnicode_4BYTE_DATA(to) + to_start + ); + } + else { + /* check if max_char(from substring) <= max_char(to) */ + if (from_kind > to_kind + /* latin1 => ascii */ + || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) + { + /* slow path to check for character overflow */ + const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); + Py_UCS4 ch; + Py_ssize_t i; + +#ifdef Py_DEBUG + for (i=0; i < how_many; i++) { + ch = PyUnicode_READ(from_kind, from_data, from_start + i); + assert(ch <= to_maxchar); + PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); + } +#else + if (!check_maxchar) { + for (i=0; i < how_many; i++) { + ch = PyUnicode_READ(from_kind, from_data, from_start + i); + PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); + } + } + else { + for (i=0; i < how_many; i++) { + ch = PyUnicode_READ(from_kind, from_data, from_start + i); + if (ch > to_maxchar) + return 1; + PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); + } + } +#endif + } + else { + assert(0 && "inconsistent state"); + return 1; + } + } + return 0; +} + +static void +copy_characters(PyObject *to, Py_ssize_t to_start, + PyObject *from, Py_ssize_t from_start, + Py_ssize_t how_many) +{ + (void)_copy_characters(to, to_start, from, from_start, how_many, 0); +} + +Py_ssize_t +PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, + PyObject *from, Py_ssize_t from_start, + Py_ssize_t how_many) +{ + int err; + + if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { + PyErr_BadInternalCall(); + return -1; + } + + if (PyUnicode_READY(from)) + return -1; + if (PyUnicode_READY(to)) + return -1; + + how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); + if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { + PyErr_Format(PyExc_SystemError, + "Cannot write %zi characters at %zi " + "in a string of %zi characters", + how_many, to_start, PyUnicode_GET_LENGTH(to)); + return -1; + } + + if (how_many == 0) + return 0; + + if (_PyUnicode_Dirty(to)) + return -1; + + err = _copy_characters(to, to_start, from, from_start, how_many, 1); + if (err) { + PyErr_Format(PyExc_SystemError, + "Cannot copy %s characters " + "into a string of %s characters", + unicode_kind_name(from), + unicode_kind_name(to)); + return -1; + } + return how_many; +} + +/* Find the maximum code point and count the number of surrogate pairs so a + correct string length can be computed before converting a string to UCS4. + This function counts single surrogates as a character and not as a pair. + + Return 0 on success, or -1 on error. */ +static int +find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, + Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) +{ + const wchar_t *iter; + + assert(num_surrogates != NULL && maxchar != NULL); + *num_surrogates = 0; + *maxchar = 0; + + for (iter = begin; iter < end; ) { + if (*iter > *maxchar) { + *maxchar = *iter; +#if SIZEOF_WCHAR_T != 2 + if (*maxchar >= 0x10000) + return 0; +#endif + } +#if SIZEOF_WCHAR_T == 2 + if (*iter >= 0xD800 && *iter <= 0xDBFF + && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) + { + Py_UCS4 surrogate_val; + surrogate_val = (((iter[0] & 0x3FF)<<10) + | (iter[1] & 0x3FF)) + 0x10000; + ++(*num_surrogates); + if (surrogate_val > *maxchar) + *maxchar = surrogate_val; + iter += 2; + } + else + iter++; +#else + iter++; +#endif + } + return 0; +} + +#ifdef Py_DEBUG +int unicode_ready_calls = 0; +#endif + +static int +unicode_ready(PyObject **p_obj, int replace) +{ + PyUnicodeObject *unicode; + wchar_t *end; + Py_UCS4 maxchar = 0; + Py_ssize_t num_surrogates; +#if SIZEOF_WCHAR_T == 2 + Py_ssize_t length_wo_surrogates; +#endif + + assert(p_obj != NULL); + unicode = (PyUnicodeObject *)*p_obj; + + /* _PyUnicode_Ready() is only intended for old-style API usage where + strings were created using _PyObject_New() and where no canonical + representation (the str field) has been set yet aka strings + which are not yet ready. */ + assert(_PyUnicode_CHECK(unicode)); + assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); + assert(_PyUnicode_WSTR(unicode) != NULL); + assert(_PyUnicode_DATA_ANY(unicode) == NULL); + assert(_PyUnicode_UTF8(unicode) == NULL); + /* Actually, it should neither be interned nor be anything else: */ + assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); + +#ifdef Py_DEBUG + ++unicode_ready_calls; +#endif + +#ifdef Py_DEBUG + assert(!replace || Py_REFCNT(unicode) == 1); +#else + if (replace && Py_REFCNT(unicode) != 1) + replace = 0; +#endif + if (replace) { + Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); + wchar_t *wstr = _PyUnicode_WSTR(unicode); + /* Optimization for empty strings */ + if (len == 0) { + Py_INCREF(unicode_empty); + Py_DECREF(*p_obj); + *p_obj = unicode_empty; + return 0; + } + if (len == 1 && wstr[0] < 256) { + PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); + if (latin1_char == NULL) + return -1; + Py_DECREF(*p_obj); + *p_obj = latin1_char; + return 0; + } + } + + end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); + if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, + &maxchar, &num_surrogates) == -1) + return -1; + + if (maxchar < 256) { + _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); + if (!_PyUnicode_DATA_ANY(unicode)) { + PyErr_NoMemory(); + return -1; + } + _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, + _PyUnicode_WSTR(unicode), end, + PyUnicode_1BYTE_DATA(unicode)); + PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; + _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); + _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; + if (maxchar < 128) { + _PyUnicode_STATE(unicode).ascii = 1; + _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); + _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); + } + else { + _PyUnicode_STATE(unicode).ascii = 0; + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + } + PyObject_FREE(_PyUnicode_WSTR(unicode)); + _PyUnicode_WSTR(unicode) = NULL; + _PyUnicode_WSTR_LENGTH(unicode) = 0; + } + /* In this case we might have to convert down from 4-byte native + wchar_t to 2-byte unicode. */ + else if (maxchar < 65536) { + assert(num_surrogates == 0 && + "FindMaxCharAndNumSurrogatePairs() messed up"); + +#if SIZEOF_WCHAR_T == 2 + /* We can share representations and are done. */ + _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); + PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; + _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); + _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; +#else + /* sizeof(wchar_t) == 4 */ + _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( + 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); + if (!_PyUnicode_DATA_ANY(unicode)) { + PyErr_NoMemory(); + return -1; + } + _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, + _PyUnicode_WSTR(unicode), end, + PyUnicode_2BYTE_DATA(unicode)); + PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; + _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); + _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + PyObject_FREE(_PyUnicode_WSTR(unicode)); + _PyUnicode_WSTR(unicode) = NULL; + _PyUnicode_WSTR_LENGTH(unicode) = 0; +#endif + } + /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ + else { +#if SIZEOF_WCHAR_T == 2 + /* in case the native representation is 2-bytes, we need to allocate a + new normalized 4-byte version. */ + length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; + _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); + if (!_PyUnicode_DATA_ANY(unicode)) { + PyErr_NoMemory(); + return -1; + } + _PyUnicode_LENGTH(unicode) = length_wo_surrogates; + _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + /* unicode_convert_wchar_to_ucs4() requires a ready string */ + _PyUnicode_STATE(unicode).ready = 1; + unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); + PyObject_FREE(_PyUnicode_WSTR(unicode)); + _PyUnicode_WSTR(unicode) = NULL; + _PyUnicode_WSTR_LENGTH(unicode) = 0; +#else + assert(num_surrogates == 0); + + _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); + _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; +#endif + PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; + } + _PyUnicode_STATE(unicode).ready = 1; + assert(_PyUnicode_CheckConsistency(unicode, 1)); + return 0; +} + +int +_PyUnicode_ReadyReplace(PyObject **op) +{ + return unicode_ready(op, 1); +} + +int +_PyUnicode_Ready(PyObject *op) +{ + return unicode_ready(&op, 0); +} + +static void +unicode_dealloc(register PyUnicodeObject *unicode) { switch (PyUnicode_CHECK_INTERNED(unicode)) { case SSTATE_NOT_INTERNED: @@ -405,113 +1374,193 @@ void unicode_dealloc(register PyUnicodeObject *unicode) Py_FatalError("Inconsistent interned string state."); } - if (PyUnicode_CheckExact(unicode) && - numfree < PyUnicode_MAXFREELIST) { - /* Keep-Alive optimization */ - if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { - PyObject_DEL(unicode->str); - unicode->str = NULL; - unicode->length = 0; - } - if (unicode->defenc) { - Py_CLEAR(unicode->defenc); - } - /* Add to free list */ - *(PyUnicodeObject **)unicode = free_list; - free_list = unicode; - numfree++; + if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) + PyObject_DEL(_PyUnicode_WSTR(unicode)); + if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) + PyObject_DEL(_PyUnicode_UTF8(unicode)); + + if (PyUnicode_IS_COMPACT(unicode)) { + Py_TYPE(unicode)->tp_free((PyObject *)unicode); } else { - PyObject_DEL(unicode->str); - Py_XDECREF(unicode->defenc); + if (_PyUnicode_DATA_ANY(unicode)) + PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); Py_TYPE(unicode)->tp_free((PyObject *)unicode); } } -static -int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) +#ifdef Py_DEBUG +static int +unicode_is_singleton(PyObject *unicode) { - register PyUnicodeObject *v; - - /* Argument checks */ - if (unicode == NULL) { - PyErr_BadInternalCall(); - return -1; + PyASCIIObject *ascii = (PyASCIIObject *)unicode; + if (unicode == unicode_empty) + return 1; + if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) + { + Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); + if (ch < 256 && unicode_latin1[ch] == unicode) + return 1; } - v = *unicode; - if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { - PyErr_BadInternalCall(); - return -1; + return 0; +} +#endif + +static int +unicode_resizable(PyObject *unicode) +{ + if (Py_REFCNT(unicode) != 1) + return 0; + if (PyUnicode_CHECK_INTERNED(unicode)) + return 0; +#ifdef Py_DEBUG + /* singleton refcount is greater than 1 */ + assert(!unicode_is_singleton(unicode)); +#endif + return 1; +} + +static int +unicode_resize(PyObject **p_unicode, Py_ssize_t length) +{ + PyObject *unicode; + Py_ssize_t old_length; + + assert(p_unicode != NULL); + unicode = *p_unicode; + + assert(unicode != NULL); + assert(PyUnicode_Check(unicode)); + assert(0 <= length); + + if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) + old_length = PyUnicode_WSTR_LENGTH(unicode); + else + old_length = PyUnicode_GET_LENGTH(unicode); + if (old_length == length) + return 0; + + if (!unicode_resizable(unicode)) { + PyObject *copy = resize_copy(unicode, length); + if (copy == NULL) + return -1; + Py_DECREF(*p_unicode); + *p_unicode = copy; + return 0; } - /* Resizing unicode_empty and single character objects is not - possible since these are being shared. We simply return a fresh - copy with the same Unicode content. */ - if (v->length != length && - (v == unicode_empty || v->length == 1)) { - PyUnicodeObject *w = _PyUnicode_New(length); - if (w == NULL) + if (PyUnicode_IS_COMPACT(unicode)) { + *p_unicode = resize_compact(unicode, length); + if (*p_unicode == NULL) return -1; - Py_UNICODE_COPY(w->str, v->str, - length < v->length ? length : v->length); - Py_DECREF(*unicode); - *unicode = w; + assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); return 0; } + return resize_inplace((PyUnicodeObject*)unicode, length); +} - /* Note that we don't have to modify *unicode for unshared Unicode - objects, since we can modify them in-place. */ - return unicode_resize(v, length); +int +PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) +{ + PyObject *unicode; + if (p_unicode == NULL) { + PyErr_BadInternalCall(); + return -1; + } + unicode = *p_unicode; + if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 + || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) + { + PyErr_BadInternalCall(); + return -1; + } + return unicode_resize(p_unicode, length); } -int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) +static PyObject* +get_latin1_char(unsigned char ch) { - return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); + PyObject *unicode = unicode_latin1[ch]; + if (!unicode) { + unicode = PyUnicode_New(1, ch); + if (!unicode) + return NULL; + PyUnicode_1BYTE_DATA(unicode)[0] = ch; + assert(_PyUnicode_CheckConsistency(unicode, 1)); + unicode_latin1[ch] = unicode; + } + Py_INCREF(unicode); + return unicode; } -PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, - Py_ssize_t size) +PyObject * +PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) { PyUnicodeObject *unicode; + Py_UCS4 maxchar = 0; + Py_ssize_t num_surrogates; + + if (u == NULL) + return (PyObject*)_PyUnicode_New(size); /* If the Unicode data is known at construction time, we can apply some optimizations which share commonly used objects. */ - if (u != NULL) { - /* Optimization for empty strings */ - if (size == 0 && unicode_empty != NULL) { - Py_INCREF(unicode_empty); - return (PyObject *)unicode_empty; - } - - /* Single character Unicode objects in the Latin-1 range are - shared when using this constructor */ - if (size == 1 && *u < 256) { - unicode = unicode_latin1[*u]; - if (!unicode) { - unicode = _PyUnicode_New(1); - if (!unicode) - return NULL; - unicode->str[0] = *u; - unicode_latin1[*u] = unicode; - } - Py_INCREF(unicode); - return (PyObject *)unicode; - } + /* Optimization for empty strings */ + if (size == 0 && unicode_empty != NULL) { + Py_INCREF(unicode_empty); + return unicode_empty; } - unicode = _PyUnicode_New(size); + /* Single character Unicode objects in the Latin-1 range are + shared when using this constructor */ + if (size == 1 && *u < 256) + return get_latin1_char((unsigned char)*u); + + /* If not empty and not single character, copy the Unicode data + into the new object */ + if (find_maxchar_surrogates(u, u + size, + &maxchar, &num_surrogates) == -1) + return NULL; + + unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, + maxchar); if (!unicode) return NULL; - /* Copy the Unicode data into the new object */ - if (u != NULL) - Py_UNICODE_COPY(unicode->str, u, size); + switch (PyUnicode_KIND(unicode)) { + case PyUnicode_1BYTE_KIND: + _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, + u, u + size, PyUnicode_1BYTE_DATA(unicode)); + break; + case PyUnicode_2BYTE_KIND: +#if Py_UNICODE_SIZE == 2 + Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); +#else + _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, + u, u + size, PyUnicode_2BYTE_DATA(unicode)); +#endif + break; + case PyUnicode_4BYTE_KIND: +#if SIZEOF_WCHAR_T == 2 + /* This is the only case which has to process surrogates, thus + a simple copy loop is not enough and we need a function. */ + unicode_convert_wchar_to_ucs4(u, u + size, unicode); +#else + assert(num_surrogates == 0); + Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); +#endif + break; + default: + assert(0 && "Impossible state"); + } + assert(_PyUnicode_CheckConsistency(unicode, 1)); return (PyObject *)unicode; } -PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) +PyObject * +PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) { PyUnicodeObject *unicode; @@ -530,23 +1579,13 @@ PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) /* Optimization for empty strings */ if (size == 0 && unicode_empty != NULL) { Py_INCREF(unicode_empty); - return (PyObject *)unicode_empty; + return unicode_empty; } /* Single characters are shared when using this constructor. Restrict to ASCII, since the input must be UTF-8. */ - if (size == 1 && Py_CHARMASK(*u) < 128) { - unicode = unicode_latin1[Py_CHARMASK(*u)]; - if (!unicode) { - unicode = _PyUnicode_New(1); - if (!unicode) - return NULL; - unicode->str[0] = Py_CHARMASK(*u); - unicode_latin1[Py_CHARMASK(*u)] = unicode; - } - Py_INCREF(unicode); - return (PyObject *)unicode; - } + if (size == 1 && Py_CHARMASK(*u) < 128) + return get_latin1_char(Py_CHARMASK(*u)); return PyUnicode_DecodeUTF8(u, size, NULL); } @@ -558,7 +1597,8 @@ PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) return (PyObject *)unicode; } -PyObject *PyUnicode_FromString(const char *u) +PyObject * +PyUnicode_FromString(const char *u) { size_t size = strlen(u); if (size > PY_SSIZE_T_MAX) { @@ -569,76 +1609,373 @@ PyObject *PyUnicode_FromString(const char *u) return PyUnicode_FromStringAndSize(u, size); } -#ifdef HAVE_WCHAR_H - -#if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) -# define CONVERT_WCHAR_TO_SURROGATES +static PyObject* +unicode_fromascii(const unsigned char* s, Py_ssize_t size) +{ + PyObject *res; +#ifdef Py_DEBUG + const unsigned char *p; + const unsigned char *end = s + size; + for (p=s; p < end; p++) { + assert(*p < 128); + } #endif + if (size == 1) + return get_latin1_char(s[0]); + res = PyUnicode_New(size, 127); + if (!res) + return NULL; + memcpy(PyUnicode_1BYTE_DATA(res), s, size); + return res; +} -#ifdef CONVERT_WCHAR_TO_SURROGATES +static Py_UCS4 +kind_maxchar_limit(unsigned int kind) +{ + switch(kind) { + case PyUnicode_1BYTE_KIND: + return 0x80; + case PyUnicode_2BYTE_KIND: + return 0x100; + case PyUnicode_4BYTE_KIND: + return 0x10000; + default: + assert(0 && "invalid kind"); + return 0x10ffff; + } +} -/* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need - to convert from UTF32 to UTF16. */ +static PyObject* +_PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) +{ + PyObject *res; + unsigned char max_char = 127; + Py_ssize_t i; -PyObject *PyUnicode_FromWideChar(register const wchar_t *w, - Py_ssize_t size) + assert(size >= 0); + if (size == 1) + return get_latin1_char(u[0]); + for (i = 0; i < size; i++) { + if (u[i] & 0x80) { + max_char = 255; + break; + } + } + res = PyUnicode_New(size, max_char); + if (!res) + return NULL; + memcpy(PyUnicode_1BYTE_DATA(res), u, size); + assert(_PyUnicode_CheckConsistency(res, 1)); + return res; +} + +static PyObject* +_PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) { - PyUnicodeObject *unicode; - register Py_ssize_t i; - Py_ssize_t alloc; - const wchar_t *orig_w; + PyObject *res; + Py_UCS2 max_char = 0; + Py_ssize_t i; - if (w == NULL) { - if (size == 0) - return PyUnicode_FromStringAndSize(NULL, 0); - PyErr_BadInternalCall(); + assert(size >= 0); + if (size == 1 && u[0] < 256) + return get_latin1_char(u[0]); + for (i = 0; i < size; i++) { + if (u[i] > max_char) { + max_char = u[i]; + if (max_char >= 256) + break; + } + } + res = PyUnicode_New(size, max_char); + if (!res) return NULL; + if (max_char >= 256) + memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); + else + for (i = 0; i < size; i++) + PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; + assert(_PyUnicode_CheckConsistency(res, 1)); + return res; +} + +static PyObject* +_PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) +{ + PyObject *res; + Py_UCS4 max_char = 0; + Py_ssize_t i; + + assert(size >= 0); + if (size == 1 && u[0] < 256) + return get_latin1_char(u[0]); + for (i = 0; i < size; i++) { + if (u[i] > max_char) { + max_char = u[i]; + if (max_char >= 0x10000) + break; + } + } + res = PyUnicode_New(size, max_char); + if (!res) + return NULL; + if (max_char >= 0x10000) + memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); + else { + int kind = PyUnicode_KIND(res); + void *data = PyUnicode_DATA(res); + for (i = 0; i < size; i++) + PyUnicode_WRITE(kind, data, i, u[i]); } + assert(_PyUnicode_CheckConsistency(res, 1)); + return res; +} - if (size == -1) { - size = wcslen(w); +PyObject* +PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) +{ + switch(kind) { + case PyUnicode_1BYTE_KIND: + return _PyUnicode_FromUCS1(buffer, size); + case PyUnicode_2BYTE_KIND: + return _PyUnicode_FromUCS2(buffer, size); + case PyUnicode_4BYTE_KIND: + return _PyUnicode_FromUCS4(buffer, size); + default: + assert(0 && "invalid kind"); + PyErr_SetString(PyExc_SystemError, "invalid kind"); + return NULL; } +} + +/* Ensure that a string uses the most efficient storage, if it is not the + case: create a new string with of the right kind. Write NULL into *p_unicode + on error. */ +void +unicode_adjust_maxchar(PyObject **p_unicode) +{ + PyObject *unicode, *copy; + Py_UCS4 max_char; + Py_ssize_t i, len; + unsigned int kind; + + assert(p_unicode != NULL); + unicode = *p_unicode; + assert(PyUnicode_IS_READY(unicode)); + if (PyUnicode_IS_ASCII(unicode)) + return; - alloc = size; - orig_w = w; - for (i = size; i > 0; i--) { - if (*w > 0xFFFF) - alloc++; - w++; + len = PyUnicode_GET_LENGTH(unicode); + kind = PyUnicode_KIND(unicode); + if (kind == PyUnicode_1BYTE_KIND) { + const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); + for (i = 0; i < len; i++) { + if (u[i] & 0x80) + return; + } + max_char = 127; } - w = orig_w; - unicode = _PyUnicode_New(alloc); - if (!unicode) + else if (kind == PyUnicode_2BYTE_KIND) { + const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); + max_char = 0; + for (i = 0; i < len; i++) { + if (u[i] > max_char) { + max_char = u[i]; + if (max_char >= 256) + return; + } + } + } + else { + const Py_UCS4 *u; + assert(kind == PyUnicode_4BYTE_KIND); + u = PyUnicode_4BYTE_DATA(unicode); + max_char = 0; + for (i = 0; i < len; i++) { + if (u[i] > max_char) { + max_char = u[i]; + if (max_char >= 0x10000) + return; + } + } + } + assert(max_char < PyUnicode_MAX_CHAR_VALUE(unicode)); + copy = PyUnicode_New(len, max_char); + copy_characters(copy, 0, unicode, 0, len); + Py_DECREF(unicode); + *p_unicode = copy; +} + +PyObject* +PyUnicode_Copy(PyObject *unicode) +{ + Py_ssize_t size; + PyObject *copy; + void *data; + + if (!PyUnicode_Check(unicode)) { + PyErr_BadInternalCall(); + return NULL; + } + if (PyUnicode_READY(unicode)) + return NULL; + + size = PyUnicode_GET_LENGTH(unicode); + copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); + if (!copy) return NULL; + assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); - /* Copy the wchar_t data into the new object */ + data = PyUnicode_DATA(unicode); + switch (PyUnicode_KIND(unicode)) { - register Py_UNICODE *u; - u = PyUnicode_AS_UNICODE(unicode); - for (i = size; i > 0; i--) { - if (*w > 0xFFFF) { - wchar_t ordinal = *w++; - ordinal -= 0x10000; - *u++ = 0xD800 | (ordinal >> 10); - *u++ = 0xDC00 | (ordinal & 0x3FF); - } - else - *u++ = *w++; + case PyUnicode_1BYTE_KIND: + memcpy(PyUnicode_1BYTE_DATA(copy), data, size); + break; + case PyUnicode_2BYTE_KIND: + memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); + break; + case PyUnicode_4BYTE_KIND: + memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); + break; + default: + assert(0); + break; + } + assert(_PyUnicode_CheckConsistency(copy, 1)); + return copy; +} + + +/* Widen Unicode objects to larger buffers. Don't write terminating null + character. Return NULL on error. */ + +void* +_PyUnicode_AsKind(PyObject *s, unsigned int kind) +{ + Py_ssize_t len; + void *result; + unsigned int skind; + + if (PyUnicode_READY(s)) + return NULL; + + len = PyUnicode_GET_LENGTH(s); + skind = PyUnicode_KIND(s); + if (skind >= kind) { + PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); + return NULL; + } + switch(kind) { + case PyUnicode_2BYTE_KIND: + result = PyMem_Malloc(len * sizeof(Py_UCS2)); + if (!result) + return PyErr_NoMemory(); + assert(skind == PyUnicode_1BYTE_KIND); + _PyUnicode_CONVERT_BYTES( + Py_UCS1, Py_UCS2, + PyUnicode_1BYTE_DATA(s), + PyUnicode_1BYTE_DATA(s) + len, + result); + return result; + case PyUnicode_4BYTE_KIND: + result = PyMem_Malloc(len * sizeof(Py_UCS4)); + if (!result) + return PyErr_NoMemory(); + if (skind == PyUnicode_2BYTE_KIND) { + _PyUnicode_CONVERT_BYTES( + Py_UCS2, Py_UCS4, + PyUnicode_2BYTE_DATA(s), + PyUnicode_2BYTE_DATA(s) + len, + result); + } + else { + assert(skind == PyUnicode_1BYTE_KIND); + _PyUnicode_CONVERT_BYTES( + Py_UCS1, Py_UCS4, + PyUnicode_1BYTE_DATA(s), + PyUnicode_1BYTE_DATA(s) + len, + result); } + return result; + default: + break; } - return (PyObject *)unicode; + PyErr_SetString(PyExc_SystemError, "invalid kind"); + return NULL; } -#else +static Py_UCS4* +as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, + int copy_null) +{ + int kind; + void *data; + Py_ssize_t len, targetlen; + if (PyUnicode_READY(string) == -1) + return NULL; + kind = PyUnicode_KIND(string); + data = PyUnicode_DATA(string); + len = PyUnicode_GET_LENGTH(string); + targetlen = len; + if (copy_null) + targetlen++; + if (!target) { + if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { + PyErr_NoMemory(); + return NULL; + } + target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); + if (!target) { + PyErr_NoMemory(); + return NULL; + } + } + else { + if (targetsize < targetlen) { + PyErr_Format(PyExc_SystemError, + "string is longer than the buffer"); + if (copy_null && 0 < targetsize) + target[0] = 0; + return NULL; + } + } + if (kind != PyUnicode_4BYTE_KIND) { + Py_ssize_t i; + for (i = 0; i < len; i++) + target[i] = PyUnicode_READ(kind, data, i); + } + else + Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); + if (copy_null) + target[len] = 0; + return target; +} -PyObject *PyUnicode_FromWideChar(register const wchar_t *w, - Py_ssize_t size) +Py_UCS4* +PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, + int copy_null) { - PyUnicodeObject *unicode; + if (target == NULL || targetsize < 1) { + PyErr_BadInternalCall(); + return NULL; + } + return as_ucs4(string, target, targetsize, copy_null); +} +Py_UCS4* +PyUnicode_AsUCS4Copy(PyObject *string) +{ + return as_ucs4(string, NULL, 0, 1); +} + +#ifdef HAVE_WCHAR_H + +PyObject * +PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) +{ if (w == NULL) { if (size == 0) - return PyUnicode_FromStringAndSize(NULL, 0); + return PyUnicode_New(0, 0); PyErr_BadInternalCall(); return NULL; } @@ -647,29 +1984,10 @@ PyObject *PyUnicode_FromWideChar(register const wchar_t *w, size = wcslen(w); } - unicode = _PyUnicode_New(size); - if (!unicode) - return NULL; - - /* Copy the wchar_t data into the new object */ -#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T - memcpy(unicode->str, w, size * sizeof(wchar_t)); -#else - { - register Py_UNICODE *u; - register Py_ssize_t i; - u = PyUnicode_AS_UNICODE(unicode); - for (i = size; i > 0; i--) - *u++ = *w++; - } -#endif - - return (PyObject *)unicode; + return PyUnicode_FromUnicode(w, size); } -#endif /* CONVERT_WCHAR_TO_SURROGATES */ - -#undef CONVERT_WCHAR_TO_SURROGATES +#endif /* HAVE_WCHAR_H */ static void makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, @@ -707,10 +2025,71 @@ makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, *fmt = '\0'; } -#define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} +/* helper for PyUnicode_FromFormatV() */ + +static const char* +parse_format_flags(const char *f, + int *p_width, int *p_precision, + int *p_longflag, int *p_longlongflag, int *p_size_tflag) +{ + int width, precision, longflag, longlongflag, size_tflag; + + /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ + f++; + width = 0; + while (Py_ISDIGIT((unsigned)*f)) + width = (width*10) + *f++ - '0'; + precision = 0; + if (*f == '.') { + f++; + while (Py_ISDIGIT((unsigned)*f)) + precision = (precision*10) + *f++ - '0'; + if (*f == '%') { + /* "%.3%s" => f points to "3" */ + f--; + } + } + if (*f == '\0') { + /* bogus format "%.1" => go backward, f points to "1" */ + f--; + } + if (p_width != NULL) + *p_width = width; + if (p_precision != NULL) + *p_precision = precision; + + /* Handle %ld, %lu, %lld and %llu. */ + longflag = 0; + longlongflag = 0; + size_tflag = 0; + + if (*f == 'l') { + if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { + longflag = 1; + ++f; + } +#ifdef HAVE_LONG_LONG + else if (f[1] == 'l' && + (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { + longlongflag = 1; + f += 2; + } +#endif + } + /* handle the size_t flag. */ + else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) { + size_tflag = 1; + ++f; + } + if (p_longflag != NULL) + *p_longflag = longflag; + if (p_longlongflag != NULL) + *p_longlongflag = longlongflag; + if (p_size_tflag != NULL) + *p_size_tflag = size_tflag; + return f; +} -/* size of fixed-size buffer for formatting single arguments */ -#define ITEM_BUFFER_LEN 21 /* maximum number of characters required for output of %ld. 21 characters allows for 64-bit integers (in decimal) and an optional sign. */ #define MAX_LONG_CHARS 21 @@ -731,123 +2110,184 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) int precision = 0; int zeropad; const char* f; - Py_UNICODE *s; PyObject *string; /* used by sprintf */ - char buffer[ITEM_BUFFER_LEN+1]; - /* use abuffer instead of buffer, if we need more space - * (which can happen if there's a format specifier with width). */ - char *abuffer = NULL; - char *realbuffer; - Py_ssize_t abuffersize = 0; char fmt[61]; /* should be enough for %0width.precisionlld */ - const char *copy; + Py_UCS4 maxchar = 127; /* result is ASCII by default */ + Py_UCS4 argmaxchar; + Py_ssize_t numbersize = 0; + char *numberresults = NULL; + char *numberresult = NULL; + Py_ssize_t i; + int kind; + void *data; Py_VA_COPY(count, vargs); /* step 1: count the number of %S/%R/%A/%s format specifications * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the - * result in an array) */ + * result in an array) + * also estimate a upper bound for all the number formats in the string, + * numbers will be formatted in step 3 and be kept in a '\0'-separated + * buffer before putting everything together. */ for (f = format; *f; f++) { - if (*f == '%') { - if (*(f+1)=='%') - continue; - if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A' || *(f+1) == 'V') - ++callcount; - while (Py_ISDIGIT((unsigned)*f)) - width = (width*10) + *f++ - '0'; - while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f)) - ; - if (*f == 's') - ++callcount; - } - else if (128 <= (unsigned char)*f) { - PyErr_Format(PyExc_ValueError, + if (*f == '%') { + int longlongflag; + /* skip width or width.precision (eg. "1.2" of "%1.2f") */ + f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); + if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') + ++callcount; + + else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { +#ifdef HAVE_LONG_LONG + if (longlongflag) { + if (width < MAX_LONG_LONG_CHARS) + width = MAX_LONG_LONG_CHARS; + } + else +#endif + /* MAX_LONG_CHARS is enough to hold a 64-bit integer, + including sign. Decimal takes the most space. This + isn't enough for octal. If a width is specified we + need more (which we allocate later). */ + if (width < MAX_LONG_CHARS) + width = MAX_LONG_CHARS; + + /* account for the size + '\0' to separate numbers + inside of the numberresults buffer */ + numbersize += (width + 1); + } + } + else if ((unsigned char)*f > 127) { + PyErr_Format(PyExc_ValueError, "PyUnicode_FromFormatV() expects an ASCII-encoded format " "string, got a non-ASCII byte: 0x%02x", (unsigned char)*f); - return NULL; - } + return NULL; + } } /* step 2: allocate memory for the results of * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ if (callcount) { - callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); + callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); if (!callresults) { PyErr_NoMemory(); return NULL; } callresult = callresults; } - /* step 3: figure out how large a buffer we need */ + /* step 2.5: allocate memory for the results of formating numbers */ + if (numbersize) { + numberresults = PyObject_Malloc(numbersize); + if (!numberresults) { + PyErr_NoMemory(); + goto fail; + } + numberresult = numberresults; + } + + /* step 3: format numbers and figure out how large a buffer we need */ for (f = format; *f; f++) { if (*f == '%') { -#ifdef HAVE_LONG_LONG - int longlongflag = 0; -#endif - const char* p = f; - width = 0; - while (Py_ISDIGIT((unsigned)*f)) - width = (width*10) + *f++ - '0'; - while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f)) - ; - - /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since - * they don't affect the amount of space we reserve. - */ - if (*f == 'l') { - if (f[1] == 'd' || f[1] == 'u') { - ++f; - } -#ifdef HAVE_LONG_LONG - else if (f[1] == 'l' && - (f[2] == 'd' || f[2] == 'u')) { - longlongflag = 1; - f += 2; - } -#endif - } - else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { - ++f; - } - + const char* p; + int longflag; + int longlongflag; + int size_tflag; + int numprinted; + + p = f; + zeropad = (f[1] == '0'); + f = parse_format_flags(f, &width, &precision, + &longflag, &longlongflag, &size_tflag); switch (*f) { case 'c': { -#ifndef Py_UNICODE_WIDE - int ordinal = va_arg(count, int); - if (ordinal > 0xffff) - n += 2; - else - n++; -#else - (void)va_arg(count, int); + Py_UCS4 ordinal = va_arg(count, int); + maxchar = Py_MAX(maxchar, ordinal); n++; -#endif break; } case '%': n++; break; - case 'd': case 'u': case 'i': case 'x': - (void) va_arg(count, int); + case 'i': + case 'd': + makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, + width, precision, *f); + if (longflag) + numprinted = sprintf(numberresult, fmt, + va_arg(count, long)); #ifdef HAVE_LONG_LONG - if (longlongflag) { - if (width < MAX_LONG_LONG_CHARS) - width = MAX_LONG_LONG_CHARS; - } + else if (longlongflag) + numprinted = sprintf(numberresult, fmt, + va_arg(count, PY_LONG_LONG)); +#endif + else if (size_tflag) + numprinted = sprintf(numberresult, fmt, + va_arg(count, Py_ssize_t)); else + numprinted = sprintf(numberresult, fmt, + va_arg(count, int)); + n += numprinted; + /* advance by +1 to skip over the '\0' */ + numberresult += (numprinted + 1); + assert(*(numberresult - 1) == '\0'); + assert(*(numberresult - 2) != '\0'); + assert(numprinted >= 0); + assert(numberresult <= numberresults + numbersize); + break; + case 'u': + makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, + width, precision, 'u'); + if (longflag) + numprinted = sprintf(numberresult, fmt, + va_arg(count, unsigned long)); +#ifdef HAVE_LONG_LONG + else if (longlongflag) + numprinted = sprintf(numberresult, fmt, + va_arg(count, unsigned PY_LONG_LONG)); #endif - /* MAX_LONG_CHARS is enough to hold a 64-bit integer, - including sign. Decimal takes the most space. This - isn't enough for octal. If a width is specified we - need more (which we allocate later). */ - if (width < MAX_LONG_CHARS) - width = MAX_LONG_CHARS; - n += width; - /* XXX should allow for large precision here too. */ - if (abuffersize < width) - abuffersize = width; + else if (size_tflag) + numprinted = sprintf(numberresult, fmt, + va_arg(count, size_t)); + else + numprinted = sprintf(numberresult, fmt, + va_arg(count, unsigned int)); + n += numprinted; + numberresult += (numprinted + 1); + assert(*(numberresult - 1) == '\0'); + assert(*(numberresult - 2) != '\0'); + assert(numprinted >= 0); + assert(numberresult <= numberresults + numbersize); + break; + case 'x': + makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); + numprinted = sprintf(numberresult, fmt, va_arg(count, int)); + n += numprinted; + numberresult += (numprinted + 1); + assert(*(numberresult - 1) == '\0'); + assert(*(numberresult - 2) != '\0'); + assert(numprinted >= 0); + assert(numberresult <= numberresults + numbersize); + break; + case 'p': + numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); + /* %p is ill-defined: ensure leading 0x. */ + if (numberresult[1] == 'X') + numberresult[1] = 'x'; + else if (numberresult[1] != 'x') { + memmove(numberresult + 2, numberresult, + strlen(numberresult) + 1); + numberresult[0] = '0'; + numberresult[1] = 'x'; + numprinted += 2; + } + n += numprinted; + numberresult += (numprinted + 1); + assert(*(numberresult - 1) == '\0'); + assert(*(numberresult - 2) != '\0'); + assert(numprinted >= 0); + assert(numberresult <= numberresults + numbersize); break; case 's': { @@ -856,7 +2296,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); if (!str) goto fail; - n += PyUnicode_GET_SIZE(str); + /* since PyUnicode_DecodeUTF8 returns already flexible + unicode objects, there is no need to call ready on them */ + argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(str); /* Remember the str and switch to the next slot */ *callresult++ = str; break; @@ -864,8 +2308,12 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) case 'U': { PyObject *obj = va_arg(count, PyObject *); - assert(obj && PyUnicode_Check(obj)); - n += PyUnicode_GET_SIZE(obj); + assert(obj && _PyUnicode_CHECK(obj)); + if (PyUnicode_READY(obj) == -1) + goto fail; + argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(obj); break; } case 'V': @@ -874,16 +2322,26 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) const char *str = va_arg(count, const char *); PyObject *str_obj; assert(obj || str); - assert(!obj || PyUnicode_Check(obj)); + assert(!obj || _PyUnicode_CHECK(obj)); if (obj) { - n += PyUnicode_GET_SIZE(obj); + if (PyUnicode_READY(obj) == -1) + goto fail; + argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(obj); *callresult++ = NULL; } else { str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); if (!str_obj) goto fail; - n += PyUnicode_GET_SIZE(str_obj); + if (PyUnicode_READY(str_obj)) { + Py_DECREF(str_obj); + goto fail; + } + argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(str_obj); *callresult++ = str_obj; } break; @@ -894,9 +2352,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) PyObject *str; assert(obj); str = PyObject_Str(obj); - if (!str) + if (!str || PyUnicode_READY(str) == -1) goto fail; - n += PyUnicode_GET_SIZE(str); + argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(str); /* Remember the str and switch to the next slot */ *callresult++ = str; break; @@ -907,9 +2367,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) PyObject *repr; assert(obj); repr = PyObject_Repr(obj); - if (!repr) + if (!repr || PyUnicode_READY(repr) == -1) goto fail; - n += PyUnicode_GET_SIZE(repr); + argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(repr); /* Remember the repr and switch to the next slot */ *callresult++ = repr; break; @@ -920,22 +2382,15 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) PyObject *ascii; assert(obj); ascii = PyObject_ASCII(obj); - if (!ascii) + if (!ascii || PyUnicode_READY(ascii) == -1) goto fail; - n += PyUnicode_GET_SIZE(ascii); + argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); + maxchar = Py_MAX(maxchar, argmaxchar); + n += PyUnicode_GET_LENGTH(ascii); /* Remember the repr and switch to the next slot */ *callresult++ = ascii; break; } - case 'p': - (void) va_arg(count, int); - /* maximum 64-bit pointer representation: - * 0xffffffffffffffff - * so 19 characters is enough. - * XXX I count 18 -- what's the extra for? - */ - n += 19; - break; default: /* if we stumble upon an unknown formatting code, copy the rest of @@ -950,127 +2405,63 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) n++; } expand: - if (abuffersize > ITEM_BUFFER_LEN) { - /* add 1 for sprintf's trailing null byte */ - abuffer = PyObject_Malloc(abuffersize + 1); - if (!abuffer) { - PyErr_NoMemory(); - goto fail; - } - realbuffer = abuffer; - } - else - realbuffer = buffer; /* step 4: fill the buffer */ - /* Since we've analyzed how much space we need for the worst case, + /* Since we've analyzed how much space we need, we don't have to resize the string. There can be no errors beyond this point. */ - string = PyUnicode_FromUnicode(NULL, n); + string = PyUnicode_New(n, maxchar); if (!string) goto fail; - - s = PyUnicode_AS_UNICODE(string); + kind = PyUnicode_KIND(string); + data = PyUnicode_DATA(string); callresult = callresults; + numberresult = numberresults; - for (f = format; *f; f++) { + for (i = 0, f = format; *f; f++) { if (*f == '%') { - const char* p = f++; - int longflag = 0; - int longlongflag = 0; - int size_tflag = 0; - zeropad = (*f == '0'); - /* parse the width.precision part */ - width = 0; - while (Py_ISDIGIT((unsigned)*f)) - width = (width*10) + *f++ - '0'; - precision = 0; - if (*f == '.') { - f++; - while (Py_ISDIGIT((unsigned)*f)) - precision = (precision*10) + *f++ - '0'; - } - /* Handle %ld, %lu, %lld and %llu. */ - if (*f == 'l') { - if (f[1] == 'd' || f[1] == 'u') { - longflag = 1; - ++f; - } -#ifdef HAVE_LONG_LONG - else if (f[1] == 'l' && - (f[2] == 'd' || f[2] == 'u')) { - longlongflag = 1; - f += 2; - } -#endif - } - /* handle the size_t flag. */ - if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { - size_tflag = 1; - ++f; - } + const char* p; + + p = f; + f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); + /* checking for == because the last argument could be a empty + string, which causes i to point to end, the assert at the end of + the loop */ + assert(i <= PyUnicode_GET_LENGTH(string)); switch (*f) { case 'c': { - int ordinal = va_arg(vargs, int); -#ifndef Py_UNICODE_WIDE - if (ordinal > 0xffff) { - ordinal -= 0x10000; - *s++ = 0xD800 | (ordinal >> 10); - *s++ = 0xDC00 | (ordinal & 0x3FF); - } else -#endif - *s++ = ordinal; + const int ordinal = va_arg(vargs, int); + PyUnicode_WRITE(kind, data, i++, ordinal); break; } + case 'i': case 'd': - makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, - width, precision, 'd'); - if (longflag) - sprintf(realbuffer, fmt, va_arg(vargs, long)); -#ifdef HAVE_LONG_LONG - else if (longlongflag) - sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); -#endif - else if (size_tflag) - sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); - else - sprintf(realbuffer, fmt, va_arg(vargs, int)); - appendstring(realbuffer); - break; case 'u': - makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, - width, precision, 'u'); - if (longflag) - sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); -#ifdef HAVE_LONG_LONG - else if (longlongflag) - sprintf(realbuffer, fmt, va_arg(vargs, - unsigned PY_LONG_LONG)); -#endif - else if (size_tflag) - sprintf(realbuffer, fmt, va_arg(vargs, size_t)); - else - sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); - appendstring(realbuffer); - break; - case 'i': - makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); - sprintf(realbuffer, fmt, va_arg(vargs, int)); - appendstring(realbuffer); - break; case 'x': - makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); - sprintf(realbuffer, fmt, va_arg(vargs, int)); - appendstring(realbuffer); + case 'p': + /* unused, since we already have the result */ + if (*f == 'p') + (void) va_arg(vargs, void *); + else + (void) va_arg(vargs, int); + /* extract the result from numberresults and append. */ + for (; *numberresult; ++i, ++numberresult) + PyUnicode_WRITE(kind, data, i, *numberresult); + /* skip over the separating '\0' */ + assert(*numberresult == '\0'); + numberresult++; + assert(numberresult <= numberresults + numbersize); break; case 's': { /* unused, since we already have the result */ + Py_ssize_t size; (void) va_arg(vargs, char *); - Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), - PyUnicode_GET_SIZE(*callresult)); - s += PyUnicode_GET_SIZE(*callresult); + size = PyUnicode_GET_LENGTH(*callresult); + assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); + copy_characters(string, i, *callresult, 0, size); + i += size; /* We're done with the unicode()/repr() => forget it */ Py_DECREF(*callresult); /* switch to next unicode()/repr() result */ @@ -1080,23 +2471,29 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) case 'U': { PyObject *obj = va_arg(vargs, PyObject *); - Py_ssize_t size = PyUnicode_GET_SIZE(obj); - Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); - s += size; + Py_ssize_t size; + assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); + size = PyUnicode_GET_LENGTH(obj); + copy_characters(string, i, obj, 0, size); + i += size; break; } case 'V': { + Py_ssize_t size; PyObject *obj = va_arg(vargs, PyObject *); va_arg(vargs, const char *); if (obj) { - Py_ssize_t size = PyUnicode_GET_SIZE(obj); - Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); - s += size; + size = PyUnicode_GET_LENGTH(obj); + assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); + copy_characters(string, i, obj, 0, size); + i += size; } else { - Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), - PyUnicode_GET_SIZE(*callresult)); - s += PyUnicode_GET_SIZE(*callresult); + size = PyUnicode_GET_LENGTH(*callresult); + assert(PyUnicode_KIND(*callresult) <= + PyUnicode_KIND(string)); + copy_characters(string, i, *callresult, 0, size); + i += size; Py_DECREF(*callresult); } ++callresult; @@ -1106,52 +2503,42 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) case 'R': case 'A': { - Py_UNICODE *ucopy; - Py_ssize_t usize; - Py_ssize_t upos; + Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); /* unused, since we already have the result */ (void) va_arg(vargs, PyObject *); - ucopy = PyUnicode_AS_UNICODE(*callresult); - usize = PyUnicode_GET_SIZE(*callresult); - for (upos = 0; upos<usize;) - *s++ = ucopy[upos++]; + assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); + copy_characters(string, i, *callresult, 0, size); + i += size; /* We're done with the unicode()/repr() => forget it */ Py_DECREF(*callresult); /* switch to next unicode()/repr() result */ ++callresult; break; } - case 'p': - sprintf(buffer, "%p", va_arg(vargs, void*)); - /* %p is ill-defined: ensure leading 0x. */ - if (buffer[1] == 'X') - buffer[1] = 'x'; - else if (buffer[1] != 'x') { - memmove(buffer+2, buffer, strlen(buffer)+1); - buffer[0] = '0'; - buffer[1] = 'x'; - } - appendstring(buffer); - break; case '%': - *s++ = '%'; + PyUnicode_WRITE(kind, data, i++, '%'); break; default: - appendstring(p); + for (; *p; ++p, ++i) + PyUnicode_WRITE(kind, data, i, *p); + assert(i == PyUnicode_GET_LENGTH(string)); goto end; } } - else - *s++ = *f; + else { + assert(i < PyUnicode_GET_LENGTH(string)); + PyUnicode_WRITE(kind, data, i++, *f); + } } + assert(i == PyUnicode_GET_LENGTH(string)); end: if (callresults) PyObject_Free(callresults); - if (abuffer) - PyObject_Free(abuffer); - PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); - return string; + if (numberresults) + PyObject_Free(numberresults); + assert(_PyUnicode_CheckConsistency(string, 1)); + return (PyObject *)string; fail: if (callresults) { PyObject **callresult2 = callresults; @@ -1161,13 +2548,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) } PyObject_Free(callresults); } - if (abuffer) - PyObject_Free(abuffer); + if (numberresults) + PyObject_Free(numberresults); return NULL; } -#undef appendstring - PyObject * PyUnicode_FromFormat(const char *format, ...) { @@ -1184,6 +2569,8 @@ PyUnicode_FromFormat(const char *format, ...) return ret; } +#ifdef HAVE_WCHAR_H + /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): convert a Unicode object to a wide character string. @@ -1198,99 +2585,23 @@ unicode_aswidechar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size) { -#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T Py_ssize_t res; + const wchar_t *wstr; + + wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); + if (wstr == NULL) + return -1; + if (w != NULL) { - res = PyUnicode_GET_SIZE(unicode); if (size > res) size = res + 1; else res = size; - memcpy(w, unicode->str, size * sizeof(wchar_t)); + Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); return res; } else - return PyUnicode_GET_SIZE(unicode) + 1; -#elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 - register const Py_UNICODE *u; - const Py_UNICODE *uend; - const wchar_t *worig, *wend; - Py_ssize_t nchar; - - u = PyUnicode_AS_UNICODE(unicode); - uend = u + PyUnicode_GET_SIZE(unicode); - if (w != NULL) { - worig = w; - wend = w + size; - while (u != uend && w != wend) { - if (0xD800 <= u[0] && u[0] <= 0xDBFF - && 0xDC00 <= u[1] && u[1] <= 0xDFFF) - { - *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000; - u += 2; - } - else { - *w = *u; - u++; - } - w++; - } - if (w != wend) - *w = L'\0'; - return w - worig; - } - else { - nchar = 1; /* null character at the end */ - while (u != uend) { - if (0xD800 <= u[0] && u[0] <= 0xDBFF - && 0xDC00 <= u[1] && u[1] <= 0xDFFF) - u += 2; - else - u++; - nchar++; - } - } - return nchar; -#elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 - register Py_UNICODE *u, *uend, ordinal; - register Py_ssize_t i; - wchar_t *worig, *wend; - Py_ssize_t nchar; - - u = PyUnicode_AS_UNICODE(unicode); - uend = u + PyUnicode_GET_SIZE(u); - if (w != NULL) { - worig = w; - wend = w + size; - while (u != uend && w != wend) { - ordinal = *u; - if (ordinal > 0xffff) { - ordinal -= 0x10000; - *w++ = 0xD800 | (ordinal >> 10); - *w++ = 0xDC00 | (ordinal & 0x3FF); - } - else - *w++ = ordinal; - u++; - } - if (w != wend) - *w = 0; - return w - worig; - } - else { - nchar = 1; /* null character */ - while (u != uend) { - if (*u > 0xffff) - nchar += 2; - else - nchar++; - u++; - } - return nchar; - } -#else -# error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670" -#endif + return res + 1; } Py_ssize_t @@ -1318,6 +2629,8 @@ PyUnicode_AsWideCharString(PyObject *unicode, } buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); + if (buflen == -1) + return NULL; if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { PyErr_NoMemory(); return NULL; @@ -1329,49 +2642,51 @@ PyUnicode_AsWideCharString(PyObject *unicode, return NULL; } buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); + if (buflen == -1) + return NULL; if (size != NULL) *size = buflen; return buffer; } -#endif +#endif /* HAVE_WCHAR_H */ -PyObject *PyUnicode_FromOrdinal(int ordinal) +PyObject * +PyUnicode_FromOrdinal(int ordinal) { - Py_UNICODE s[2]; - + PyObject *v; if (ordinal < 0 || ordinal > 0x10ffff) { PyErr_SetString(PyExc_ValueError, "chr() arg not in range(0x110000)"); return NULL; } -#ifndef Py_UNICODE_WIDE - if (ordinal > 0xffff) { - ordinal -= 0x10000; - s[0] = 0xD800 | (ordinal >> 10); - s[1] = 0xDC00 | (ordinal & 0x3FF); - return PyUnicode_FromUnicode(s, 2); - } -#endif + if (ordinal < 256) + return get_latin1_char(ordinal); - s[0] = (Py_UNICODE)ordinal; - return PyUnicode_FromUnicode(s, 1); + v = PyUnicode_New(1, ordinal); + if (v == NULL) + return NULL; + PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); + assert(_PyUnicode_CheckConsistency(v, 1)); + return v; } -PyObject *PyUnicode_FromObject(register PyObject *obj) +PyObject * +PyUnicode_FromObject(register PyObject *obj) { /* XXX Perhaps we should make this API an alias of PyObject_Str() instead ?! */ if (PyUnicode_CheckExact(obj)) { + if (PyUnicode_READY(obj)) + return NULL; Py_INCREF(obj); return obj; } if (PyUnicode_Check(obj)) { /* For a Unicode subtype that's not a Unicode object, return a true Unicode object with the same data. */ - return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), - PyUnicode_GET_SIZE(obj)); + return PyUnicode_Copy(obj); } PyErr_Format(PyExc_TypeError, "Can't convert '%.100s' object to str implicitly", @@ -1379,9 +2694,10 @@ PyObject *PyUnicode_FromObject(register PyObject *obj) return NULL; } -PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_FromEncodedObject(register PyObject *obj, + const char *encoding, + const char *errors) { Py_buffer buffer; PyObject *v; @@ -1395,7 +2711,7 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, if (PyBytes_Check(obj)) { if (PyBytes_GET_SIZE(obj) == 0) { Py_INCREF(unicode_empty); - v = (PyObject *) unicode_empty; + v = unicode_empty; } else { v = PyUnicode_Decode( @@ -1422,7 +2738,7 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, if (buffer.len == 0) { Py_INCREF(unicode_empty); - v = (PyObject *) unicode_empty; + v = unicode_empty; } else v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); @@ -1464,26 +2780,29 @@ normalize_encoding(const char *encoding, return 1; } -PyObject *PyUnicode_Decode(const char *s, - Py_ssize_t size, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_Decode(const char *s, + Py_ssize_t size, + const char *encoding, + const char *errors) { PyObject *buffer = NULL, *unicode; Py_buffer info; char lower[11]; /* Enough for any encoding shortcut */ if (encoding == NULL) - encoding = PyUnicode_GetDefaultEncoding(); + return PyUnicode_DecodeUTF8(s, size, errors); /* Shortcuts for common default encodings */ if (normalize_encoding(encoding, lower, sizeof(lower))) { - if (strcmp(lower, "utf-8") == 0) + if ((strcmp(lower, "utf-8") == 0) || + (strcmp(lower, "utf8") == 0)) return PyUnicode_DecodeUTF8(s, size, errors); else if ((strcmp(lower, "latin-1") == 0) || + (strcmp(lower, "latin1") == 0) || (strcmp(lower, "iso-8859-1") == 0)) return PyUnicode_DecodeLatin1(s, size, errors); -#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) +#ifdef HAVE_MBCS else if (strcmp(lower, "mbcs") == 0) return PyUnicode_DecodeMBCS(s, size, errors); #endif @@ -1513,6 +2832,13 @@ PyObject *PyUnicode_Decode(const char *s, goto onError; } Py_DECREF(buffer); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&unicode)) { + Py_DECREF(unicode); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(unicode, 1)); return unicode; onError: @@ -1520,9 +2846,10 @@ PyObject *PyUnicode_Decode(const char *s, return NULL; } -PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_AsDecodedObject(PyObject *unicode, + const char *encoding, + const char *errors) { PyObject *v; @@ -1538,15 +2865,17 @@ PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, v = PyCodec_Decode(unicode, encoding, errors); if (v == NULL) goto onError; + assert(_PyUnicode_CheckConsistency(v, 1)); return v; onError: return NULL; } -PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_AsDecodedUnicode(PyObject *unicode, + const char *encoding, + const char *errors) { PyObject *v; @@ -1569,16 +2898,18 @@ PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, Py_DECREF(v); goto onError; } + assert(_PyUnicode_CheckConsistency(v, 1)); return v; onError: return NULL; } -PyObject *PyUnicode_Encode(const Py_UNICODE *s, - Py_ssize_t size, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_Encode(const Py_UNICODE *s, + Py_ssize_t size, + const char *encoding, + const char *errors) { PyObject *v, *unicode; @@ -1590,9 +2921,10 @@ PyObject *PyUnicode_Encode(const Py_UNICODE *s, return v; } -PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_AsEncodedObject(PyObject *unicode, + const char *encoding, + const char *errors) { PyObject *v; @@ -1617,14 +2949,12 @@ PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, PyObject * PyUnicode_EncodeFSDefault(PyObject *unicode) { -#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) +#ifdef HAVE_MBCS return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), NULL); #elif defined(__APPLE__) - return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - "surrogateescape"); + return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); #else PyInterpreterState *interp = PyThreadState_GET()->interp; /* Bootstrap check: if the filesystem codec is implemented in Python, we @@ -1679,9 +3009,10 @@ PyUnicode_EncodeFSDefault(PyObject *unicode) #endif } -PyObject *PyUnicode_AsEncodedString(PyObject *unicode, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_AsEncodedString(PyObject *unicode, + const char *encoding, + const char *errors) { PyObject *v; char lower[11]; /* Enough for any encoding shortcut */ @@ -1691,46 +3022,36 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode, return NULL; } - if (encoding == NULL) - encoding = PyUnicode_GetDefaultEncoding(); + if (encoding == NULL) { + if (errors == NULL || strcmp(errors, "strict") == 0) + return _PyUnicode_AsUTF8String(unicode, NULL); + else + return _PyUnicode_AsUTF8String(unicode, errors); + } /* Shortcuts for common default encodings */ if (normalize_encoding(encoding, lower, sizeof(lower))) { - if (strcmp(lower, "utf-8") == 0) - return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - errors); + if ((strcmp(lower, "utf-8") == 0) || + (strcmp(lower, "utf8") == 0)) + { + if (errors == NULL || strcmp(errors, "strict") == 0) + return _PyUnicode_AsUTF8String(unicode, NULL); + else + return _PyUnicode_AsUTF8String(unicode, errors); + } else if ((strcmp(lower, "latin-1") == 0) || + (strcmp(lower, "latin1") == 0) || (strcmp(lower, "iso-8859-1") == 0)) - return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - errors); -#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) + return _PyUnicode_AsLatin1String(unicode, errors); +#ifdef HAVE_MBCS else if (strcmp(lower, "mbcs") == 0) return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), errors); #endif else if (strcmp(lower, "ascii") == 0) - return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - errors); - } - /* During bootstrap, we may need to find the encodings - package, to load the file system encoding, and require the - file system encoding in order to load the encodings - package. - - Break out of this dependency by assuming that the path to - the encodings module is ASCII-only. XXX could try wcstombs - instead, if the file system encoding is the locale's - encoding. */ - if (Py_FileSystemDefaultEncoding && - strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && - !PyThreadState_GET()->interp->codecs_initialized) - return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - errors); + return _PyUnicode_AsASCIIString(unicode, errors); + } /* Encode via the codec registry */ v = PyCodec_Encode(unicode, encoding, errors); @@ -1766,9 +3087,10 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode, return NULL; } -PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, - const char *encoding, - const char *errors) +PyObject * +PyUnicode_AsEncodedUnicode(PyObject *unicode, + const char *encoding, + const char *errors) { PyObject *v; @@ -1797,23 +3119,6 @@ PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, return NULL; } -PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, - const char *errors) -{ - PyObject *v = ((PyUnicodeObject *)unicode)->defenc; - if (v) - return v; - if (errors != NULL) - Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); - v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - NULL); - if (!v) - return NULL; - ((PyUnicodeObject *)unicode)->defenc = v; - return v; -} - PyObject* PyUnicode_DecodeFSDefault(const char *s) { Py_ssize_t size = (Py_ssize_t)strlen(s); @@ -1823,7 +3128,7 @@ PyUnicode_DecodeFSDefault(const char *s) { PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) { -#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) +#ifdef HAVE_MBCS return PyUnicode_DecodeMBCS(s, size, NULL); #elif defined(__APPLE__) return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); @@ -1910,13 +3215,13 @@ int PyUnicode_FSDecoder(PyObject* arg, void* addr) { PyObject *output = NULL; - Py_ssize_t size; - void *data; if (arg == NULL) { Py_DECREF(*(PyObject**)addr); return 1; } if (PyUnicode_Check(arg)) { + if (PyUnicode_READY(arg)) + return 0; output = arg; Py_INCREF(output); } @@ -1935,9 +3240,8 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) return 0; } } - size = PyUnicode_GET_SIZE(output); - data = PyUnicode_AS_UNICODE(output); - if (size != Py_UNICODE_strlen(data)) { + if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), + PyUnicode_GET_LENGTH(output), 0, 1)) { PyErr_SetString(PyExc_TypeError, "embedded NUL character"); Py_DECREF(output); return 0; @@ -1948,40 +3252,175 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) char* -_PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) +PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) { PyObject *bytes; + PyUnicodeObject *u = (PyUnicodeObject *)unicode; + if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); return NULL; } - bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); - if (bytes == NULL) + if (PyUnicode_READY(u) == -1) return NULL; - if (psize != NULL) - *psize = PyBytes_GET_SIZE(bytes); - return PyBytes_AS_STRING(bytes); + + if (PyUnicode_UTF8(unicode) == NULL) { + assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); + bytes = _PyUnicode_AsUTF8String(unicode, "strict"); + if (bytes == NULL) + return NULL; + _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); + if (_PyUnicode_UTF8(u) == NULL) { + Py_DECREF(bytes); + return NULL; + } + _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); + Py_MEMCPY(_PyUnicode_UTF8(u), PyBytes_AS_STRING(bytes), _PyUnicode_UTF8_LENGTH(u) + 1); + Py_DECREF(bytes); + } + + if (psize) + *psize = PyUnicode_UTF8_LENGTH(unicode); + return PyUnicode_UTF8(unicode); } char* -_PyUnicode_AsString(PyObject *unicode) +PyUnicode_AsUTF8(PyObject *unicode) { - return _PyUnicode_AsStringAndSize(unicode, NULL); + return PyUnicode_AsUTF8AndSize(unicode, NULL); } -Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) +#ifdef Py_DEBUG +int unicode_as_unicode_calls = 0; +#endif + + +Py_UNICODE * +PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) { + PyUnicodeObject *u; + const unsigned char *one_byte; +#if SIZEOF_WCHAR_T == 4 + const Py_UCS2 *two_bytes; +#else + const Py_UCS4 *four_bytes; + const Py_UCS4 *ucs4_end; + Py_ssize_t num_surrogates; +#endif + wchar_t *w; + wchar_t *wchar_end; + if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); - goto onError; + return NULL; } - return PyUnicode_AS_UNICODE(unicode); + u = (PyUnicodeObject*)unicode; + if (_PyUnicode_WSTR(u) == NULL) { + /* Non-ASCII compact unicode object */ + assert(_PyUnicode_KIND(u) != 0); + assert(PyUnicode_IS_READY(u)); - onError: - return NULL; +#ifdef Py_DEBUG + ++unicode_as_unicode_calls; +#endif + + if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { +#if SIZEOF_WCHAR_T == 2 + four_bytes = PyUnicode_4BYTE_DATA(u); + ucs4_end = four_bytes + _PyUnicode_LENGTH(u); + num_surrogates = 0; + + for (; four_bytes < ucs4_end; ++four_bytes) { + if (*four_bytes > 0xFFFF) + ++num_surrogates; + } + + _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( + sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); + if (!_PyUnicode_WSTR(u)) { + PyErr_NoMemory(); + return NULL; + } + _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; + + w = _PyUnicode_WSTR(u); + wchar_end = w + _PyUnicode_WSTR_LENGTH(u); + four_bytes = PyUnicode_4BYTE_DATA(u); + for (; four_bytes < ucs4_end; ++four_bytes, ++w) { + if (*four_bytes > 0xFFFF) { + /* encode surrogate pair in this case */ + *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); + *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); + } + else + *w = *four_bytes; + + if (w > wchar_end) { + assert(0 && "Miscalculated string end"); + } + } + *w = 0; +#else + /* sizeof(wchar_t) == 4 */ + Py_FatalError("Impossible unicode object state, wstr and str " + "should share memory already."); + return NULL; +#endif + } + else { + _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * + (_PyUnicode_LENGTH(u) + 1)); + if (!_PyUnicode_WSTR(u)) { + PyErr_NoMemory(); + return NULL; + } + if (!PyUnicode_IS_COMPACT_ASCII(u)) + _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); + w = _PyUnicode_WSTR(u); + wchar_end = w + _PyUnicode_LENGTH(u); + + if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { + one_byte = PyUnicode_1BYTE_DATA(u); + for (; w < wchar_end; ++one_byte, ++w) + *w = *one_byte; + /* null-terminate the wstr */ + *w = 0; + } + else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { +#if SIZEOF_WCHAR_T == 4 + two_bytes = PyUnicode_2BYTE_DATA(u); + for (; w < wchar_end; ++two_bytes, ++w) + *w = *two_bytes; + /* null-terminate the wstr */ + *w = 0; +#else + /* sizeof(wchar_t) == 2 */ + PyObject_FREE(_PyUnicode_WSTR(u)); + _PyUnicode_WSTR(u) = NULL; + Py_FatalError("Impossible unicode object state, wstr " + "and str should share memory already."); + return NULL; +#endif + } + else { + assert(0 && "This should never happen."); + } + } + } + if (size != NULL) + *size = PyUnicode_WSTR_LENGTH(u); + return _PyUnicode_WSTR(u); } -Py_ssize_t PyUnicode_GetSize(PyObject *unicode) +Py_UNICODE * +PyUnicode_AsUnicode(PyObject *unicode) +{ + return PyUnicode_AsUnicodeAndSize(unicode, NULL); +} + + +Py_ssize_t +PyUnicode_GetSize(PyObject *unicode) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); @@ -1993,7 +3432,51 @@ Py_ssize_t PyUnicode_GetSize(PyObject *unicode) return -1; } -const char *PyUnicode_GetDefaultEncoding(void) +Py_ssize_t +PyUnicode_GetLength(PyObject *unicode) +{ + if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { + PyErr_BadArgument(); + return -1; + } + + return PyUnicode_GET_LENGTH(unicode); +} + +Py_UCS4 +PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) +{ + if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { + PyErr_BadArgument(); + return (Py_UCS4)-1; + } + if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { + PyErr_SetString(PyExc_IndexError, "string index out of range"); + return (Py_UCS4)-1; + } + return PyUnicode_READ_CHAR(unicode, index); +} + +int +PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) +{ + if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { + PyErr_BadArgument(); + return -1; + } + if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { + PyErr_SetString(PyExc_IndexError, "string index out of range"); + return -1; + } + if (_PyUnicode_Dirty(unicode)) + return -1; + PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), + index, ch); + return 0; +} + +const char * +PyUnicode_GetDefaultEncoding(void) { return "utf-8"; } @@ -2032,12 +3515,12 @@ onError: return 0 on success, -1 on error */ -static -int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, - const char *encoding, const char *reason, - const char **input, const char **inend, Py_ssize_t *startinpos, - Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, - PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) +static int +unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, + const char *encoding, const char *reason, + const char **input, const char **inend, Py_ssize_t *startinpos, + Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, + PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) { static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; @@ -2047,7 +3530,7 @@ int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler Py_ssize_t insize; Py_ssize_t requiredsize; Py_ssize_t newpos; - Py_UNICODE *repptr; + const Py_UNICODE *repptr; PyObject *inputobj = NULL; Py_ssize_t repsize; int res = -1; @@ -2108,7 +3591,7 @@ int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler if (requiredsize > outsize) { if (requiredsize<2*outsize) requiredsize = 2*outsize; - if (_PyUnicode_Resize(output, requiredsize) < 0) + if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) goto onError; *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; } @@ -2207,9 +3690,10 @@ char utf7_category[128] = { (directWS && (utf7_category[(c)] == 2)) || \ (directO && (utf7_category[(c)] == 1)))) -PyObject *PyUnicode_DecodeUTF7(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeUTF7(const char *s, + Py_ssize_t size, + const char *errors) { return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); } @@ -2221,10 +3705,11 @@ PyObject *PyUnicode_DecodeUTF7(const char *s, * all the shift state (seen bits, number of bits seen, high * surrogate). */ -PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, - Py_ssize_t size, - const char *errors, - Py_ssize_t *consumed) +PyObject * +PyUnicode_DecodeUTF7Stateful(const char *s, + Py_ssize_t size, + const char *errors, + Py_ssize_t *consumed) { const char *starts = s; Py_ssize_t startinpos; @@ -2251,7 +3736,7 @@ PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, return (PyObject *)unicode; } - p = unicode->str; + p = PyUnicode_AS_UNICODE(unicode); shiftOutStart = p; e = s + size; @@ -2396,11 +3881,18 @@ utf7Error: } } - if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) + if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&unicode)) { + Py_DECREF(unicode); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(unicode, 1)); return (PyObject *)unicode; onError: @@ -2411,11 +3903,12 @@ utf7Error: } -PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, - Py_ssize_t size, - int base64SetO, - int base64WhiteSpace, - const char *errors) +PyObject * +PyUnicode_EncodeUTF7(const Py_UNICODE *s, + Py_ssize_t size, + int base64SetO, + int base64WhiteSpace, + const char *errors) { PyObject *v; /* It might be possible to tighten this worst case */ @@ -2536,9 +4029,10 @@ char utf8_code_length[256] = { 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ }; -PyObject *PyUnicode_DecodeUTF8(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeUTF8(const char *s, + Py_ssize_t size, + const char *errors) { return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); } @@ -2556,37 +4050,197 @@ PyObject *PyUnicode_DecodeUTF8(const char *s, # error C 'long' size should be either 4 or 8! #endif -PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, - Py_ssize_t size, - const char *errors, - Py_ssize_t *consumed) +/* Scans a UTF-8 string and returns the maximum character to be expected, + the size of the decoded unicode string and if any major errors were + encountered. + + This function does check basic UTF-8 sanity, it does however NOT CHECK + if the string contains surrogates, and if all continuation bytes are + within the correct ranges, these checks are performed in + PyUnicode_DecodeUTF8Stateful. + + If it sets has_errors to 1, it means the value of unicode_size and max_char + will be bogus and you should not rely on useful information in them. + */ +static Py_UCS4 +utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, + Py_ssize_t *unicode_size, Py_ssize_t* consumed, + int *has_errors) +{ + Py_ssize_t n; + Py_ssize_t char_count = 0; + Py_UCS4 max_char = 127, new_max; + Py_UCS4 upper_bound; + const unsigned char *p = (const unsigned char *)s; + const unsigned char *end = p + string_size; + const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); + int err = 0; + + for (; p < end && !err; ++p, ++char_count) { + /* Only check value if it's not a ASCII char... */ + if (*p < 0x80) { + /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for + an explanation. */ + if (!((size_t) p & LONG_PTR_MASK)) { + /* Help register allocation */ + register const unsigned char *_p = p; + while (_p < aligned_end) { + unsigned long value = *(unsigned long *) _p; + if (value & ASCII_CHAR_MASK) + break; + _p += SIZEOF_LONG; + char_count += SIZEOF_LONG; + } + p = _p; + if (p == end) + break; + } + } + if (*p >= 0x80) { + n = utf8_code_length[*p]; + new_max = max_char; + switch (n) { + /* invalid start byte */ + case 0: + err = 1; + break; + case 2: + /* Code points between 0x00FF and 0x07FF inclusive. + Approximate the upper bound of the code point, + if this flips over 255 we can be sure it will be more + than 255 and the string will need 2 bytes per code coint, + if it stays under or equal to 255, we can be sure 1 byte + is enough. + ((*p & 0b00011111) << 6) | 0b00111111 */ + upper_bound = ((*p & 0x1F) << 6) | 0x3F; + if (max_char < upper_bound) + new_max = upper_bound; + /* Ensure we track at least that we left ASCII space. */ + if (new_max < 128) + new_max = 128; + break; + case 3: + /* Between 0x0FFF and 0xFFFF inclusive, so values are + always > 255 and <= 65535 and will always need 2 bytes. */ + if (max_char < 65535) + new_max = 65535; + break; + case 4: + /* Code point will be above 0xFFFF for sure in this case. */ + new_max = 65537; + break; + /* Internal error, this should be caught by the first if */ + case 1: + default: + assert(0 && "Impossible case in utf8_max_char_and_size"); + err = 1; + } + /* Instead of number of overall bytes for this code point, + n contains the number of following bytes: */ + --n; + /* Check if the follow up chars are all valid continuation bytes */ + if (n >= 1) { + const unsigned char *cont; + if ((p + n) >= end) { + if (consumed == 0) + /* incomplete data, non-incremental decoding */ + err = 1; + break; + } + for (cont = p + 1; cont < (p + n); ++cont) { + if ((*cont & 0xc0) != 0x80) { + err = 1; + break; + } + } + p += n; + } + else + err = 1; + max_char = new_max; + } + } + + if (unicode_size) + *unicode_size = char_count; + if (has_errors) + *has_errors = err; + return max_char; +} + +/* Similar to PyUnicode_WRITE but can also write into wstr field + of the legacy unicode representation */ +#define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ + do { \ + const int k_ = (kind); \ + if (k_ == PyUnicode_WCHAR_KIND) \ + ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ + else if (k_ == PyUnicode_1BYTE_KIND) \ + ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ + else if (k_ == PyUnicode_2BYTE_KIND) \ + ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ + else \ + ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ + } while (0) + +PyObject * +PyUnicode_DecodeUTF8Stateful(const char *s, + Py_ssize_t size, + const char *errors, + Py_ssize_t *consumed) { const char *starts = s; int n; int k; Py_ssize_t startinpos; Py_ssize_t endinpos; - Py_ssize_t outpos; const char *e, *aligned_end; PyUnicodeObject *unicode; - Py_UNICODE *p; const char *errmsg = ""; PyObject *errorHandler = NULL; PyObject *exc = NULL; + Py_UCS4 maxchar = 0; + Py_ssize_t unicode_size; + Py_ssize_t i; + int kind; + void *data; + int has_errors; + Py_UNICODE *error_outptr; +#if SIZEOF_WCHAR_T == 2 + Py_ssize_t wchar_offset = 0; +#endif - /* Note: size will always be longer than the resulting Unicode - character count */ - unicode = _PyUnicode_New(size); - if (!unicode) - return NULL; if (size == 0) { if (consumed) *consumed = 0; - return (PyObject *)unicode; + return (PyObject *)PyUnicode_New(0, 0); + } + maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, + consumed, &has_errors); + if (has_errors) { + unicode = _PyUnicode_New(size); + if (!unicode) + return NULL; + kind = PyUnicode_WCHAR_KIND; + data = PyUnicode_AS_UNICODE(unicode); + assert(data != NULL); + } + else { + unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); + if (!unicode) + return NULL; + /* When the string is ASCII only, just use memcpy and return. + unicode_size may be != size if there is an incomplete UTF-8 + sequence at the end of the ASCII block. */ + if (maxchar < 128 && size == unicode_size) { + Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); + return (PyObject *)unicode; + } + kind = PyUnicode_KIND(unicode); + data = PyUnicode_DATA(unicode); } - /* Unpack UTF-8 encoded data */ - p = unicode->str; + i = 0; e = s + size; aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); @@ -2604,29 +4258,29 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, if (!((size_t) s & LONG_PTR_MASK)) { /* Help register allocation */ register const char *_s = s; - register Py_UNICODE *_p = p; + register Py_ssize_t _i = i; while (_s < aligned_end) { /* Read a whole long at a time (either 4 or 8 bytes), and do a fast unrolled copy if it only contains ASCII characters. */ - unsigned long data = *(unsigned long *) _s; - if (data & ASCII_CHAR_MASK) + unsigned long value = *(unsigned long *) _s; + if (value & ASCII_CHAR_MASK) break; - _p[0] = (unsigned char) _s[0]; - _p[1] = (unsigned char) _s[1]; - _p[2] = (unsigned char) _s[2]; - _p[3] = (unsigned char) _s[3]; + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); #if (SIZEOF_LONG == 8) - _p[4] = (unsigned char) _s[4]; - _p[5] = (unsigned char) _s[5]; - _p[6] = (unsigned char) _s[6]; - _p[7] = (unsigned char) _s[7]; + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); + WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); #endif _s += SIZEOF_LONG; - _p += SIZEOF_LONG; + _i += SIZEOF_LONG; } s = _s; - p = _p; + i = _i; if (s == e) break; ch = (unsigned char)*s; @@ -2634,7 +4288,7 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, } if (ch < 0x80) { - *p++ = (Py_UNICODE)ch; + WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); s++; continue; } @@ -2677,7 +4331,7 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, } ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); assert ((ch > 0x007F) && (ch <= 0x07FF)); - *p++ = (Py_UNICODE)ch; + WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); break; case 3: @@ -2706,7 +4360,7 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, } ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); assert ((ch > 0x07FF) && (ch <= 0xFFFF)); - *p++ = (Py_UNICODE)ch; + WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); break; case 4: @@ -2731,19 +4385,27 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); -#ifdef Py_UNICODE_WIDE - *p++ = (Py_UNICODE)ch; -#else - /* compute and append the two surrogates: */ + /* If the string is flexible or we have native UCS-4, write + directly.. */ + if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) + WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); - /* translate from 10000..10FFFF to 0..FFFF */ - ch -= 0x10000; + else { + /* compute and append the two surrogates: */ - /* high surrogate = top 10 bits added to D800 */ - *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); + /* translate from 10000..10FFFF to 0..FFFF */ + ch -= 0x10000; - /* low surrogate = bottom 10 bits added to DC00 */ - *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); + /* high surrogate = top 10 bits added to D800 */ + WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, + (Py_UNICODE)(0xD800 + (ch >> 10))); + + /* low surrogate = bottom 10 bits added to DC00 */ + WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, + (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); + } +#if SIZEOF_WCHAR_T == 2 + wchar_offset++; #endif break; } @@ -2751,24 +4413,59 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, continue; utf8Error: - outpos = p-PyUnicode_AS_UNICODE(unicode); + /* If this is not yet a resizable string, make it one.. */ + if (kind != PyUnicode_WCHAR_KIND) { + const Py_UNICODE *u; + PyUnicodeObject *new_unicode = _PyUnicode_New(size); + if (!new_unicode) + goto onError; + u = PyUnicode_AsUnicode((PyObject *)unicode); + if (!u) + goto onError; +#if SIZEOF_WCHAR_T == 2 + i += wchar_offset; +#endif + Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); + Py_DECREF(unicode); + unicode = new_unicode; + kind = 0; + data = PyUnicode_AS_UNICODE(new_unicode); + assert(data != NULL); + } + error_outptr = PyUnicode_AS_UNICODE(unicode) + i; if (unicode_decode_call_errorhandler( errors, &errorHandler, "utf8", errmsg, &starts, &e, &startinpos, &endinpos, &exc, &s, - &unicode, &outpos, &p)) + &unicode, &i, &error_outptr)) goto onError; + /* Update data because unicode_decode_call_errorhandler might have + re-created or resized the unicode object. */ + data = PyUnicode_AS_UNICODE(unicode); aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); } + /* Ensure the unicode_size calculation above was correct: */ + assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); + if (consumed) *consumed = s-starts; - /* Adjust length */ - if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) - goto onError; + /* Adjust length and ready string when it contained errors and + is of the old resizable kind. */ + if (kind == PyUnicode_WCHAR_KIND) { + if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) + goto onError; + } Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&unicode)) { + Py_DECREF(unicode); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(unicode, 1)); return (PyObject *)unicode; onError: @@ -2778,7 +4475,7 @@ PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, return NULL; } -#undef ASCII_CHAR_MASK +#undef WRITE_FLEXIBLE_OR_WSTR #ifdef __APPLE__ @@ -2849,7 +4546,7 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) } ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); assert ((ch > 0x07FF) && (ch <= 0xFFFF)); - *p++ = (Py_UNICODE)ch; + *p++ = (wchar_t)ch; break; case 4: @@ -2895,15 +4592,15 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) #endif /* __APPLE__ */ -/* Allocation strategy: if the string is short, convert into a stack buffer +/* Primary internal function which creates utf8 encoded bytes objects. + + Allocation strategy: if the string is short, convert into a stack buffer and allocate exactly as much space needed at the end. Else allocate the maximum possible needed (4 result bytes per Unicode character), and return the excess memory at the end. */ PyObject * -PyUnicode_EncodeUTF8(const Py_UNICODE *s, - Py_ssize_t size, - const char *errors) +_PyUnicode_AsUTF8String(PyObject *obj, const char *errors) { #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ @@ -2915,8 +4612,30 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s, char stackbuf[MAX_SHORT_UNICHARS * 4]; PyObject *errorHandler = NULL; PyObject *exc = NULL; + int kind; + void *data; + Py_ssize_t size; + PyUnicodeObject *unicode = (PyUnicodeObject *)obj; +#if SIZEOF_WCHAR_T == 2 + Py_ssize_t wchar_offset = 0; +#endif + + if (!PyUnicode_Check(unicode)) { + PyErr_BadArgument(); + return NULL; + } + + if (PyUnicode_READY(unicode) == -1) + return NULL; + + if (PyUnicode_UTF8(unicode)) + return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), + PyUnicode_UTF8_LENGTH(unicode)); + + kind = PyUnicode_KIND(unicode); + data = PyUnicode_DATA(unicode); + size = PyUnicode_GET_LENGTH(unicode); - assert(s != NULL); assert(size >= 0); if (size <= MAX_SHORT_UNICHARS) { @@ -2940,7 +4659,7 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s, } for (i = 0; i < size;) { - Py_UCS4 ch = s[i++]; + Py_UCS4 ch = PyUnicode_READ(kind, data, i++); if (ch < 0x80) /* Encode ASCII */ @@ -2951,83 +4670,72 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s, *p++ = (char)(0xc0 | (ch >> 6)); *p++ = (char)(0x80 | (ch & 0x3f)); } else if (0xD800 <= ch && ch <= 0xDFFF) { -#ifndef Py_UNICODE_WIDE - /* Special case: check for high and low surrogate */ - if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { - Py_UCS4 ch2 = s[i]; - /* Combine the two surrogates to form a UCS4 value */ - ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; - i++; - - /* Encode UCS4 Unicode ordinals */ - *p++ = (char)(0xf0 | (ch >> 18)); - *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); - *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); - *p++ = (char)(0x80 | (ch & 0x3f)); - } else { + Py_ssize_t newpos; + PyObject *rep; + Py_ssize_t repsize, k, startpos; + startpos = i-1; +#if SIZEOF_WCHAR_T == 2 + startpos += wchar_offset; #endif - Py_ssize_t newpos; - PyObject *rep; - Py_ssize_t repsize, k; - rep = unicode_encode_call_errorhandler - (errors, &errorHandler, "utf-8", "surrogates not allowed", - s, size, &exc, i-1, i, &newpos); - if (!rep) - goto error; + rep = unicode_encode_call_errorhandler( + errors, &errorHandler, "utf-8", "surrogates not allowed", + PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), + &exc, startpos, startpos+1, &newpos); + if (!rep) + goto error; + + if (PyBytes_Check(rep)) + repsize = PyBytes_GET_SIZE(rep); + else + repsize = PyUnicode_GET_SIZE(rep); - if (PyBytes_Check(rep)) - repsize = PyBytes_GET_SIZE(rep); - else - repsize = PyUnicode_GET_SIZE(rep); + if (repsize > 4) { + Py_ssize_t offset; - if (repsize > 4) { - Py_ssize_t offset; + if (result == NULL) + offset = p - stackbuf; + else + offset = p - PyBytes_AS_STRING(result); + if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { + /* integer overflow */ + PyErr_NoMemory(); + goto error; + } + nallocated += repsize - 4; + if (result != NULL) { + if (_PyBytes_Resize(&result, nallocated) < 0) + goto error; + } else { + result = PyBytes_FromStringAndSize(NULL, nallocated); if (result == NULL) - offset = p - stackbuf; - else - offset = p - PyBytes_AS_STRING(result); - - if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { - /* integer overflow */ - PyErr_NoMemory(); goto error; - } - nallocated += repsize - 4; - if (result != NULL) { - if (_PyBytes_Resize(&result, nallocated) < 0) - goto error; - } else { - result = PyBytes_FromStringAndSize(NULL, nallocated); - if (result == NULL) - goto error; - Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); - } - p = PyBytes_AS_STRING(result) + offset; + Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); } - - if (PyBytes_Check(rep)) { - char *prep = PyBytes_AS_STRING(rep); - for(k = repsize; k > 0; k--) - *p++ = *prep++; - } else /* rep is unicode */ { - Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); - Py_UNICODE c; - - for(k=0; k<repsize; k++) { - c = prep[k]; - if (0x80 <= c) { - raise_encode_exception(&exc, "utf-8", s, size, - i-1, i, "surrogates not allowed"); - goto error; - } - *p++ = (char)prep[k]; + p = PyBytes_AS_STRING(result) + offset; + } + + if (PyBytes_Check(rep)) { + char *prep = PyBytes_AS_STRING(rep); + for(k = repsize; k > 0; k--) + *p++ = *prep++; + } else /* rep is unicode */ { + const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); + Py_UNICODE c; + + for(k=0; k<repsize; k++) { + c = prep[k]; + if (0x80 <= c) { + raise_encode_exception(&exc, "utf-8", + PyUnicode_AS_UNICODE(unicode), + size, i-1, i, + "surrogates not allowed"); + goto error; } + *p++ = (char)prep[k]; } - Py_DECREF(rep); -#ifndef Py_UNICODE_WIDE } -#endif + Py_DECREF(rep); } else if (ch < 0x10000) { *p++ = (char)(0xe0 | (ch >> 12)); *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); @@ -3038,6 +4746,9 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s, *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); *p++ = (char)(0x80 | (ch & 0x3f)); +#if SIZEOF_WCHAR_T == 2 + wchar_offset++; +#endif } } @@ -3053,6 +4764,7 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s, assert(nneeded <= nallocated); _PyBytes_Resize(&result, nneeded); } + Py_XDECREF(errorHandler); Py_XDECREF(exc); return result; @@ -3065,15 +4777,25 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s, #undef MAX_SHORT_UNICHARS } -PyObject *PyUnicode_AsUTF8String(PyObject *unicode) +PyObject * +PyUnicode_EncodeUTF8(const Py_UNICODE *s, + Py_ssize_t size, + const char *errors) { - if (!PyUnicode_Check(unicode)) { - PyErr_BadArgument(); + PyObject *v, *unicode; + + unicode = PyUnicode_FromUnicode(s, size); + if (unicode == NULL) return NULL; - } - return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), - PyUnicode_GET_SIZE(unicode), - NULL); + v = _PyUnicode_AsUTF8String(unicode, errors); + Py_DECREF(unicode); + return v; +} + +PyObject * +PyUnicode_AsUTF8String(PyObject *unicode) +{ + return _PyUnicode_AsUTF8String(unicode, NULL); } /* --- UTF-32 Codec ------------------------------------------------------- */ @@ -3185,7 +4907,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s, return (PyObject *)unicode; /* Unpack UTF-32 encoded data */ - p = unicode->str; + p = PyUnicode_AS_UNICODE(unicode); while (q < e) { Py_UCS4 ch; @@ -3238,11 +4960,18 @@ PyUnicode_DecodeUTF32Stateful(const char *s, *consumed = (const char *)q-starts; /* Adjust length */ - if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) + if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&unicode)) { + Py_DECREF(unicode); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(unicode, 1)); return (PyObject *)unicode; onError: @@ -3339,7 +5068,8 @@ PyUnicode_EncodeUTF32(const Py_UNICODE *s, #undef STORECHAR } -PyObject *PyUnicode_AsUTF32String(PyObject *unicode) +PyObject * +PyUnicode_AsUTF32String(PyObject *unicode) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); @@ -3414,7 +5144,7 @@ PyUnicode_DecodeUTF16Stateful(const char *s, return (PyObject *)unicode; /* Unpack UTF-16 encoded data */ - p = unicode->str; + p = PyUnicode_AS_UNICODE(unicode); q = (unsigned char *)s; e = q + size - 1; @@ -3631,11 +5361,18 @@ PyUnicode_DecodeUTF16Stateful(const char *s, *consumed = (const char *)q-starts; /* Adjust length */ - if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) + if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&unicode)) { + Py_DECREF(unicode); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(unicode, 1)); return (PyObject *)unicode; onError: @@ -3729,7 +5466,8 @@ PyUnicode_EncodeUTF16(const Py_UNICODE *s, #undef STORECHAR } -PyObject *PyUnicode_AsUTF16String(PyObject *unicode) +PyObject * +PyUnicode_AsUTF16String(PyObject *unicode) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); @@ -3743,17 +5481,87 @@ PyObject *PyUnicode_AsUTF16String(PyObject *unicode) /* --- Unicode Escape Codec ----------------------------------------------- */ +/* Helper function for PyUnicode_DecodeUnicodeEscape, determines + if all the escapes in the string make it still a valid ASCII string. + Returns -1 if any escapes were found which cause the string to + pop out of ASCII range. Otherwise returns the length of the + required buffer to hold the string. + */ +Py_ssize_t +length_of_escaped_ascii_string(const char *s, Py_ssize_t size) +{ + const unsigned char *p = (const unsigned char *)s; + const unsigned char *end = p + size; + Py_ssize_t length = 0; + + if (size < 0) + return -1; + + for (; p < end; ++p) { + if (*p > 127) { + /* Non-ASCII */ + return -1; + } + else if (*p != '\\') { + /* Normal character */ + ++length; + } + else { + /* Backslash-escape, check next char */ + ++p; + /* Escape sequence reaches till end of string or + non-ASCII follow-up. */ + if (p >= end || *p > 127) + return -1; + switch (*p) { + case '\n': + /* backslash + \n result in zero characters */ + break; + case '\\': case '\'': case '\"': + case 'b': case 'f': case 't': + case 'n': case 'r': case 'v': case 'a': + ++length; + break; + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + case 'x': case 'u': case 'U': case 'N': + /* these do not guarantee ASCII characters */ + return -1; + default: + /* count the backslash + the other character */ + length += 2; + } + } + } + return length; +} + +/* Similar to PyUnicode_WRITE but either write into wstr field + or treat string as ASCII. */ +#define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ + do { \ + if ((kind) != PyUnicode_WCHAR_KIND) \ + ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ + else \ + ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ + } while (0) + +#define WRITE_WSTR(buf, index, value) \ + assert(kind == PyUnicode_WCHAR_KIND), \ + ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) + + static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; -PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeUnicodeEscape(const char *s, + Py_ssize_t size, + const char *errors) { const char *starts = s; Py_ssize_t startinpos; Py_ssize_t endinpos; - Py_ssize_t outpos; - int i; + int j; PyUnicodeObject *v; Py_UNICODE *p; const char *end; @@ -3761,19 +5569,42 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ PyObject *errorHandler = NULL; PyObject *exc = NULL; + Py_ssize_t ascii_length; + Py_ssize_t i; + int kind; + void *data; + + ascii_length = length_of_escaped_ascii_string(s, size); + + /* After length_of_escaped_ascii_string() there are two alternatives, + either the string is pure ASCII with named escapes like \n, etc. + and we determined it's exact size (common case) + or it contains \x, \u, ... escape sequences. then we create a + legacy wchar string and resize it at the end of this function. */ + if (ascii_length >= 0) { + v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); + if (!v) + goto onError; + assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); + kind = PyUnicode_1BYTE_KIND; + data = PyUnicode_DATA(v); + } + else { + /* Escaped strings will always be longer than the resulting + Unicode string, so we start with size here and then reduce the + length after conversion to the true value. + (but if the error callback returns a long replacement string + we'll have to allocate more space) */ + v = _PyUnicode_New(size); + if (!v) + goto onError; + kind = PyUnicode_WCHAR_KIND; + data = PyUnicode_AS_UNICODE(v); + } - /* Escaped strings will always be longer than the resulting - Unicode string, so we start with size here and then reduce the - length after conversion to the true value. - (but if the error callback returns a long replacement string - we'll have to allocate more space) */ - v = _PyUnicode_New(size); - if (v == NULL) - goto onError; if (size == 0) return (PyObject *)v; - - p = PyUnicode_AS_UNICODE(v); + i = 0; end = s + size; while (s < end) { @@ -3781,9 +5612,18 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, Py_UNICODE x; int digits; + if (kind == PyUnicode_WCHAR_KIND) { + assert(i < _PyUnicode_WSTR_LENGTH(v)); + } + else { + /* The only case in which i == ascii_length is a backslash + followed by a newline. */ + assert(i <= ascii_length); + } + /* Non-escape characters are interpreted as Unicode ordinals */ if (*s != '\\') { - *p++ = (unsigned char) *s++; + WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); continue; } @@ -3793,20 +5633,33 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, c = *s++; if (s > end) c = '\0'; /* Invalid after \ */ + + if (kind == PyUnicode_WCHAR_KIND) { + assert(i < _PyUnicode_WSTR_LENGTH(v)); + } + else { + /* The only case in which i == ascii_length is a backslash + followed by a newline. */ + assert(i < ascii_length || (i == ascii_length && c == '\n')); + } + switch (c) { /* \x escapes */ case '\n': break; - case '\\': *p++ = '\\'; break; - case '\'': *p++ = '\''; break; - case '\"': *p++ = '\"'; break; - case 'b': *p++ = '\b'; break; - case 'f': *p++ = '\014'; break; /* FF */ - case 't': *p++ = '\t'; break; - case 'n': *p++ = '\n'; break; - case 'r': *p++ = '\r'; break; - case 'v': *p++ = '\013'; break; /* VT */ - case 'a': *p++ = '\007'; break; /* BEL, not classic C */ + case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; + case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; + case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; + case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; + /* FF */ + case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; + case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; + case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; + case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; + /* VT */ + case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; + /* BEL, not classic C */ + case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; /* \OOO (octal) escapes */ case '0': case '1': case '2': case '3': @@ -3817,7 +5670,7 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, if (s < end && '0' <= *s && *s <= '7') x = (x<<3) + *s++ - '0'; } - *p++ = x; + WRITE_WSTR(data, i++, x); break; /* hex escapes */ @@ -3839,27 +5692,30 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, message = "truncated \\UXXXXXXXX escape"; hexescape: chr = 0; - outpos = p-PyUnicode_AS_UNICODE(v); + p = PyUnicode_AS_UNICODE(v) + i; if (s+digits>end) { endinpos = size; if (unicode_decode_call_errorhandler( errors, &errorHandler, "unicodeescape", "end of string in escape sequence", &starts, &end, &startinpos, &endinpos, &exc, &s, - &v, &outpos, &p)) + &v, &i, &p)) goto onError; + data = PyUnicode_AS_UNICODE(v); goto nextByte; } - for (i = 0; i < digits; ++i) { - c = (unsigned char) s[i]; + for (j = 0; j < digits; ++j) { + c = (unsigned char) s[j]; if (!Py_ISXDIGIT(c)) { - endinpos = (s+i+1)-starts; + endinpos = (s+j+1)-starts; + p = PyUnicode_AS_UNICODE(v) + i; if (unicode_decode_call_errorhandler( errors, &errorHandler, "unicodeescape", message, &starts, &end, &startinpos, &endinpos, &exc, &s, - &v, &outpos, &p)) + &v, &i, &p)) goto onError; + data = PyUnicode_AS_UNICODE(v); goto nextByte; } chr = (chr<<4) & ~0xF; @@ -3870,7 +5726,7 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, else chr += 10 + c - 'A'; } - s += i; + s += j; if (chr == 0xffffffff && PyErr_Occurred()) /* _decoding_error will have already written into the target buffer. */ @@ -3879,26 +5735,27 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, /* when we get here, chr is a 32-bit unicode character */ if (chr <= 0xffff) /* UCS-2 character */ - *p++ = (Py_UNICODE) chr; + WRITE_WSTR(data, i++, chr); else if (chr <= 0x10ffff) { /* UCS-4 character. Either store directly, or as surrogate pair. */ #ifdef Py_UNICODE_WIDE - *p++ = chr; + WRITE_WSTR(data, i++, chr); #else chr -= 0x10000L; - *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); - *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); + WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); + WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); #endif } else { endinpos = s-starts; - outpos = p-PyUnicode_AS_UNICODE(v); + p = PyUnicode_AS_UNICODE(v) + i; if (unicode_decode_call_errorhandler( errors, &errorHandler, "unicodeescape", "illegal Unicode character", &starts, &end, &startinpos, &endinpos, &exc, &s, - &v, &outpos, &p)) + &v, &i, &p)) goto onError; + data = PyUnicode_AS_UNICODE(v); } break; @@ -3907,7 +5764,8 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, message = "malformed \\N character escape"; if (ucnhash_CAPI == NULL) { /* load the unicode data module */ - ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); + ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( + PyUnicodeData_CAPSULE_NAME, 1); if (ucnhash_CAPI == NULL) goto ucnhashError; } @@ -3920,46 +5778,63 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, /* found a name. look it up in the unicode database */ message = "unknown Unicode character name"; s++; - if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) + if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), + &chr)) goto store; } } endinpos = s-starts; - outpos = p-PyUnicode_AS_UNICODE(v); + p = PyUnicode_AS_UNICODE(v) + i; if (unicode_decode_call_errorhandler( errors, &errorHandler, "unicodeescape", message, &starts, &end, &startinpos, &endinpos, &exc, &s, - &v, &outpos, &p)) + &v, &i, &p)) goto onError; + data = PyUnicode_AS_UNICODE(v); break; default: if (s > end) { + assert(kind == PyUnicode_WCHAR_KIND); message = "\\ at end of string"; s--; endinpos = s-starts; - outpos = p-PyUnicode_AS_UNICODE(v); + p = PyUnicode_AS_UNICODE(v) + i; if (unicode_decode_call_errorhandler( errors, &errorHandler, "unicodeescape", message, &starts, &end, &startinpos, &endinpos, &exc, &s, - &v, &outpos, &p)) + &v, &i, &p)) goto onError; + data = PyUnicode_AS_UNICODE(v); } else { - *p++ = '\\'; - *p++ = (unsigned char)s[-1]; + WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); + WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); } break; } nextByte: ; } - if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) - goto onError; + /* Ensure the length prediction worked in case of ASCII strings */ + assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); + + if (kind == PyUnicode_WCHAR_KIND) + { + if (PyUnicode_Resize((PyObject**)&v, i) < 0) + goto onError; + } Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&v)) { + Py_DECREF(v); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(v, 1)); return (PyObject *)v; ucnhashError: @@ -3979,6 +5854,9 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, return NULL; } +#undef WRITE_ASCII_OR_WSTR +#undef WRITE_WSTR + /* Return a Unicode-Escape string version of the Unicode object. If quotes is true, the string is enclosed in u"" or u'' quotes as @@ -3986,25 +5864,11 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, */ -Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, - Py_ssize_t size, - Py_UNICODE ch) -{ - /* like wcschr, but doesn't stop at NULL characters */ - - while (size-- > 0) { - if (*s == ch) - return s; - s++; - } - - return NULL; -} - static const char *hexdigits = "0123456789abcdef"; -PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, - Py_ssize_t size) +PyObject * +PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, + Py_ssize_t size) { PyObject *repr; char *p; @@ -4144,7 +6008,8 @@ PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, return repr; } -PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) +PyObject * +PyUnicode_AsUnicodeEscapeString(PyObject *unicode) { PyObject *s; if (!PyUnicode_Check(unicode)) { @@ -4158,9 +6023,10 @@ PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) /* --- Raw Unicode Escape Codec ------------------------------------------- */ -PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeRawUnicodeEscape(const char *s, + Py_ssize_t size, + const char *errors) { const char *starts = s; Py_ssize_t startinpos; @@ -4262,10 +6128,17 @@ PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, nextByte: ; } - if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) + if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&v)) { + Py_DECREF(v); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(v, 1)); return (PyObject *)v; onError: @@ -4275,8 +6148,9 @@ PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, return NULL; } -PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, - Py_ssize_t size) +PyObject * +PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, + Py_ssize_t size) { PyObject *repr; char *p; @@ -4363,7 +6237,8 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, return repr; } -PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) +PyObject * +PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) { PyObject *s; if (!PyUnicode_Check(unicode)) { @@ -4378,9 +6253,10 @@ PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) /* --- Unicode Internal Codec ------------------------------------------- */ -PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +_PyUnicode_DecodeUnicodeInternal(const char *s, + Py_ssize_t size, + const char *errors) { const char *starts = s; Py_ssize_t startinpos; @@ -4401,7 +6277,9 @@ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); if (v == NULL) goto onError; - if (PyUnicode_GetSize((PyObject *)v) == 0) + /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH + as string was created with the old API. */ + if (PyUnicode_GET_SIZE(v) == 0) return (PyObject *)v; p = PyUnicode_AS_UNICODE(v); end = s + size; @@ -4441,10 +6319,17 @@ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, } } - if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) + if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&v)) { + Py_DECREF(v); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(v, 1)); return (PyObject *)v; onError: @@ -4456,53 +6341,22 @@ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, /* --- Latin-1 Codec ------------------------------------------------------ */ -PyObject *PyUnicode_DecodeLatin1(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeLatin1(const char *s, + Py_ssize_t size, + const char *errors) { - PyUnicodeObject *v; - Py_UNICODE *p; - const char *e, *unrolled_end; - /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ - if (size == 1) { - Py_UNICODE r = *(unsigned char*)s; - return PyUnicode_FromUnicode(&r, 1); - } - - v = _PyUnicode_New(size); - if (v == NULL) - goto onError; - if (size == 0) - return (PyObject *)v; - p = PyUnicode_AS_UNICODE(v); - e = s + size; - /* Unrolling the copy makes it much faster by reducing the looping - overhead. This is similar to what many memcpy() implementations do. */ - unrolled_end = e - 4; - while (s < unrolled_end) { - p[0] = (unsigned char) s[0]; - p[1] = (unsigned char) s[1]; - p[2] = (unsigned char) s[2]; - p[3] = (unsigned char) s[3]; - s += 4; - p += 4; - } - while (s < e) - *p++ = (unsigned char) *s++; - return (PyObject *)v; - - onError: - Py_XDECREF(v); - return NULL; + return _PyUnicode_FromUCS1((unsigned char*)s, size); } /* create or adjust a UnicodeEncodeError */ -static void make_encode_exception(PyObject **exceptionObject, - const char *encoding, - const Py_UNICODE *unicode, Py_ssize_t size, - Py_ssize_t startpos, Py_ssize_t endpos, - const char *reason) +static void +make_encode_exception(PyObject **exceptionObject, + const char *encoding, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason) { if (*exceptionObject == NULL) { *exceptionObject = PyUnicodeEncodeError_Create( @@ -4523,11 +6377,12 @@ static void make_encode_exception(PyObject **exceptionObject, } /* raises a UnicodeEncodeError */ -static void raise_encode_exception(PyObject **exceptionObject, - const char *encoding, - const Py_UNICODE *unicode, Py_ssize_t size, - Py_ssize_t startpos, Py_ssize_t endpos, - const char *reason) +static void +raise_encode_exception(PyObject **exceptionObject, + const char *encoding, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason) { make_encode_exception(exceptionObject, encoding, unicode, size, startpos, endpos, reason); @@ -4539,12 +6394,13 @@ static void raise_encode_exception(PyObject **exceptionObject, 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(const char *errors, - PyObject **errorHandler, - const char *encoding, const char *reason, - const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, - Py_ssize_t startpos, Py_ssize_t endpos, - Py_ssize_t *newpos) +static PyObject * +unicode_encode_call_errorhandler(const char *errors, + PyObject **errorHandler, + const char *encoding, const char *reason, + const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, + Py_ssize_t startpos, Py_ssize_t endpos, + Py_ssize_t *newpos) { static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; @@ -4593,10 +6449,11 @@ static PyObject *unicode_encode_call_errorhandler(const char *errors, return resunicode; } -static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, - Py_ssize_t size, - const char *errors, - int limit) +static PyObject * +unicode_encode_ucs1(const Py_UNICODE *p, + Py_ssize_t size, + const char *errors, + int limit) { /* output object */ PyObject *res; @@ -4789,76 +6646,135 @@ static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, return NULL; } -PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_EncodeLatin1(const Py_UNICODE *p, + Py_ssize_t size, + const char *errors) { return unicode_encode_ucs1(p, size, errors, 256); } -PyObject *PyUnicode_AsLatin1String(PyObject *unicode) +PyObject * +_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); return NULL; } + if (PyUnicode_READY(unicode) == -1) + return NULL; + /* Fast path: if it is a one-byte string, construct + bytes object directly. */ + if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) + return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), + PyUnicode_GET_LENGTH(unicode)); + /* Non-Latin-1 characters present. Defer to above function to + raise the exception. */ return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), - NULL); + errors); +} + +PyObject* +PyUnicode_AsLatin1String(PyObject *unicode) +{ + return _PyUnicode_AsLatin1String(unicode, NULL); } /* --- 7-bit ASCII Codec -------------------------------------------------- */ -PyObject *PyUnicode_DecodeASCII(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeASCII(const char *s, + Py_ssize_t size, + const char *errors) { const char *starts = s; PyUnicodeObject *v; - Py_UNICODE *p; + Py_UNICODE *u; Py_ssize_t startinpos; Py_ssize_t endinpos; Py_ssize_t outpos; const char *e; + int has_error; + const unsigned char *p = (const unsigned char *)s; + const unsigned char *end = p + size; + const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); PyObject *errorHandler = NULL; PyObject *exc = NULL; /* ASCII is equivalent to the first 128 ordinals in Unicode. */ - if (size == 1 && *(unsigned char*)s < 128) { - Py_UNICODE r = *(unsigned char*)s; - return PyUnicode_FromUnicode(&r, 1); + if (size == 1 && (unsigned char)s[0] < 128) + return get_latin1_char((unsigned char)s[0]); + + has_error = 0; + while (p < end && !has_error) { + /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for + an explanation. */ + if (!((size_t) p & LONG_PTR_MASK)) { + /* Help register allocation */ + register const unsigned char *_p = p; + while (_p < aligned_end) { + unsigned long value = *(unsigned long *) _p; + if (value & ASCII_CHAR_MASK) { + has_error = 1; + break; + } + _p += SIZEOF_LONG; + } + if (_p == end) + break; + if (has_error) + break; + p = _p; + } + if (*p & 0x80) { + has_error = 1; + break; + } + else { + ++p; + } } + if (!has_error) + return unicode_fromascii((const unsigned char *)s, size); v = _PyUnicode_New(size); if (v == NULL) goto onError; if (size == 0) return (PyObject *)v; - p = PyUnicode_AS_UNICODE(v); + u = PyUnicode_AS_UNICODE(v); e = s + size; while (s < e) { register unsigned char c = (unsigned char)*s; if (c < 128) { - *p++ = c; + *u++ = c; ++s; } else { startinpos = s-starts; endinpos = startinpos + 1; - outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); + outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); if (unicode_decode_call_errorhandler( errors, &errorHandler, "ascii", "ordinal not in range(128)", &starts, &e, &startinpos, &endinpos, &exc, &s, - &v, &outpos, &p)) + &v, &outpos, &u)) goto onError; } } - if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) - if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) + if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) + if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&v)) { + Py_DECREF(v); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(v, 1)); return (PyObject *)v; onError: @@ -4868,25 +6784,40 @@ PyObject *PyUnicode_DecodeASCII(const char *s, return NULL; } -PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_EncodeASCII(const Py_UNICODE *p, + Py_ssize_t size, + const char *errors) { return unicode_encode_ucs1(p, size, errors, 128); } -PyObject *PyUnicode_AsASCIIString(PyObject *unicode) +PyObject * +_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); return NULL; } + if (PyUnicode_READY(unicode) == -1) + return NULL; + /* Fast path: if it is an ASCII-only string, construct bytes object + directly. Else defer to above function to raise the exception. */ + if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) + return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), + PyUnicode_GET_LENGTH(unicode)); return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), - NULL); + errors); +} + +PyObject * +PyUnicode_AsASCIIString(PyObject *unicode) +{ + return _PyUnicode_AsASCIIString(unicode, NULL); } -#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) +#ifdef HAVE_MBCS /* --- MBCS codecs for Windows -------------------------------------------- */ @@ -4899,7 +6830,8 @@ PyObject *PyUnicode_AsASCIIString(PyObject *unicode) b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte encodings, see IsDBCSLeadByteEx documentation. */ -static int is_dbcs_lead_byte(const char *s, int offset) +static int +is_dbcs_lead_byte(const char *s, int offset) { const char *curr = s + offset; @@ -4914,11 +6846,12 @@ static int is_dbcs_lead_byte(const char *s, int offset) * Decode MBCS string into unicode object. If 'final' is set, converts * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. */ -static int decode_mbcs(PyUnicodeObject **v, - const char *s, /* MBCS string */ - int size, /* sizeof MBCS string */ - int final, - const char *errors) +static int +decode_mbcs(PyUnicodeObject **v, + const char *s, /* MBCS string */ + int size, /* sizeof MBCS string */ + int final, + const char *errors) { Py_UNICODE *p; Py_ssize_t n; @@ -4961,7 +6894,7 @@ static int decode_mbcs(PyUnicodeObject **v, else { /* Extend unicode object */ n = PyUnicode_GET_SIZE(*v); - if (_PyUnicode_Resize(v, n + usize) < 0) + if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) return -1; } @@ -4997,10 +6930,11 @@ mbcs_decode_error: return -1; } -PyObject *PyUnicode_DecodeMBCSStateful(const char *s, - Py_ssize_t size, - const char *errors, - Py_ssize_t *consumed) +PyObject * +PyUnicode_DecodeMBCSStateful(const char *s, + Py_ssize_t size, + const char *errors, + Py_ssize_t *consumed) { PyUnicodeObject *v = NULL; int done; @@ -5031,13 +6965,20 @@ PyObject *PyUnicode_DecodeMBCSStateful(const char *s, goto retry; } #endif - +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&v)) { + Py_DECREF(v); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(v, 1)); return (PyObject *)v; } -PyObject *PyUnicode_DecodeMBCS(const char *s, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_DecodeMBCS(const char *s, + Py_ssize_t size, + const char *errors) { return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); } @@ -5046,10 +6987,11 @@ PyObject *PyUnicode_DecodeMBCS(const char *s, * Convert unicode into string object (MBCS). * Returns 0 if succeed, -1 otherwise. */ -static int encode_mbcs(PyObject **repr, - const Py_UNICODE *p, /* unicode */ - int size, /* size of unicode */ - const char* errors) +static int +encode_mbcs(PyObject **repr, + const Py_UNICODE *p, /* unicode */ + int size, /* size of unicode */ + const char* errors) { BOOL usedDefaultChar = FALSE; BOOL *pusedDefaultChar; @@ -5122,9 +7064,10 @@ mbcs_encode_error: return -1; } -PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, - Py_ssize_t size, - const char *errors) +PyObject * +PyUnicode_EncodeMBCS(const Py_UNICODE *p, + Py_ssize_t size, + const char *errors) { PyObject *repr = NULL; int ret; @@ -5153,7 +7096,8 @@ PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, return repr; } -PyObject *PyUnicode_AsMBCSString(PyObject *unicode) +PyObject * +PyUnicode_AsMBCSString(PyObject *unicode) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); @@ -5166,14 +7110,15 @@ PyObject *PyUnicode_AsMBCSString(PyObject *unicode) #undef NEED_RETRY -#endif /* MS_WINDOWS */ +#endif /* HAVE_MBCS */ /* --- Character Mapping Codec -------------------------------------------- */ -PyObject *PyUnicode_DecodeCharmap(const char *s, - Py_ssize_t size, - PyObject *mapping, - const char *errors) +PyObject * +PyUnicode_DecodeCharmap(const char *s, + Py_ssize_t size, + PyObject *mapping, + const char *errors) { const char *starts = s; Py_ssize_t startinpos; @@ -5291,7 +7236,7 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, (targetsize << 2); extrachars += needed; /* XXX overflow detection missing */ - if (_PyUnicode_Resize(&v, + if (PyUnicode_Resize((PyObject**)&v, PyUnicode_GET_SIZE(v) + needed) < 0) { Py_DECREF(x); goto onError; @@ -5318,10 +7263,17 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, } } if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) - if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) + if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) goto onError; Py_XDECREF(errorHandler); Py_XDECREF(exc); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&v)) { + Py_DECREF(v); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(v, 1)); return (PyObject *)v; onError: @@ -5333,7 +7285,7 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, /* Charmap encoding: the lookup table */ -struct encoding_map{ +struct encoding_map { PyObject_HEAD unsigned char level1[32]; int count2, count3; @@ -5407,7 +7359,6 @@ static PyTypeObject EncodingMapType = { PyObject* PyUnicode_BuildEncodingMap(PyObject* string) { - Py_UNICODE *decode; PyObject *result; struct encoding_map *mresult; int i; @@ -5416,35 +7367,36 @@ PyUnicode_BuildEncodingMap(PyObject* string) unsigned char level2[512]; unsigned char *mlevel1, *mlevel2, *mlevel3; int count2 = 0, count3 = 0; + int kind; + void *data; + Py_UCS4 ch; - if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { + if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { PyErr_BadArgument(); return NULL; } - decode = PyUnicode_AS_UNICODE(string); + kind = PyUnicode_KIND(string); + data = PyUnicode_DATA(string); memset(level1, 0xFF, sizeof level1); memset(level2, 0xFF, sizeof level2); /* If there isn't a one-to-one mapping of NULL to \0, or if there are non-BMP characters, we need to use a mapping dictionary. */ - if (decode[0] != 0) + if (PyUnicode_READ(kind, data, 0) != 0) need_dict = 1; for (i = 1; i < 256; i++) { int l1, l2; - if (decode[i] == 0 -#ifdef Py_UNICODE_WIDE - || decode[i] > 0xFFFF -#endif - ) { + ch = PyUnicode_READ(kind, data, i); + if (ch == 0 || ch > 0xFFFF) { need_dict = 1; break; } - if (decode[i] == 0xFFFE) + if (ch == 0xFFFE) /* unmapped character */ continue; - l1 = decode[i] >> 11; - l2 = decode[i] >> 7; + l1 = ch >> 11; + l2 = ch >> 7; if (level1[l1] == 0xFF) level1[l1] = count2++; if (level2[l2] == 0xFF) @@ -5460,8 +7412,7 @@ PyUnicode_BuildEncodingMap(PyObject* string) if (!result) return NULL; for (i = 0; i < 256; i++) { - key = value = NULL; - key = PyLong_FromLong(decode[i]); + key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); value = PyLong_FromLong(i); if (!key || !value) goto failed1; @@ -5496,15 +7447,15 @@ PyUnicode_BuildEncodingMap(PyObject* string) count3 = 0; for (i = 1; i < 256; i++) { int o1, o2, o3, i2, i3; - if (decode[i] == 0xFFFE) + if (PyUnicode_READ(kind, data, i) == 0xFFFE) /* unmapped character */ continue; - o1 = decode[i]>>11; - o2 = (decode[i]>>7) & 0xF; + o1 = PyUnicode_READ(kind, data, i)>>11; + o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; i2 = 16*mlevel1[o1] + o2; if (mlevel2[i2] == 0xFF) mlevel2[i2] = count3++; - o3 = decode[i] & 0x7F; + o3 = PyUnicode_READ(kind, data, i) & 0x7F; i3 = 128*mlevel2[i2] + o3; mlevel3[i3] = i; } @@ -5548,7 +7499,8 @@ encoding_map_lookup(Py_UNICODE c, PyObject *mapping) /* Lookup the character ch in the mapping. If the character can't be found, Py_None is returned (or NULL, if another error occurred). */ -static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) +static PyObject * +charmapencode_lookup(Py_UNICODE c, PyObject *mapping) { PyObject *w = PyLong_FromLong((long)c); PyObject *x; @@ -5605,16 +7557,16 @@ charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requireds typedef enum charmapencode_result { enc_SUCCESS, enc_FAILED, enc_EXCEPTION -}charmapencode_result; +} charmapencode_result; /* lookup the character, put the result in the output string and adjust various state variables. Resize the output bytes object if not enough space is available. Return a new reference to the object that was put in the output buffer, or Py_None, if the mapping was undefined (in which case no character was written) or NULL, if a reallocation error occurred. The caller must decref the result */ -static -charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, - PyObject **outobj, Py_ssize_t *outpos) +static charmapencode_result +charmapencode_output(Py_UNICODE c, PyObject *mapping, + PyObject **outobj, Py_ssize_t *outpos) { PyObject *rep; char *outstart; @@ -5670,8 +7622,8 @@ charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, /* handle an error in PyUnicode_EncodeCharmap Return 0 on success, -1 on error */ -static -int charmap_encoding_error( +static int +charmap_encoding_error( const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, PyObject **exceptionObject, int *known_errorHandler, PyObject **errorHandler, const char *errors, @@ -5805,10 +7757,11 @@ int charmap_encoding_error( return 0; } -PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, - Py_ssize_t size, - PyObject *mapping, - const char *errors) +PyObject * +PyUnicode_EncodeCharmap(const Py_UNICODE *p, + Py_ssize_t size, + PyObject *mapping, + const char *errors) { /* output object */ PyObject *res = NULL; @@ -5869,8 +7822,9 @@ PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, return NULL; } -PyObject *PyUnicode_AsCharmapString(PyObject *unicode, - PyObject *mapping) +PyObject * +PyUnicode_AsCharmapString(PyObject *unicode, + PyObject *mapping) { if (!PyUnicode_Check(unicode) || mapping == NULL) { PyErr_BadArgument(); @@ -5883,14 +7837,15 @@ PyObject *PyUnicode_AsCharmapString(PyObject *unicode, } /* create or adjust a UnicodeTranslateError */ -static void make_translate_exception(PyObject **exceptionObject, - const Py_UNICODE *unicode, Py_ssize_t size, - Py_ssize_t startpos, Py_ssize_t endpos, - const char *reason) +static void +make_translate_exception(PyObject **exceptionObject, + PyObject *unicode, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason) { if (*exceptionObject == NULL) { - *exceptionObject = PyUnicodeTranslateError_Create( - unicode, size, startpos, endpos, reason); + *exceptionObject = _PyUnicodeTranslateError_Create( + unicode, startpos, endpos, reason); } else { if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) @@ -5907,13 +7862,14 @@ static void make_translate_exception(PyObject **exceptionObject, } /* raises a UnicodeTranslateError */ -static void raise_translate_exception(PyObject **exceptionObject, - const Py_UNICODE *unicode, Py_ssize_t size, - Py_ssize_t startpos, Py_ssize_t endpos, - const char *reason) +static void +raise_translate_exception(PyObject **exceptionObject, + PyObject *unicode, + Py_ssize_t startpos, Py_ssize_t endpos, + const char *reason) { make_translate_exception(exceptionObject, - unicode, size, startpos, endpos, reason); + unicode, startpos, endpos, reason); if (*exceptionObject != NULL) PyCodec_StrictErrors(*exceptionObject); } @@ -5922,12 +7878,13 @@ static void raise_translate_exception(PyObject **exceptionObject, 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_translate_call_errorhandler(const char *errors, - PyObject **errorHandler, - const char *reason, - const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, - Py_ssize_t startpos, Py_ssize_t endpos, - Py_ssize_t *newpos) +static PyObject * +unicode_translate_call_errorhandler(const char *errors, + PyObject **errorHandler, + const char *reason, + PyObject *unicode, PyObject **exceptionObject, + Py_ssize_t startpos, Py_ssize_t endpos, + Py_ssize_t *newpos) { static char *argparse = "O!n;translating error handler must return (str, int) tuple"; @@ -5942,7 +7899,7 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors, } make_translate_exception(exceptionObject, - unicode, size, startpos, endpos, reason); + unicode, startpos, endpos, reason); if (*exceptionObject == NULL) return NULL; @@ -5961,10 +7918,10 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors, return NULL; } if (i_newpos<0) - *newpos = size+i_newpos; + *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; else *newpos = i_newpos; - if (*newpos<0 || *newpos>size) { + if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); Py_DECREF(restuple); return NULL; @@ -5977,8 +7934,8 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors, /* Lookup the character ch in the mapping and put the result in result, which must be decrefed by the caller. Return 0 on success, -1 on error */ -static -int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) +static int +charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) { PyObject *w = PyLong_FromLong((long)c); PyObject *x; @@ -6027,20 +7984,19 @@ int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) /* ensure that *outobj is at least requiredsize characters long, if not reallocate and adjust various state variables. Return 0 on success, -1 on error */ -static -int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, +static int +charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, Py_ssize_t requiredsize) { - Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); + Py_ssize_t oldsize = *psize; if (requiredsize > oldsize) { - /* remember old output position */ - Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); /* exponentially overallocate to minimize reallocations */ if (requiredsize < 2 * oldsize) requiredsize = 2 * oldsize; - if (PyUnicode_Resize(outobj, requiredsize) < 0) + *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); + if (*outobj == 0) return -1; - *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; + *psize = requiredsize; } return 0; } @@ -6050,38 +8006,44 @@ int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, undefined (in which case no character was written). The called must decref result. Return 0 on success, -1 on error. */ -static -int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, - Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, - PyObject **res) +static int +charmaptranslate_output(PyObject *input, Py_ssize_t ipos, + PyObject *mapping, Py_UCS4 **output, + Py_ssize_t *osize, Py_ssize_t *opos, + PyObject **res) { - if (charmaptranslate_lookup(*curinp, mapping, res)) + Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); + if (charmaptranslate_lookup(curinp, mapping, res)) return -1; if (*res==NULL) { /* not found => default to 1:1 mapping */ - *(*outp)++ = *curinp; + (*output)[(*opos)++] = curinp; } else if (*res==Py_None) ; else if (PyLong_Check(*res)) { /* no overflow check, because we know that the space is enough */ - *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); + (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); } else if (PyUnicode_Check(*res)) { - Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); + Py_ssize_t repsize; + if (PyUnicode_READY(*res) == -1) + return -1; + repsize = PyUnicode_GET_LENGTH(*res); if (repsize==1) { /* no overflow check, because we know that the space is enough */ - *(*outp)++ = *PyUnicode_AS_UNICODE(*res); + (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); } else if (repsize!=0) { /* more than one character */ - Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + - (insize - (curinp-startinp)) + + Py_ssize_t requiredsize = *opos + + (PyUnicode_GET_LENGTH(input) - ipos) + repsize - 1; - if (charmaptranslate_makespace(outobj, outp, requiredsize)) + Py_ssize_t i; + if (charmaptranslate_makespace(output, osize, requiredsize)) return -1; - memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); - *outp += repsize; + for(i = 0; i < repsize; i++) + (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); } } else @@ -6089,20 +8051,21 @@ int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp return 0; } -PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, - Py_ssize_t size, - PyObject *mapping, - const char *errors) +PyObject * +_PyUnicode_TranslateCharmap(PyObject *input, + PyObject *mapping, + const char *errors) { - /* output object */ - PyObject *res = NULL; - /* pointers to the beginning and end+1 of input */ - const Py_UNICODE *startp = p; - const Py_UNICODE *endp = p + size; - /* pointer into the output */ - Py_UNICODE *str; + /* input object */ + char *idata; + Py_ssize_t size, i; + int kind; + /* output buffer */ + Py_UCS4 *output = NULL; + Py_ssize_t osize; + PyObject *res; /* current output position */ - Py_ssize_t respos = 0; + Py_ssize_t opos; char *reason = "character maps to <undefined>"; PyObject *errorHandler = NULL; PyObject *exc = NULL; @@ -6116,38 +8079,52 @@ PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, return NULL; } + if (PyUnicode_READY(input) == -1) + return NULL; + idata = (char*)PyUnicode_DATA(input); + kind = PyUnicode_KIND(input); + size = PyUnicode_GET_LENGTH(input); + i = 0; + + if (size == 0) { + Py_INCREF(input); + return input; + } + /* allocate enough for a simple 1:1 translation without replacements, if we need more, we'll resize */ - res = PyUnicode_FromUnicode(NULL, size); - if (res == NULL) + osize = size; + output = PyMem_Malloc(osize * sizeof(Py_UCS4)); + opos = 0; + if (output == NULL) { + PyErr_NoMemory(); goto onError; - if (size == 0) - return res; - str = PyUnicode_AS_UNICODE(res); + } - while (p<endp) { + while (i<size) { /* try to encode it */ PyObject *x = NULL; - if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { + if (charmaptranslate_output(input, i, mapping, + &output, &osize, &opos, &x)) { Py_XDECREF(x); goto onError; } Py_XDECREF(x); if (x!=Py_None) /* it worked => adjust input pointer */ - ++p; + ++i; else { /* untranslatable character */ PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ Py_ssize_t repsize; Py_ssize_t newpos; - Py_UNICODE *uni2; + Py_ssize_t uni2; /* startpos for collecting untranslatable chars */ - const Py_UNICODE *collstart = p; - const Py_UNICODE *collend = p+1; - const Py_UNICODE *coll; + Py_ssize_t collstart = i; + Py_ssize_t collend = i+1; + Py_ssize_t coll; /* find all untranslatable characters */ - while (collend < endp) { - if (charmaptranslate_lookup(*collend, mapping, &x)) + while (collend < size) { + if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) goto onError; Py_XDECREF(x); if (x!=Py_None) @@ -6170,80 +8147,90 @@ PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, } switch (known_errorHandler) { case 1: /* strict */ - raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); + raise_translate_exception(&exc, input, collstart, + collend, reason); goto onError; case 2: /* replace */ /* No need to check for space, this is a 1:1 replacement */ - for (coll = collstart; coll<collend; ++coll) - *str++ = '?'; + for (coll = collstart; coll<collend; coll++) + output[opos++] = '?'; /* fall through */ case 3: /* ignore */ - p = collend; + i = collend; break; case 4: /* xmlcharrefreplace */ - /* generate replacement (temporarily (mis)uses p) */ - for (p = collstart; p < collend; ++p) { + /* generate replacement (temporarily (mis)uses i) */ + for (i = collstart; i < collend; ++i) { char buffer[2+29+1+1]; char *cp; - sprintf(buffer, "&#%d;", (int)*p); - if (charmaptranslate_makespace(&res, &str, - (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) + sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); + if (charmaptranslate_makespace(&output, &osize, + opos+strlen(buffer)+(size-collend))) goto onError; for (cp = buffer; *cp; ++cp) - *str++ = *cp; + output[opos++] = *cp; } - p = collend; + i = collend; break; default: repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, - reason, startp, size, &exc, - collstart-startp, collend-startp, &newpos); - if (repunicode == NULL) + reason, input, &exc, + collstart, collend, &newpos); + if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) goto onError; /* generate replacement */ - repsize = PyUnicode_GET_SIZE(repunicode); - if (charmaptranslate_makespace(&res, &str, - (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { + repsize = PyUnicode_GET_LENGTH(repunicode); + if (charmaptranslate_makespace(&output, &osize, + opos+repsize+(size-collend))) { Py_DECREF(repunicode); goto onError; } - for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) - *str++ = *uni2; - p = startp + newpos; + for (uni2 = 0; repsize-->0; ++uni2) + output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); + i = newpos; Py_DECREF(repunicode); } } } - /* Resize if we allocated to much */ - respos = str-PyUnicode_AS_UNICODE(res); - if (respos<PyUnicode_GET_SIZE(res)) { - if (PyUnicode_Resize(&res, respos) < 0) - goto onError; - } + res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); + if (!res) + goto onError; + PyMem_Free(output); Py_XDECREF(exc); Py_XDECREF(errorHandler); return res; onError: - Py_XDECREF(res); + PyMem_Free(output); Py_XDECREF(exc); Py_XDECREF(errorHandler); return NULL; } -PyObject *PyUnicode_Translate(PyObject *str, - PyObject *mapping, - const char *errors) +/* Deprecated. Use PyUnicode_Translate instead. */ +PyObject * +PyUnicode_TranslateCharmap(const Py_UNICODE *p, + Py_ssize_t size, + PyObject *mapping, + const char *errors) +{ + PyObject *unicode = PyUnicode_FromUnicode(p, size); + if (!unicode) + return NULL; + return _PyUnicode_TranslateCharmap(unicode, mapping, errors); +} + +PyObject * +PyUnicode_Translate(PyObject *str, + PyObject *mapping, + const char *errors) { PyObject *result; str = PyUnicode_FromObject(str); if (str == NULL) goto onError; - result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), - PyUnicode_GET_SIZE(str), - mapping, - errors); + result = _PyUnicode_TranslateCharmap(str, mapping, errors); Py_DECREF(str); return result; @@ -6252,6 +8239,60 @@ PyObject *PyUnicode_Translate(PyObject *str, return NULL; } +static Py_UCS4 +fix_decimal_and_space_to_ascii(PyObject *self) +{ + /* No need to call PyUnicode_READY(self) because this function is only + called as a callback from fixup() which does it already. */ + const Py_ssize_t len = PyUnicode_GET_LENGTH(self); + const int kind = PyUnicode_KIND(self); + void *data = PyUnicode_DATA(self); + Py_UCS4 maxchar = 0, ch, fixed; + Py_ssize_t i; + + for (i = 0; i < len; ++i) { + ch = PyUnicode_READ(kind, data, i); + fixed = 0; + if (ch > 127) { + if (Py_UNICODE_ISSPACE(ch)) + fixed = ' '; + else { + const int decimal = Py_UNICODE_TODECIMAL(ch); + if (decimal >= 0) + fixed = '0' + decimal; + } + if (fixed != 0) { + if (fixed > maxchar) + maxchar = fixed; + PyUnicode_WRITE(kind, data, i, fixed); + } + else if (ch > maxchar) + maxchar = ch; + } + else if (ch > maxchar) + maxchar = ch; + } + + return maxchar; +} + +PyObject * +_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) +{ + if (!PyUnicode_Check(unicode)) { + PyErr_BadInternalCall(); + return NULL; + } + if (PyUnicode_READY(unicode) == -1) + return NULL; + if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { + /* If the string is already ASCII, just return the same string */ + Py_INCREF(unicode); + return unicode; + } + return fixup(unicode, fix_decimal_and_space_to_ascii); +} + PyObject * PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, Py_ssize_t length) @@ -6274,14 +8315,22 @@ PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, p[i] = '0' + decimal; } } +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&result)) { + Py_DECREF(result); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(result, 1)); return result; } /* --- Decimal Encoder ---------------------------------------------------- */ -int PyUnicode_EncodeDecimal(Py_UNICODE *s, - Py_ssize_t length, - char *output, - const char *errors) +int +PyUnicode_EncodeDecimal(Py_UNICODE *s, + Py_ssize_t length, + char *output, + const char *errors) { Py_UNICODE *p, *end; PyObject *errorHandler = NULL; @@ -6415,17 +8464,143 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s, /* --- Helpers ------------------------------------------------------------ */ -#include "stringlib/unicodedefs.h" +#include "stringlib/asciilib.h" #include "stringlib/fastsearch.h" +#include "stringlib/partition.h" +#include "stringlib/split.h" +#include "stringlib/count.h" +#include "stringlib/find.h" +#include "stringlib/localeutil.h" +#include "stringlib/undef.h" +#include "stringlib/ucs1lib.h" +#include "stringlib/fastsearch.h" +#include "stringlib/partition.h" +#include "stringlib/split.h" #include "stringlib/count.h" #include "stringlib/find.h" +#include "stringlib/localeutil.h" +#include "stringlib/undef.h" + +#include "stringlib/ucs2lib.h" +#include "stringlib/fastsearch.h" #include "stringlib/partition.h" #include "stringlib/split.h" +#include "stringlib/count.h" +#include "stringlib/find.h" +#include "stringlib/localeutil.h" +#include "stringlib/undef.h" -#define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping -#define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale +#include "stringlib/ucs4lib.h" +#include "stringlib/fastsearch.h" +#include "stringlib/partition.h" +#include "stringlib/split.h" +#include "stringlib/count.h" +#include "stringlib/find.h" #include "stringlib/localeutil.h" +#include "stringlib/undef.h" + +static Py_ssize_t +any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ascii)(const Py_UCS1*, Py_ssize_t, + const Py_UCS1*, Py_ssize_t, + Py_ssize_t, Py_ssize_t), + Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, + const Py_UCS1*, Py_ssize_t, + Py_ssize_t, Py_ssize_t), + Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, + const Py_UCS2*, Py_ssize_t, + Py_ssize_t, Py_ssize_t), + Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, + const Py_UCS4*, Py_ssize_t, + Py_ssize_t, Py_ssize_t), + PyObject* s1, PyObject* s2, + Py_ssize_t start, + Py_ssize_t end) +{ + int kind1, kind2, kind; + void *buf1, *buf2; + Py_ssize_t len1, len2, result; + + kind1 = PyUnicode_KIND(s1); + kind2 = PyUnicode_KIND(s2); + kind = kind1 > kind2 ? kind1 : kind2; + buf1 = PyUnicode_DATA(s1); + buf2 = PyUnicode_DATA(s2); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind(s1, kind); + if (!buf1) + return -2; + if (kind2 != kind) + buf2 = _PyUnicode_AsKind(s2, kind); + if (!buf2) { + if (kind1 != kind) PyMem_Free(buf1); + return -2; + } + len1 = PyUnicode_GET_LENGTH(s1); + len2 = PyUnicode_GET_LENGTH(s2); + + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) + result = ascii(buf1, len1, buf2, len2, start, end); + else + result = ucs1(buf1, len1, buf2, len2, start, end); + break; + case PyUnicode_2BYTE_KIND: + result = ucs2(buf1, len1, buf2, len2, start, end); + break; + case PyUnicode_4BYTE_KIND: + result = ucs4(buf1, len1, buf2, len2, start, end); + break; + default: + assert(0); result = -2; + } + + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); + + return result; +} + +Py_ssize_t +_PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, + Py_ssize_t n_buffer, + void *digits, Py_ssize_t n_digits, + Py_ssize_t min_width, + const char *grouping, + const char *thousands_sep) +{ + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) + return _PyUnicode_ascii_InsertThousandsGrouping( + (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, + min_width, grouping, thousands_sep); + else + return _PyUnicode_ucs1_InsertThousandsGrouping( + (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, + min_width, grouping, thousands_sep); + case PyUnicode_2BYTE_KIND: + return _PyUnicode_ucs2_InsertThousandsGrouping( + (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, + min_width, grouping, thousands_sep); + case PyUnicode_4BYTE_KIND: + return _PyUnicode_ucs4_InsertThousandsGrouping( + (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, + min_width, grouping, thousands_sep); + } + assert(0); + return -1; +} + + +#include "stringlib/unicodedefs.h" +#include "stringlib/fastsearch.h" + +#include "stringlib/count.h" +#include "stringlib/find.h" /* helper macro to fixup start/end slice values */ #define ADJUST_INDICES(start, end, len) \ @@ -6442,95 +8617,122 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s, start = 0; \ } -/* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed - * by 'ptr', possibly combining surrogate pairs on narrow builds. - * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character - * that should be returned and 'end' pointing to the end of the buffer. - * ('end' is used on narrow builds to detect a lone surrogate at the - * end of the buffer that should be returned unchanged.) - * The ptr and end arguments should be side-effect free and ptr must an lvalue. - * The type of the returned char is always Py_UCS4. - * - * Note: the macro advances ptr to next char, so it might have side-effects - * (especially if used with other macros). - */ - -/* helper macros used by _Py_UNICODE_NEXT */ -#define _Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= ch && ch <= 0xDBFF) -#define _Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= ch && ch <= 0xDFFF) -/* Join two surrogate characters and return a single Py_UCS4 value. */ -#define _Py_UNICODE_JOIN_SURROGATES(high, low) \ - (((((Py_UCS4)(high) & 0x03FF) << 10) | \ - ((Py_UCS4)(low) & 0x03FF)) + 0x10000) - -#ifdef Py_UNICODE_WIDE -#define _Py_UNICODE_NEXT(ptr, end) *(ptr)++ -#else -#define _Py_UNICODE_NEXT(ptr, end) \ - (((_Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) && \ - _Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ? \ - ((ptr) += 2,_Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \ - (Py_UCS4)*(ptr)++) -#endif - -Py_ssize_t PyUnicode_Count(PyObject *str, - PyObject *substr, - Py_ssize_t start, - Py_ssize_t end) +Py_ssize_t +PyUnicode_Count(PyObject *str, + PyObject *substr, + Py_ssize_t start, + Py_ssize_t end) { Py_ssize_t result; PyUnicodeObject* str_obj; PyUnicodeObject* sub_obj; + int kind1, kind2, kind; + void *buf1 = NULL, *buf2 = NULL; + Py_ssize_t len1, len2; str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); - if (!str_obj) + if (!str_obj || PyUnicode_READY(str_obj) == -1) return -1; sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); - if (!sub_obj) { + if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { Py_DECREF(str_obj); return -1; } - ADJUST_INDICES(start, end, str_obj->length); - result = stringlib_count( - str_obj->str + start, end - start, sub_obj->str, sub_obj->length, - PY_SSIZE_T_MAX - ); + kind1 = PyUnicode_KIND(str_obj); + kind2 = PyUnicode_KIND(sub_obj); + kind = kind1 > kind2 ? kind1 : kind2; + buf1 = PyUnicode_DATA(str_obj); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); + if (!buf1) + goto onError; + buf2 = PyUnicode_DATA(sub_obj); + if (kind2 != kind) + buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); + if (!buf2) + goto onError; + len1 = PyUnicode_GET_LENGTH(str_obj); + len2 = PyUnicode_GET_LENGTH(sub_obj); + + ADJUST_INDICES(start, end, len1); + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) + result = asciilib_count( + ((Py_UCS1*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + else + result = ucs1lib_count( + ((Py_UCS1*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + break; + case PyUnicode_2BYTE_KIND: + result = ucs2lib_count( + ((Py_UCS2*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + break; + case PyUnicode_4BYTE_KIND: + result = ucs4lib_count( + ((Py_UCS4*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + break; + default: + assert(0); result = 0; + } Py_DECREF(sub_obj); Py_DECREF(str_obj); + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); + return result; + onError: + Py_DECREF(sub_obj); + Py_DECREF(str_obj); + if (kind1 != kind && buf1) + PyMem_Free(buf1); + if (kind2 != kind && buf2) + PyMem_Free(buf2); + return -1; } -Py_ssize_t PyUnicode_Find(PyObject *str, - PyObject *sub, - Py_ssize_t start, - Py_ssize_t end, - int direction) +Py_ssize_t +PyUnicode_Find(PyObject *str, + PyObject *sub, + Py_ssize_t start, + Py_ssize_t end, + int direction) { Py_ssize_t result; str = PyUnicode_FromObject(str); - if (!str) + if (!str || PyUnicode_READY(str) == -1) return -2; sub = PyUnicode_FromObject(sub); - if (!sub) { + if (!sub || PyUnicode_READY(sub) == -1) { Py_DECREF(str); return -2; } if (direction > 0) - result = stringlib_find_slice( - PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), - PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), - start, end + result = any_find_slice( + asciilib_find_slice, ucs1lib_find_slice, + ucs2lib_find_slice, ucs4lib_find_slice, + str, sub, start, end ); else - result = stringlib_rfind_slice( - PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), - PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), - start, end + result = any_find_slice( + asciilib_rfind_slice, ucs1lib_rfind_slice, + ucs2lib_rfind_slice, ucs4lib_rfind_slice, + str, sub, start, end ); Py_DECREF(str); @@ -6539,37 +8741,105 @@ Py_ssize_t PyUnicode_Find(PyObject *str, return result; } -static -int tailmatch(PyUnicodeObject *self, - PyUnicodeObject *substring, - Py_ssize_t start, - Py_ssize_t end, - int direction) +Py_ssize_t +PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, + Py_ssize_t start, Py_ssize_t end, + int direction) { - if (substring->length == 0) + char *result; + int kind; + if (PyUnicode_READY(str) == -1) + return -2; + if (start < 0 || end < 0) { + PyErr_SetString(PyExc_IndexError, "string index out of range"); + return -2; + } + if (end > PyUnicode_GET_LENGTH(str)) + end = PyUnicode_GET_LENGTH(str); + kind = PyUnicode_KIND(str); + result = findchar(PyUnicode_1BYTE_DATA(str) + + kind*start, + kind, + end-start, ch, direction); + if (!result) + return -1; + return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); +} + +static int +tailmatch(PyUnicodeObject *self, + PyUnicodeObject *substring, + Py_ssize_t start, + Py_ssize_t end, + int direction) +{ + int kind_self; + int kind_sub; + void *data_self; + void *data_sub; + Py_ssize_t offset; + Py_ssize_t i; + Py_ssize_t end_sub; + + if (PyUnicode_READY(self) == -1 || + PyUnicode_READY(substring) == -1) + return 0; + + if (PyUnicode_GET_LENGTH(substring) == 0) return 1; - ADJUST_INDICES(start, end, self->length); - end -= substring->length; + ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); + end -= PyUnicode_GET_LENGTH(substring); if (end < start) return 0; - if (direction > 0) { - if (Py_UNICODE_MATCH(self, end, substring)) - return 1; - } else { - if (Py_UNICODE_MATCH(self, start, substring)) + kind_self = PyUnicode_KIND(self); + data_self = PyUnicode_DATA(self); + kind_sub = PyUnicode_KIND(substring); + data_sub = PyUnicode_DATA(substring); + end_sub = PyUnicode_GET_LENGTH(substring) - 1; + + if (direction > 0) + offset = end; + else + offset = start; + + if (PyUnicode_READ(kind_self, data_self, offset) == + PyUnicode_READ(kind_sub, data_sub, 0) && + PyUnicode_READ(kind_self, data_self, offset + end_sub) == + PyUnicode_READ(kind_sub, data_sub, end_sub)) { + /* If both are of the same kind, memcmp is sufficient */ + if (kind_self == kind_sub) { + return ! memcmp((char *)data_self + + (offset * PyUnicode_KIND(substring)), + data_sub, + PyUnicode_GET_LENGTH(substring) * + PyUnicode_KIND(substring)); + } + /* otherwise we have to compare each character by first accesing it */ + else { + /* We do not need to compare 0 and len(substring)-1 because + the if statement above ensured already that they are equal + when we end up here. */ + // TODO: honor direction and do a forward or backwards search + for (i = 1; i < end_sub; ++i) { + if (PyUnicode_READ(kind_self, data_self, offset + i) != + PyUnicode_READ(kind_sub, data_sub, i)) + return 0; + } return 1; + } } return 0; } -Py_ssize_t PyUnicode_Tailmatch(PyObject *str, - PyObject *substr, - Py_ssize_t start, - Py_ssize_t end, - int direction) +Py_ssize_t +PyUnicode_Tailmatch(PyObject *str, + PyObject *substr, + Py_ssize_t start, + Py_ssize_t end, + int direction) { Py_ssize_t result; @@ -6593,20 +8863,41 @@ Py_ssize_t PyUnicode_Tailmatch(PyObject *str, /* Apply fixfct filter to the Unicode object self and return a reference to the modified object */ -static -PyObject *fixup(PyUnicodeObject *self, - int (*fixfct)(PyUnicodeObject *s)) +static PyObject * +fixup(PyObject *self, + Py_UCS4 (*fixfct)(PyObject *s)) { + PyObject *u; + Py_UCS4 maxchar_old, maxchar_new = 0; - PyUnicodeObject *u; - - u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); + if (PyUnicode_READY(self) == -1) + return NULL; + maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); + u = PyUnicode_New(PyUnicode_GET_LENGTH(self), + maxchar_old); if (u == NULL) return NULL; - Py_UNICODE_COPY(u->str, self->str, self->length); + Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), + PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); + + /* fix functions return the new maximum character in a string, + if the kind of the resulting unicode object does not change, + everything is fine. Otherwise we need to change the string kind + and re-run the fix function. */ + maxchar_new = fixfct(u); + if (maxchar_new == 0) + /* do nothing, keep maxchar_new at 0 which means no changes. */; + else if (maxchar_new <= 127) + maxchar_new = 127; + else if (maxchar_new <= 255) + maxchar_new = 255; + else if (maxchar_new <= 65535) + maxchar_new = 65535; + else + maxchar_new = 1114111; /* 0x10ffff */ - if (!fixfct(u) && PyUnicode_CheckExact(self)) { + if (!maxchar_new && PyUnicode_CheckExact(self)) { /* fixfct should return TRUE if it modified the buffer. If FALSE, return a reference to the original buffer instead (to save space, not time) */ @@ -6614,123 +8905,208 @@ PyObject *fixup(PyUnicodeObject *self, Py_DECREF(u); return (PyObject*) self; } - return (PyObject*) u; + else if (maxchar_new == maxchar_old) { + return u; + } + else { + /* In case the maximum character changed, we need to + convert the string to the new category. */ + PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); + if (v == NULL) { + Py_DECREF(u); + return NULL; + } + if (maxchar_new > maxchar_old) { + /* If the maxchar increased so that the kind changed, not all + characters are representable anymore and we need to fix the + string again. This only happens in very few cases. */ + copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); + maxchar_old = fixfct(v); + assert(maxchar_old > 0 && maxchar_old <= maxchar_new); + } + else { + copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); + } + + Py_DECREF(u); + assert(_PyUnicode_CheckConsistency(v, 1)); + return v; + } } -static -int fixupper(PyUnicodeObject *self) +static Py_UCS4 +fixupper(PyObject *self) { - Py_ssize_t len = self->length; - Py_UNICODE *s = self->str; - int status = 0; - - while (len-- > 0) { - register Py_UNICODE ch; + /* No need to call PyUnicode_READY(self) because this function is only + called as a callback from fixup() which does it already. */ + const Py_ssize_t len = PyUnicode_GET_LENGTH(self); + const int kind = PyUnicode_KIND(self); + void *data = PyUnicode_DATA(self); + int touched = 0; + Py_UCS4 maxchar = 0; + Py_ssize_t i; - ch = Py_UNICODE_TOUPPER(*s); - if (ch != *s) { - status = 1; - *s = ch; + for (i = 0; i < len; ++i) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); + const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); + if (up != ch) { + if (up > maxchar) + maxchar = up; + PyUnicode_WRITE(kind, data, i, up); + touched = 1; } - s++; + else if (ch > maxchar) + maxchar = ch; } - return status; + if (touched) + return maxchar; + else + return 0; } -static -int fixlower(PyUnicodeObject *self) +static Py_UCS4 +fixlower(PyObject *self) { - Py_ssize_t len = self->length; - Py_UNICODE *s = self->str; - int status = 0; - - while (len-- > 0) { - register Py_UNICODE ch; + /* No need to call PyUnicode_READY(self) because fixup() which does it. */ + const Py_ssize_t len = PyUnicode_GET_LENGTH(self); + const int kind = PyUnicode_KIND(self); + void *data = PyUnicode_DATA(self); + int touched = 0; + Py_UCS4 maxchar = 0; + Py_ssize_t i; - ch = Py_UNICODE_TOLOWER(*s); - if (ch != *s) { - status = 1; - *s = ch; + for(i = 0; i < len; ++i) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); + const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); + if (lo != ch) { + if (lo > maxchar) + maxchar = lo; + PyUnicode_WRITE(kind, data, i, lo); + touched = 1; } - s++; + else if (ch > maxchar) + maxchar = ch; } - return status; + if (touched) + return maxchar; + else + return 0; } -static -int fixswapcase(PyUnicodeObject *self) +static Py_UCS4 +fixswapcase(PyObject *self) { - Py_ssize_t len = self->length; - Py_UNICODE *s = self->str; - int status = 0; + /* No need to call PyUnicode_READY(self) because fixup() which does it. */ + const Py_ssize_t len = PyUnicode_GET_LENGTH(self); + const int kind = PyUnicode_KIND(self); + void *data = PyUnicode_DATA(self); + int touched = 0; + Py_UCS4 maxchar = 0; + Py_ssize_t i; - while (len-- > 0) { - if (Py_UNICODE_ISUPPER(*s)) { - *s = Py_UNICODE_TOLOWER(*s); - status = 1; - } else if (Py_UNICODE_ISLOWER(*s)) { - *s = Py_UNICODE_TOUPPER(*s); - status = 1; + for(i = 0; i < len; ++i) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); + Py_UCS4 nu = 0; + + if (Py_UNICODE_ISUPPER(ch)) + nu = Py_UNICODE_TOLOWER(ch); + else if (Py_UNICODE_ISLOWER(ch)) + nu = Py_UNICODE_TOUPPER(ch); + + if (nu != 0) { + if (nu > maxchar) + maxchar = nu; + PyUnicode_WRITE(kind, data, i, nu); + touched = 1; } - s++; + else if (ch > maxchar) + maxchar = ch; } - return status; + if (touched) + return maxchar; + else + return 0; } -static -int fixcapitalize(PyUnicodeObject *self) +static Py_UCS4 +fixcapitalize(PyObject *self) { - Py_ssize_t len = self->length; - Py_UNICODE *s = self->str; - int status = 0; + /* No need to call PyUnicode_READY(self) because fixup() which does it. */ + const Py_ssize_t len = PyUnicode_GET_LENGTH(self); + const int kind = PyUnicode_KIND(self); + void *data = PyUnicode_DATA(self); + int touched = 0; + Py_UCS4 maxchar = 0; + Py_ssize_t i = 0; + Py_UCS4 ch; if (len == 0) return 0; - if (!Py_UNICODE_ISUPPER(*s)) { - *s = Py_UNICODE_TOUPPER(*s); - status = 1; + + ch = PyUnicode_READ(kind, data, i); + if (!Py_UNICODE_ISUPPER(ch)) { + maxchar = Py_UNICODE_TOUPPER(ch); + PyUnicode_WRITE(kind, data, i, maxchar); + touched = 1; } - s++; - while (--len > 0) { - if (!Py_UNICODE_ISLOWER(*s)) { - *s = Py_UNICODE_TOLOWER(*s); - status = 1; + ++i; + for(; i < len; ++i) { + ch = PyUnicode_READ(kind, data, i); + if (!Py_UNICODE_ISLOWER(ch)) { + const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); + if (lo > maxchar) + maxchar = lo; + PyUnicode_WRITE(kind, data, i, lo); + touched = 1; } - s++; + else if (ch > maxchar) + maxchar = ch; } - return status; + + if (touched) + return maxchar; + else + return 0; } -static -int fixtitle(PyUnicodeObject *self) +static Py_UCS4 +fixtitle(PyObject *self) { - register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register Py_UNICODE *e; + /* No need to call PyUnicode_READY(self) because fixup() which does it. */ + const Py_ssize_t len = PyUnicode_GET_LENGTH(self); + const int kind = PyUnicode_KIND(self); + void *data = PyUnicode_DATA(self); + Py_UCS4 maxchar = 0; + Py_ssize_t i = 0; int previous_is_cased; /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1) { - Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); - if (*p != ch) { - *p = ch; - return 1; + if (len == 1) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); + const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); + if (ti != ch) { + PyUnicode_WRITE(kind, data, i, ti); + return ti; } else return 0; } - - e = p + PyUnicode_GET_SIZE(self); previous_is_cased = 0; - for (; p < e; p++) { - register const Py_UNICODE ch = *p; + for(; i < len; ++i) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); + Py_UCS4 nu; if (previous_is_cased) - *p = Py_UNICODE_TOLOWER(ch); + nu = Py_UNICODE_TOLOWER(ch); else - *p = Py_UNICODE_TOTITLE(ch); + nu = Py_UNICODE_TOTITLE(ch); + + if (nu > maxchar) + maxchar = nu; + PyUnicode_WRITE(kind, data, i, nu); if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISUPPER(ch) || @@ -6739,22 +9115,26 @@ int fixtitle(PyUnicodeObject *self) else previous_is_cased = 0; } - return 1; + return maxchar; } PyObject * PyUnicode_Join(PyObject *separator, PyObject *seq) { - const Py_UNICODE blank = ' '; - const Py_UNICODE *sep = ␣ - Py_ssize_t seplen = 1; - PyUnicodeObject *res = NULL; /* the result */ - Py_UNICODE *res_p; /* pointer to free byte in res's string area */ + PyObject *sep = NULL; + Py_ssize_t seplen; + PyObject *res = NULL; /* the result */ PyObject *fseq; /* PySequence_Fast(seq) */ Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ PyObject **items; PyObject *item; - Py_ssize_t sz, i; + Py_ssize_t sz, i, res_offset; + Py_UCS4 maxchar; + Py_UCS4 item_maxchar; + int use_memcpy; + unsigned char *res_data = NULL, *sep_data = NULL; + PyObject *last_obj; + unsigned int kind = 0; fseq = PySequence_Fast(seq, ""); if (fseq == NULL) { @@ -6768,24 +9148,34 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) seqlen = PySequence_Fast_GET_SIZE(fseq); /* If empty sequence, return u"". */ if (seqlen == 0) { - res = _PyUnicode_New(0); /* empty sequence; return u"" */ - goto Done; + Py_DECREF(fseq); + Py_INCREF(unicode_empty); + res = unicode_empty; + return res; } - items = PySequence_Fast_ITEMS(fseq); + /* If singleton sequence with an exact Unicode, return that. */ + last_obj = NULL; + items = PySequence_Fast_ITEMS(fseq); if (seqlen == 1) { - item = items[0]; - if (PyUnicode_CheckExact(item)) { - Py_INCREF(item); - res = (PyUnicodeObject *)item; - goto Done; + if (PyUnicode_CheckExact(items[0])) { + res = items[0]; + Py_INCREF(res); + Py_DECREF(fseq); + return res; } + seplen = 0; + maxchar = 0; } else { /* Set up sep and seplen */ if (separator == NULL) { - sep = ␣ + /* fall back to a blank space separator */ + sep = PyUnicode_FromOrdinal(' '); + if (!sep) + goto onError; seplen = 1; + maxchar = 32; } else { if (!PyUnicode_Check(separator)) { @@ -6795,9 +9185,16 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) Py_TYPE(separator)->tp_name); goto onError; } - sep = PyUnicode_AS_UNICODE(separator); - seplen = PyUnicode_GET_SIZE(separator); + if (PyUnicode_READY(separator)) + goto onError; + sep = separator; + seplen = PyUnicode_GET_LENGTH(separator); + maxchar = PyUnicode_MAX_CHAR_VALUE(separator); + /* inc refcount to keep this code path symmetric with the + above case of a blank separator */ + Py_INCREF(sep); } + last_obj = sep; } /* There are at least two things to join, or else we have a subclass @@ -6806,6 +9203,11 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) * need (sz), and see whether all argument are strings. */ sz = 0; +#ifdef Py_DEBUG + use_memcpy = 0; +#else + use_memcpy = 1; +#endif for (i = 0; i < seqlen; i++) { const Py_ssize_t old_sz = sz; item = items[i]; @@ -6816,7 +9218,11 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) i, Py_TYPE(item)->tp_name); goto onError; } - sz += PyUnicode_GET_SIZE(item); + if (PyUnicode_READY(item) == -1) + goto onError; + sz += PyUnicode_GET_LENGTH(item); + item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); + maxchar = Py_MAX(maxchar, item_maxchar); if (i != 0) sz += seplen; if (sz < old_sz || sz > PY_SSIZE_T_MAX) { @@ -6824,44 +9230,109 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) "join() result is too long for a Python string"); goto onError; } + if (use_memcpy && last_obj != NULL) { + if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) + use_memcpy = 0; + } + last_obj = item; } - res = _PyUnicode_New(sz); + res = PyUnicode_New(sz, maxchar); if (res == NULL) goto onError; /* Catenate everything. */ - res_p = PyUnicode_AS_UNICODE(res); - for (i = 0; i < seqlen; ++i) { +#ifdef Py_DEBUG + use_memcpy = 0; +#else + if (use_memcpy) { + res_data = PyUnicode_1BYTE_DATA(res); + kind = PyUnicode_KIND(res); + if (seplen != 0) + sep_data = PyUnicode_1BYTE_DATA(sep); + } +#endif + for (i = 0, res_offset = 0; i < seqlen; ++i) { Py_ssize_t itemlen; item = items[i]; - itemlen = PyUnicode_GET_SIZE(item); /* Copy item, and maybe the separator. */ - if (i) { - Py_UNICODE_COPY(res_p, sep, seplen); - res_p += seplen; + if (i && seplen != 0) { + if (use_memcpy) { + Py_MEMCPY(res_data, + sep_data, + kind * seplen); + res_data += kind * seplen; + } + else { + copy_characters(res, res_offset, sep, 0, seplen); + res_offset += seplen; + } + } + itemlen = PyUnicode_GET_LENGTH(item); + if (itemlen != 0) { + if (use_memcpy) { + Py_MEMCPY(res_data, + PyUnicode_DATA(item), + kind * itemlen); + res_data += kind * itemlen; + } + else { + copy_characters(res, res_offset, item, 0, itemlen); + res_offset += itemlen; + } } - Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); - res_p += itemlen; } + if (use_memcpy) + assert(res_data == PyUnicode_1BYTE_DATA(res) + + kind * PyUnicode_GET_LENGTH(res)); + else + assert(res_offset == PyUnicode_GET_LENGTH(res)); - Done: Py_DECREF(fseq); - return (PyObject *)res; + Py_XDECREF(sep); + assert(_PyUnicode_CheckConsistency(res, 1)); + return res; onError: Py_DECREF(fseq); + Py_XDECREF(sep); Py_XDECREF(res); return NULL; } -static -PyUnicodeObject *pad(PyUnicodeObject *self, - Py_ssize_t left, - Py_ssize_t right, - Py_UNICODE fill) -{ - PyUnicodeObject *u; +#define FILL(kind, data, value, start, length) \ + do { \ + Py_ssize_t i_ = 0; \ + assert(kind != PyUnicode_WCHAR_KIND); \ + switch ((kind)) { \ + case PyUnicode_1BYTE_KIND: { \ + unsigned char * to_ = (unsigned char *)((data)) + (start); \ + memset(to_, (unsigned char)value, length); \ + break; \ + } \ + case PyUnicode_2BYTE_KIND: { \ + Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ + for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ + break; \ + } \ + default: { \ + Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ + for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ + break; \ + } \ + } \ + } while (0) + +static PyObject * +pad(PyObject *self, + Py_ssize_t left, + Py_ssize_t right, + Py_UCS4 fill) +{ + PyObject *u; + Py_UCS4 maxchar; + int kind; + void *data; if (left < 0) left = 0; @@ -6873,218 +9344,555 @@ PyUnicodeObject *pad(PyUnicodeObject *self, return self; } - if (left > PY_SSIZE_T_MAX - self->length || - right > PY_SSIZE_T_MAX - (left + self->length)) { + if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || + right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { PyErr_SetString(PyExc_OverflowError, "padded string is too long"); return NULL; } - u = _PyUnicode_New(left + self->length + right); - if (u) { - if (left) - Py_UNICODE_FILL(u->str, fill, left); - Py_UNICODE_COPY(u->str + left, self->str, self->length); - if (right) - Py_UNICODE_FILL(u->str + left + self->length, fill, right); - } + maxchar = PyUnicode_MAX_CHAR_VALUE(self); + if (fill > maxchar) + maxchar = fill; + u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); + if (!u) + return NULL; + kind = PyUnicode_KIND(u); + data = PyUnicode_DATA(u); + if (left) + FILL(kind, data, fill, 0, left); + if (right) + FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); + copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); + assert(_PyUnicode_CheckConsistency(u, 1)); return u; } +#undef FILL -PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) +PyObject * +PyUnicode_Splitlines(PyObject *string, int keepends) { PyObject *list; string = PyUnicode_FromObject(string); - if (string == NULL) + if (string == NULL || PyUnicode_READY(string) == -1) return NULL; - list = stringlib_splitlines( - (PyObject*) string, PyUnicode_AS_UNICODE(string), - PyUnicode_GET_SIZE(string), keepends); - + switch(PyUnicode_KIND(string)) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(string)) + list = asciilib_splitlines( + (PyObject*) string, PyUnicode_1BYTE_DATA(string), + PyUnicode_GET_LENGTH(string), keepends); + else + list = ucs1lib_splitlines( + (PyObject*) string, PyUnicode_1BYTE_DATA(string), + PyUnicode_GET_LENGTH(string), keepends); + break; + case PyUnicode_2BYTE_KIND: + list = ucs2lib_splitlines( + (PyObject*) string, PyUnicode_2BYTE_DATA(string), + PyUnicode_GET_LENGTH(string), keepends); + break; + case PyUnicode_4BYTE_KIND: + list = ucs4lib_splitlines( + (PyObject*) string, PyUnicode_4BYTE_DATA(string), + PyUnicode_GET_LENGTH(string), keepends); + break; + default: + assert(0); + list = 0; + } Py_DECREF(string); return list; } -static -PyObject *split(PyUnicodeObject *self, - PyUnicodeObject *substring, - Py_ssize_t maxcount) +static PyObject * +split(PyObject *self, + PyObject *substring, + Py_ssize_t maxcount) { + int kind1, kind2, kind; + void *buf1, *buf2; + Py_ssize_t len1, len2; + PyObject* out; + if (maxcount < 0) maxcount = PY_SSIZE_T_MAX; + if (PyUnicode_READY(self) == -1) + return NULL; + if (substring == NULL) - return stringlib_split_whitespace( - (PyObject*) self, self->str, self->length, maxcount - ); + switch(PyUnicode_KIND(self)) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(self)) + return asciilib_split_whitespace( + (PyObject*) self, PyUnicode_1BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + else + return ucs1lib_split_whitespace( + (PyObject*) self, PyUnicode_1BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + case PyUnicode_2BYTE_KIND: + return ucs2lib_split_whitespace( + (PyObject*) self, PyUnicode_2BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + case PyUnicode_4BYTE_KIND: + return ucs4lib_split_whitespace( + (PyObject*) self, PyUnicode_4BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + default: + assert(0); + return NULL; + } - return stringlib_split( - (PyObject*) self, self->str, self->length, - substring->str, substring->length, - maxcount - ); + if (PyUnicode_READY(substring) == -1) + return NULL; + + kind1 = PyUnicode_KIND(self); + kind2 = PyUnicode_KIND(substring); + kind = kind1 > kind2 ? kind1 : kind2; + buf1 = PyUnicode_DATA(self); + buf2 = PyUnicode_DATA(substring); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind((PyObject*)self, kind); + if (!buf1) + return NULL; + if (kind2 != kind) + buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); + if (!buf2) { + if (kind1 != kind) PyMem_Free(buf1); + return NULL; + } + len1 = PyUnicode_GET_LENGTH(self); + len2 = PyUnicode_GET_LENGTH(substring); + + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) + out = asciilib_split( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + else + out = ucs1lib_split( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + break; + case PyUnicode_2BYTE_KIND: + out = ucs2lib_split( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + break; + case PyUnicode_4BYTE_KIND: + out = ucs4lib_split( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + break; + default: + out = NULL; + } + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); + return out; } -static -PyObject *rsplit(PyUnicodeObject *self, - PyUnicodeObject *substring, - Py_ssize_t maxcount) +static PyObject * +rsplit(PyObject *self, + PyObject *substring, + Py_ssize_t maxcount) { + int kind1, kind2, kind; + void *buf1, *buf2; + Py_ssize_t len1, len2; + PyObject* out; + if (maxcount < 0) maxcount = PY_SSIZE_T_MAX; + if (PyUnicode_READY(self) == -1) + return NULL; + if (substring == NULL) - return stringlib_rsplit_whitespace( - (PyObject*) self, self->str, self->length, maxcount - ); + switch(PyUnicode_KIND(self)) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(self)) + return asciilib_rsplit_whitespace( + (PyObject*) self, PyUnicode_1BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + else + return ucs1lib_rsplit_whitespace( + (PyObject*) self, PyUnicode_1BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + case PyUnicode_2BYTE_KIND: + return ucs2lib_rsplit_whitespace( + (PyObject*) self, PyUnicode_2BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + case PyUnicode_4BYTE_KIND: + return ucs4lib_rsplit_whitespace( + (PyObject*) self, PyUnicode_4BYTE_DATA(self), + PyUnicode_GET_LENGTH(self), maxcount + ); + default: + assert(0); + return NULL; + } - return stringlib_rsplit( - (PyObject*) self, self->str, self->length, - substring->str, substring->length, - maxcount - ); + if (PyUnicode_READY(substring) == -1) + return NULL; + + kind1 = PyUnicode_KIND(self); + kind2 = PyUnicode_KIND(substring); + kind = kind1 > kind2 ? kind1 : kind2; + buf1 = PyUnicode_DATA(self); + buf2 = PyUnicode_DATA(substring); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind((PyObject*)self, kind); + if (!buf1) + return NULL; + if (kind2 != kind) + buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); + if (!buf2) { + if (kind1 != kind) PyMem_Free(buf1); + return NULL; + } + len1 = PyUnicode_GET_LENGTH(self); + len2 = PyUnicode_GET_LENGTH(substring); + + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) + out = asciilib_rsplit( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + else + out = ucs1lib_rsplit( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + break; + case PyUnicode_2BYTE_KIND: + out = ucs2lib_rsplit( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + break; + case PyUnicode_4BYTE_KIND: + out = ucs4lib_rsplit( + (PyObject*) self, buf1, len1, buf2, len2, maxcount); + break; + default: + out = NULL; + } + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); + return out; } -static -PyObject *replace(PyUnicodeObject *self, - PyUnicodeObject *str1, - PyUnicodeObject *str2, - Py_ssize_t maxcount) +static Py_ssize_t +anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, + PyObject *str2, void *buf2, Py_ssize_t len2, Py_ssize_t offset) { - PyUnicodeObject *u; + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) + return asciilib_find(buf1, len1, buf2, len2, offset); + else + return ucs1lib_find(buf1, len1, buf2, len2, offset); + case PyUnicode_2BYTE_KIND: + return ucs2lib_find(buf1, len1, buf2, len2, offset); + case PyUnicode_4BYTE_KIND: + return ucs4lib_find(buf1, len1, buf2, len2, offset); + } + assert(0); + return -1; +} + +static Py_ssize_t +anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, + PyObject *str1, void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) +{ + switch(kind) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) + return asciilib_count(sbuf, slen, buf1, len1, maxcount); + else + return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); + case PyUnicode_2BYTE_KIND: + return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); + case PyUnicode_4BYTE_KIND: + return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); + } + assert(0); + return 0; +} + +static PyObject * +replace(PyObject *self, PyObject *str1, + PyObject *str2, Py_ssize_t maxcount) +{ + PyObject *u; + char *sbuf = PyUnicode_DATA(self); + char *buf1 = PyUnicode_DATA(str1); + char *buf2 = PyUnicode_DATA(str2); + int srelease = 0, release1 = 0, release2 = 0; + int skind = PyUnicode_KIND(self); + int kind1 = PyUnicode_KIND(str1); + int kind2 = PyUnicode_KIND(str2); + Py_ssize_t slen = PyUnicode_GET_LENGTH(self); + Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); + Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); if (maxcount < 0) maxcount = PY_SSIZE_T_MAX; - else if (maxcount == 0 || self->length == 0) + else if (maxcount == 0 || slen == 0) + goto nothing; + + if (str1 == str2) + goto nothing; + if (skind < kind1) + /* substring too wide to be present */ goto nothing; - if (str1->length == str2->length) { + if (len1 == len2) { Py_ssize_t i; /* same length */ - if (str1->length == 0) + if (len1 == 0) goto nothing; - if (str1->length == 1) { + if (len1 == 1) { /* replace characters */ - Py_UNICODE u1, u2; - if (!findchar(self->str, self->length, str1->str[0])) + Py_UCS4 u1, u2, maxchar; + int mayshrink, rkind; + u1 = PyUnicode_READ_CHAR(str1, 0); + if (!findchar(sbuf, PyUnicode_KIND(self), + slen, u1, 1)) goto nothing; - u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); + u2 = PyUnicode_READ_CHAR(str2, 0); + maxchar = PyUnicode_MAX_CHAR_VALUE(self); + /* Replacing u1 with u2 may cause a maxchar reduction in the + result string. */ + if (u2 > maxchar) { + maxchar = u2; + mayshrink = 0; + } + else + mayshrink = maxchar > 127; + u = PyUnicode_New(slen, maxchar); if (!u) - return NULL; - Py_UNICODE_COPY(u->str, self->str, self->length); - u1 = str1->str[0]; - u2 = str2->str[0]; - for (i = 0; i < u->length; i++) - if (u->str[i] == u1) { + goto error; + copy_characters(u, 0, self, 0, slen); + rkind = PyUnicode_KIND(u); + for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) + if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { if (--maxcount < 0) break; - u->str[i] = u2; + PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); } + if (mayshrink) { + unicode_adjust_maxchar(&u); + if (u == NULL) + goto error; + } } else { - i = stringlib_find( - self->str, self->length, str1->str, str1->length, 0 - ); + int rkind = skind; + char *res; + PyObject *rstr; + Py_UCS4 maxchar; + + if (kind1 < rkind) { + /* widen substring */ + buf1 = _PyUnicode_AsKind(str1, rkind); + if (!buf1) goto error; + release1 = 1; + } + i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); if (i < 0) goto nothing; - u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); - if (!u) - return NULL; - Py_UNICODE_COPY(u->str, self->str, self->length); - + if (rkind > kind2) { + /* widen replacement */ + buf2 = _PyUnicode_AsKind(str2, rkind); + if (!buf2) goto error; + release2 = 1; + } + else if (rkind < kind2) { + /* widen self and buf1 */ + rkind = kind2; + if (release1) PyMem_Free(buf1); + sbuf = _PyUnicode_AsKind(self, rkind); + if (!sbuf) goto error; + srelease = 1; + buf1 = _PyUnicode_AsKind(str1, rkind); + if (!buf1) goto error; + release1 = 1; + } + maxchar = PyUnicode_MAX_CHAR_VALUE(self); + maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); + rstr = PyUnicode_New(slen, maxchar); + if (!rstr) + goto error; + res = PyUnicode_DATA(rstr); + + memcpy(res, sbuf, rkind * slen); /* change everything in-place, starting with this one */ - Py_UNICODE_COPY(u->str+i, str2->str, str2->length); - i += str1->length; + memcpy(res + rkind * i, + buf2, + rkind * len2); + i += len1; while ( --maxcount > 0) { - i = stringlib_find(self->str+i, self->length-i, - str1->str, str1->length, - i); + i = anylib_find(rkind, self, + sbuf+rkind*i, slen-i, + str1, buf1, len1, i); if (i == -1) break; - Py_UNICODE_COPY(u->str+i, str2->str, str2->length); - i += str1->length; + memcpy(res + rkind * i, + buf2, + rkind * len2); + i += len1; } + + u = rstr; + unicode_adjust_maxchar(&u); + if (!u) + goto error; } } else { - Py_ssize_t n, i, j, e; - Py_ssize_t product, new_size, delta; - Py_UNICODE *p; + Py_ssize_t n, i, j, ires; + Py_ssize_t product, new_size; + int rkind = skind; + PyObject *rstr; + char *res; + Py_UCS4 maxchar; - /* replace strings */ - n = stringlib_count(self->str, self->length, str1->str, str1->length, - maxcount); + if (kind1 < rkind) { + buf1 = _PyUnicode_AsKind(str1, rkind); + if (!buf1) goto error; + release1 = 1; + } + n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); if (n == 0) goto nothing; - /* new_size = self->length + n * (str2->length - str1->length)); */ - delta = (str2->length - str1->length); - if (delta == 0) { - new_size = self->length; - } else { - product = n * (str2->length - str1->length); - if ((product / (str2->length - str1->length)) != n) { - PyErr_SetString(PyExc_OverflowError, - "replace string is too long"); - return NULL; - } - new_size = self->length + product; - if (new_size < 0) { + if (kind2 < rkind) { + buf2 = _PyUnicode_AsKind(str2, rkind); + if (!buf2) goto error; + release2 = 1; + } + else if (kind2 > rkind) { + rkind = kind2; + sbuf = _PyUnicode_AsKind(self, rkind); + if (!sbuf) goto error; + srelease = 1; + if (release1) PyMem_Free(buf1); + buf1 = _PyUnicode_AsKind(str1, rkind); + if (!buf1) goto error; + release1 = 1; + } + /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - + PyUnicode_GET_LENGTH(str1))); */ + product = n * (len2-len1); + if ((product / (len2-len1)) != n) { PyErr_SetString(PyExc_OverflowError, "replace string is too long"); - return NULL; - } + goto error; } - u = _PyUnicode_New(new_size); - if (!u) - return NULL; - i = 0; - p = u->str; - e = self->length - str1->length; - if (str1->length > 0) { + new_size = slen + product; + if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { + PyErr_SetString(PyExc_OverflowError, + "replace string is too long"); + goto error; + } + maxchar = PyUnicode_MAX_CHAR_VALUE(self); + maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); + rstr = PyUnicode_New(new_size, maxchar); + if (!rstr) + goto error; + res = PyUnicode_DATA(rstr); + ires = i = 0; + if (len1 > 0) { while (n-- > 0) { /* look for next match */ - j = stringlib_find(self->str+i, self->length-i, - str1->str, str1->length, - i); + j = anylib_find(rkind, self, + sbuf + rkind * i, slen-i, + str1, buf1, len1, i); if (j == -1) break; else if (j > i) { /* copy unchanged part [i:j] */ - Py_UNICODE_COPY(p, self->str+i, j-i); - p += j - i; + memcpy(res + rkind * ires, + sbuf + rkind * i, + rkind * (j-i)); + ires += j - i; } /* copy substitution string */ - if (str2->length > 0) { - Py_UNICODE_COPY(p, str2->str, str2->length); - p += str2->length; + if (len2 > 0) { + memcpy(res + rkind * ires, + buf2, + rkind * len2); + ires += len2; } - i = j + str1->length; + i = j + len1; } - if (i < self->length) + if (i < slen) /* copy tail [i:] */ - Py_UNICODE_COPY(p, self->str+i, self->length-i); + memcpy(res + rkind * ires, + sbuf + rkind * i, + rkind * (slen-i)); } else { /* interleave */ while (n > 0) { - Py_UNICODE_COPY(p, str2->str, str2->length); - p += str2->length; + memcpy(res + rkind * ires, + buf2, + rkind * len2); + ires += len2; if (--n <= 0) break; - *p++ = self->str[i++]; + memcpy(res + rkind * ires, + sbuf + rkind * i, + rkind); + ires++; + i++; } - Py_UNICODE_COPY(p, self->str+i, self->length-i); - } - } - return (PyObject *) u; + memcpy(res + rkind * ires, + sbuf + rkind * i, + rkind * (slen-i)); + } + u = rstr; + unicode_adjust_maxchar(&u); + if (u == NULL) + goto error; + } + if (srelease) + PyMem_FREE(sbuf); + if (release1) + PyMem_FREE(buf1); + if (release2) + PyMem_FREE(buf2); + assert(_PyUnicode_CheckConsistency(u, 1)); + return u; nothing: /* nothing to replace; return original string (when possible) */ + if (srelease) + PyMem_FREE(sbuf); + if (release1) + PyMem_FREE(buf1); + if (release2) + PyMem_FREE(buf2); if (PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject *) self; } - return PyUnicode_FromUnicode(self->str, self->length); + return PyUnicode_Copy(self); + error: + if (srelease && sbuf) + PyMem_FREE(sbuf); + if (release1 && buf1) + PyMem_FREE(buf1); + if (release2 && buf2) + PyMem_FREE(buf2); + return NULL; } /* --- Unicode Object Methods --------------------------------------------- */ @@ -7096,7 +9904,7 @@ Return a titlecased version of S, i.e. words start with title case\n\ characters, all remaining cased characters have lower case."); static PyObject* -unicode_title(PyUnicodeObject *self) +unicode_title(PyObject *self) { return fixup(self, fixtitle); } @@ -7108,7 +9916,7 @@ Return a capitalized version of S, i.e. make the first character\n\ have upper case and the rest lower case."); static PyObject* -unicode_capitalize(PyUnicodeObject *self) +unicode_capitalize(PyObject *self) { return fixup(self, fixcapitalize); } @@ -7156,9 +9964,8 @@ unicode_capwords(PyUnicodeObject *self) static int convert_uc(PyObject *obj, void *addr) { - Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; + Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; PyObject *uniobj; - Py_UNICODE *unistr; uniobj = PyUnicode_FromObject(obj); if (uniobj == NULL) { @@ -7166,14 +9973,13 @@ convert_uc(PyObject *obj, void *addr) "The fill character cannot be converted to Unicode"); return 0; } - if (PyUnicode_GET_SIZE(uniobj) != 1) { + if (PyUnicode_GET_LENGTH(uniobj) != 1) { PyErr_SetString(PyExc_TypeError, "The fill character must be exactly one character long"); Py_DECREF(uniobj); return 0; } - unistr = PyUnicode_AS_UNICODE(uniobj); - *fillcharloc = unistr[0]; + *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); Py_DECREF(uniobj); return 1; } @@ -7185,24 +9991,27 @@ Return S centered in a string of length width. Padding is\n\ done using the specified fill character (default is a space)"); static PyObject * -unicode_center(PyUnicodeObject *self, PyObject *args) +unicode_center(PyObject *self, PyObject *args) { Py_ssize_t marg, left; Py_ssize_t width; - Py_UNICODE fillchar = ' '; + Py_UCS4 fillchar = ' '; if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) return NULL; - if (self->length >= width && PyUnicode_CheckExact(self)) { + if (PyUnicode_READY(self) == -1) + return NULL; + + if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } - marg = width - self->length; + marg = width - _PyUnicode_LENGTH(self); left = marg / 2 + (marg & width & 1); - return (PyObject*) pad(self, left, marg - left, fillchar); + return pad(self, left, marg - left, fillchar); } #if 0 @@ -7231,8 +10040,8 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) Py_UNICODE *s1 = str1->str; Py_UNICODE *s2 = str2->str; - len1 = str1->length; - len2 = str2->length; + len1 = str1->_base._base.length; + len2 = str2->_base._base.length; while (len1 > 0 && len2 > 0) { Py_UNICODE c1, c2; @@ -7257,27 +10066,29 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) #else +/* This function assumes that str1 and str2 are readied by the caller. */ + static int unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) { - register Py_ssize_t len1, len2; + int kind1, kind2; + void *data1, *data2; + Py_ssize_t len1, len2, i; - Py_UNICODE *s1 = str1->str; - Py_UNICODE *s2 = str2->str; - - len1 = str1->length; - len2 = str2->length; + kind1 = PyUnicode_KIND(str1); + kind2 = PyUnicode_KIND(str2); + data1 = PyUnicode_DATA(str1); + data2 = PyUnicode_DATA(str2); + len1 = PyUnicode_GET_LENGTH(str1); + len2 = PyUnicode_GET_LENGTH(str2); - while (len1 > 0 && len2 > 0) { - Py_UNICODE c1, c2; - - c1 = *s1++; - c2 = *s2++; + for (i = 0; i < len1 && i < len2; ++i) { + Py_UCS4 c1, c2; + c1 = PyUnicode_READ(kind1, data1, i); + c2 = PyUnicode_READ(kind2, data2, i); if (c1 != c2) return (c1 < c2) ? -1 : 1; - - len1--; len2--; } return (len1 < len2) ? -1 : (len1 != len2); @@ -7285,12 +10096,16 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) #endif -int PyUnicode_Compare(PyObject *left, - PyObject *right) +int +PyUnicode_Compare(PyObject *left, PyObject *right) { - if (PyUnicode_Check(left) && PyUnicode_Check(right)) + if (PyUnicode_Check(left) && PyUnicode_Check(right)) { + if (PyUnicode_READY(left) == -1 || + PyUnicode_READY(right) == -1) + return -1; return unicode_compare((PyUnicodeObject *)left, (PyUnicodeObject *)right); + } PyErr_Format(PyExc_TypeError, "Can't compare %.100s and %.100s", left->ob_type->tp_name, @@ -7301,17 +10116,23 @@ int PyUnicode_Compare(PyObject *left, int PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) { - int i; - Py_UNICODE *id; - assert(PyUnicode_Check(uni)); - id = PyUnicode_AS_UNICODE(uni); + Py_ssize_t i; + int kind; + void *data; + Py_UCS4 chr; + + assert(_PyUnicode_CHECK(uni)); + if (PyUnicode_READY(uni) == -1) + return -1; + kind = PyUnicode_KIND(uni); + data = PyUnicode_DATA(uni); /* Compare Unicode string and source character set string */ - for (i = 0; id[i] && str[i]; i++) - if (id[i] != str[i]) - return ((int)id[i] < (int)str[i]) ? -1 : 1; + for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) + if (chr != str[i]) + return (chr < (unsigned char)(str[i])) ? -1 : 1; /* This check keeps Python strings that end in '\0' from comparing equal to C strings identical up to that point. */ - if (PyUnicode_GET_SIZE(uni) != i || id[i]) + if (PyUnicode_GET_LENGTH(uni) != i || chr) return 1; /* uni is longer */ if (str[i]) return -1; /* str is longer */ @@ -7322,16 +10143,18 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) #define TEST_COND(cond) \ ((cond) ? Py_True : Py_False) -PyObject *PyUnicode_RichCompare(PyObject *left, - PyObject *right, - int op) +PyObject * +PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) { int result; if (PyUnicode_Check(left) && PyUnicode_Check(right)) { PyObject *v; - if (((PyUnicodeObject *) left)->length != - ((PyUnicodeObject *) right)->length) { + if (PyUnicode_READY(left) == -1 || + PyUnicode_READY(right) == -1) + return NULL; + if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || + PyUnicode_KIND(left) != PyUnicode_KIND(right)) { if (op == Py_EQ) { Py_INCREF(Py_False); return Py_False; @@ -7375,14 +10198,16 @@ PyObject *PyUnicode_RichCompare(PyObject *left, return v; } - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } -int PyUnicode_Contains(PyObject *container, - PyObject *element) +int +PyUnicode_Contains(PyObject *container, PyObject *element) { PyObject *str, *sub; + int kind1, kind2, kind; + void *buf1, *buf2; + Py_ssize_t len1, len2; int result; /* Coerce the two arguments */ @@ -7393,56 +10218,103 @@ int PyUnicode_Contains(PyObject *container, element->ob_type->tp_name); return -1; } + if (PyUnicode_READY(sub) == -1) + return -1; str = PyUnicode_FromObject(container); - if (!str) { + if (!str || PyUnicode_READY(str) == -1) { + Py_DECREF(sub); + return -1; + } + + kind1 = PyUnicode_KIND(str); + kind2 = PyUnicode_KIND(sub); + kind = kind1 > kind2 ? kind1 : kind2; + buf1 = PyUnicode_DATA(str); + buf2 = PyUnicode_DATA(sub); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind((PyObject*)str, kind); + if (!buf1) { + Py_DECREF(sub); + return -1; + } + if (kind2 != kind) + buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); + if (!buf2) { Py_DECREF(sub); + if (kind1 != kind) PyMem_Free(buf1); return -1; } + len1 = PyUnicode_GET_LENGTH(str); + len2 = PyUnicode_GET_LENGTH(sub); - result = stringlib_contains_obj(str, sub); + switch(kind) { + case PyUnicode_1BYTE_KIND: + result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; + break; + case PyUnicode_2BYTE_KIND: + result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; + break; + case PyUnicode_4BYTE_KIND: + result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; + break; + default: + result = -1; + assert(0); + } Py_DECREF(str); Py_DECREF(sub); + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); + return result; } /* Concat to string or Unicode object giving a new Unicode object. */ -PyObject *PyUnicode_Concat(PyObject *left, - PyObject *right) +PyObject * +PyUnicode_Concat(PyObject *left, PyObject *right) { - PyUnicodeObject *u = NULL, *v = NULL, *w; + PyObject *u = NULL, *v = NULL, *w; + Py_UCS4 maxchar; /* Coerce the two arguments */ - u = (PyUnicodeObject *)PyUnicode_FromObject(left); + u = PyUnicode_FromObject(left); if (u == NULL) goto onError; - v = (PyUnicodeObject *)PyUnicode_FromObject(right); + v = PyUnicode_FromObject(right); if (v == NULL) goto onError; /* Shortcuts */ if (v == unicode_empty) { Py_DECREF(v); - return (PyObject *)u; + return u; } if (u == unicode_empty) { Py_DECREF(u); - return (PyObject *)v; + return v; } + maxchar = PyUnicode_MAX_CHAR_VALUE(u); + maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); + /* Concat the two Unicode strings */ - w = _PyUnicode_New(u->length + v->length); + w = PyUnicode_New( + PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), + maxchar); if (w == NULL) goto onError; - Py_UNICODE_COPY(w->str, u->str, u->length); - Py_UNICODE_COPY(w->str + u->length, v->str, v->length); - + copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); + copy_characters(w, PyUnicode_GET_LENGTH(u), v, 0, PyUnicode_GET_LENGTH(v)); Py_DECREF(u); Py_DECREF(v); - return (PyObject *)w; + assert(_PyUnicode_CheckConsistency(w, 1)); + return w; onError: Py_XDECREF(u); @@ -7450,20 +10322,96 @@ PyObject *PyUnicode_Concat(PyObject *left, return NULL; } +static void +unicode_append_inplace(PyObject **p_left, PyObject *right) +{ + Py_ssize_t left_len, right_len, new_len; + + assert(PyUnicode_IS_READY(*p_left)); + assert(PyUnicode_IS_READY(right)); + + left_len = PyUnicode_GET_LENGTH(*p_left); + right_len = PyUnicode_GET_LENGTH(right); + if (left_len > PY_SSIZE_T_MAX - right_len) { + PyErr_SetString(PyExc_OverflowError, + "strings are too large to concat"); + goto error; + } + new_len = left_len + right_len; + + /* Now we own the last reference to 'left', so we can resize it + * in-place. + */ + if (unicode_resize(p_left, new_len) != 0) { + /* XXX if _PyUnicode_Resize() fails, 'left' has been + * deallocated so it cannot be put back into + * 'variable'. The MemoryError is raised when there + * is no value in 'variable', which might (very + * remotely) be a cause of incompatibilities. + */ + goto error; + } + /* copy 'right' into the newly allocated area of 'left' */ + copy_characters(*p_left, left_len, right, 0, right_len); + _PyUnicode_DIRTY(*p_left); + return; + +error: + Py_DECREF(*p_left); + *p_left = NULL; +} + void -PyUnicode_Append(PyObject **pleft, PyObject *right) +PyUnicode_Append(PyObject **p_left, PyObject *right) { - PyObject *new; - if (*pleft == NULL) - return; - if (right == NULL || !PyUnicode_Check(*pleft)) { - Py_DECREF(*pleft); - *pleft = NULL; + PyObject *left, *res; + + if (p_left == NULL) { + if (!PyErr_Occurred()) + PyErr_BadInternalCall(); return; } - new = PyUnicode_Concat(*pleft, right); - Py_DECREF(*pleft); - *pleft = new; + left = *p_left; + if (right == NULL || !PyUnicode_Check(left)) { + if (!PyErr_Occurred()) + PyErr_BadInternalCall(); + goto error; + } + + if (PyUnicode_READY(left)) + goto error; + if (PyUnicode_READY(right)) + goto error; + + if (PyUnicode_CheckExact(left) && left != unicode_empty + && PyUnicode_CheckExact(right) && right != unicode_empty + && unicode_resizable(left) + && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) + || _PyUnicode_WSTR(left) != NULL)) + { + /* Don't resize for ascii += latin1. Convert ascii to latin1 requires + to change the structure size, but characters are stored just after + the structure, and so it requires to move all characters which is + not so different than duplicating the string. */ + if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) + { + unicode_append_inplace(p_left, right); + if (p_left != NULL) + assert(_PyUnicode_CheckConsistency(*p_left, 1)); + return; + } + } + + res = PyUnicode_Concat(left, right); + if (res == NULL) + goto error; + Py_DECREF(left); + *p_left = res; + return; + +error: + Py_DECREF(*p_left); + *p_left = NULL; } void @@ -7487,17 +10435,65 @@ unicode_count(PyUnicodeObject *self, PyObject *args) Py_ssize_t start = 0; Py_ssize_t end = PY_SSIZE_T_MAX; PyObject *result; + int kind1, kind2, kind; + void *buf1, *buf2; + Py_ssize_t len1, len2, iresult; if (!stringlib_parse_args_finds_unicode("count", args, &substring, &start, &end)) return NULL; - ADJUST_INDICES(start, end, self->length); - result = PyLong_FromSsize_t( - stringlib_count(self->str + start, end - start, - substring->str, substring->length, - PY_SSIZE_T_MAX) - ); + kind1 = PyUnicode_KIND(self); + kind2 = PyUnicode_KIND(substring); + kind = kind1 > kind2 ? kind1 : kind2; + buf1 = PyUnicode_DATA(self); + buf2 = PyUnicode_DATA(substring); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind((PyObject*)self, kind); + if (!buf1) { + Py_DECREF(substring); + return NULL; + } + if (kind2 != kind) + buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); + if (!buf2) { + Py_DECREF(substring); + if (kind1 != kind) PyMem_Free(buf1); + return NULL; + } + len1 = PyUnicode_GET_LENGTH(self); + len2 = PyUnicode_GET_LENGTH(substring); + + ADJUST_INDICES(start, end, len1); + switch(kind) { + case PyUnicode_1BYTE_KIND: + iresult = ucs1lib_count( + ((Py_UCS1*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + break; + case PyUnicode_2BYTE_KIND: + iresult = ucs2lib_count( + ((Py_UCS2*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + break; + case PyUnicode_4BYTE_KIND: + iresult = ucs4lib_count( + ((Py_UCS4*)buf1) + start, end - start, + buf2, len2, PY_SSIZE_T_MAX + ); + break; + default: + assert(0); iresult = 0; + } + + result = PyLong_FromSsize_t(iresult); + + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); Py_DECREF(substring); @@ -7536,80 +10532,91 @@ If tabsize is not given, a tab size of 8 characters is assumed."); static PyObject* unicode_expandtabs(PyUnicodeObject *self, PyObject *args) { - Py_UNICODE *e; - Py_UNICODE *p; - Py_UNICODE *q; - Py_UNICODE *qe; - Py_ssize_t i, j, incr; - PyUnicodeObject *u; + Py_ssize_t i, j, line_pos, src_len, incr; + Py_UCS4 ch; + PyObject *u; + void *src_data, *dest_data; int tabsize = 8; + int kind; + int found; if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) return NULL; + if (PyUnicode_READY(self) == -1) + return NULL; + /* First pass: determine size of output string */ - i = 0; /* chars up to and including most recent \n or \r */ - j = 0; /* chars since most recent \n or \r (use in tab calculations) */ - e = self->str + self->length; /* end of input */ - for (p = self->str; p < e; p++) - if (*p == '\t') { + src_len = PyUnicode_GET_LENGTH(self); + i = j = line_pos = 0; + kind = PyUnicode_KIND(self); + src_data = PyUnicode_DATA(self); + found = 0; + for (; i < src_len; i++) { + ch = PyUnicode_READ(kind, src_data, i); + if (ch == '\t') { + found = 1; if (tabsize > 0) { - incr = tabsize - (j % tabsize); /* cannot overflow */ + incr = tabsize - (line_pos % tabsize); /* cannot overflow */ if (j > PY_SSIZE_T_MAX - incr) - goto overflow1; + goto overflow; + line_pos += incr; j += incr; } } else { if (j > PY_SSIZE_T_MAX - 1) - goto overflow1; + goto overflow; + line_pos++; j++; - if (*p == '\n' || *p == '\r') { - if (i > PY_SSIZE_T_MAX - j) - goto overflow1; - i += j; - j = 0; - } + if (ch == '\n' || ch == '\r') + line_pos = 0; } - - if (i > PY_SSIZE_T_MAX - j) - goto overflow1; + } + if (!found && PyUnicode_CheckExact(self)) { + Py_INCREF((PyObject *) self); + return (PyObject *) self; + } /* Second pass: create output string and fill it */ - u = _PyUnicode_New(i + j); + u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); if (!u) return NULL; + dest_data = PyUnicode_DATA(u); - j = 0; /* same as in first pass */ - q = u->str; /* next output char */ - qe = u->str + u->length; /* end of output */ + i = j = line_pos = 0; - for (p = self->str; p < e; p++) - if (*p == '\t') { + for (; i < src_len; i++) { + ch = PyUnicode_READ(kind, src_data, i); + if (ch == '\t') { if (tabsize > 0) { - i = tabsize - (j % tabsize); - j += i; - while (i--) { - if (q >= qe) - goto overflow2; - *q++ = ' '; + incr = tabsize - (line_pos % tabsize); + line_pos += incr; + while (incr--) { + PyUnicode_WRITE(kind, dest_data, j, ' '); + j++; } } } else { - if (q >= qe) - goto overflow2; - *q++ = *p; + line_pos++; + PyUnicode_WRITE(kind, dest_data, j, ch); j++; - if (*p == '\n' || *p == '\r') - j = 0; + if (ch == '\n' || ch == '\r') + line_pos = 0; } - + } + assert (j == PyUnicode_GET_LENGTH(u)); +#ifndef DONT_MAKE_RESULT_READY + if (_PyUnicode_READY_REPLACE(&u)) { + Py_DECREF(u); + return NULL; + } +#endif + assert(_PyUnicode_CheckConsistency(u, 1)); return (PyObject*) u; - overflow2: - Py_DECREF(u); - overflow1: + overflow: PyErr_SetString(PyExc_OverflowError, "new string is too long"); return NULL; } @@ -7624,7 +10631,7 @@ arguments start and end are interpreted as in slice notation.\n\ Return -1 on failure."); static PyObject * -unicode_find(PyUnicodeObject *self, PyObject *args) +unicode_find(PyObject *self, PyObject *args) { PyUnicodeObject *substring; Py_ssize_t start; @@ -7635,50 +10642,82 @@ unicode_find(PyUnicodeObject *self, PyObject *args) &start, &end)) return NULL; - result = stringlib_find_slice( - PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), - PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), - start, end + if (PyUnicode_READY(self) == -1) + return NULL; + if (PyUnicode_READY(substring) == -1) + return NULL; + + result = any_find_slice( + asciilib_find_slice, ucs1lib_find_slice, + ucs2lib_find_slice, ucs4lib_find_slice, + self, (PyObject*)substring, start, end ); Py_DECREF(substring); + if (result == -2) + return NULL; + return PyLong_FromSsize_t(result); } static PyObject * -unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) +unicode_getitem(PyObject *self, Py_ssize_t index) { - if (index < 0 || index >= self->length) { - PyErr_SetString(PyExc_IndexError, "string index out of range"); + Py_UCS4 ch = PyUnicode_ReadChar(self, index); + if (ch == (Py_UCS4)-1) return NULL; - } - - return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); + return PyUnicode_FromOrdinal(ch); } /* Believe it or not, this produces the same value for ASCII strings - as string_hash(). */ + as bytes_hash(). */ static Py_hash_t unicode_hash(PyUnicodeObject *self) { Py_ssize_t len; - Py_UNICODE *p; - Py_hash_t x; - - if (self->hash != -1) - return self->hash; - len = Py_SIZE(self); - p = self->str; - x = *p << 7; - while (--len >= 0) - x = (1000003*x) ^ *p++; - x ^= Py_SIZE(self); + Py_uhash_t x; + + if (_PyUnicode_HASH(self) != -1) + return _PyUnicode_HASH(self); + if (PyUnicode_READY(self) == -1) + return -1; + len = PyUnicode_GET_LENGTH(self); + + /* The hash function as a macro, gets expanded three times below. */ +#define HASH(P) \ + x = (Py_uhash_t)*P << 7; \ + while (--len >= 0) \ + x = (1000003*x) ^ (Py_uhash_t)*P++; + + switch (PyUnicode_KIND(self)) { + case PyUnicode_1BYTE_KIND: { + const unsigned char *c = PyUnicode_1BYTE_DATA(self); + HASH(c); + break; + } + case PyUnicode_2BYTE_KIND: { + const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); + HASH(s); + break; + } + default: { + Py_UCS4 *l; + assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && + "Impossible switch case in unicode_hash"); + l = PyUnicode_4BYTE_DATA(self); + HASH(l); + break; + } + } + x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); + if (x == -1) x = -2; - self->hash = x; + _PyUnicode_HASH(self) = x; return x; } +#undef HASH PyDoc_STRVAR(index__doc__, "S.index(sub[, start[, end]]) -> int\n\ @@ -7686,7 +10725,7 @@ PyDoc_STRVAR(index__doc__, Like S.find() but raise ValueError when the substring is not found."); static PyObject * -unicode_index(PyUnicodeObject *self, PyObject *args) +unicode_index(PyObject *self, PyObject *args) { Py_ssize_t result; PyUnicodeObject *substring; @@ -7697,14 +10736,22 @@ unicode_index(PyUnicodeObject *self, PyObject *args) &start, &end)) return NULL; - result = stringlib_find_slice( - PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), - PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), - start, end + if (PyUnicode_READY(self) == -1) + return NULL; + if (PyUnicode_READY(substring) == -1) + return NULL; + + result = any_find_slice( + asciilib_find_slice, ucs1lib_find_slice, + ucs2lib_find_slice, ucs4lib_find_slice, + self, (PyObject*)substring, start, end ); Py_DECREF(substring); + if (result == -2) + return NULL; + if (result < 0) { PyErr_SetString(PyExc_ValueError, "substring not found"); return NULL; @@ -7722,22 +10769,29 @@ at least one cased character in S, False otherwise."); static PyObject* unicode_islower(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; int cased; + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); + /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1) - return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); cased = 0; - while (p < e) { - const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); + for (i = 0; i < length; i++) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) return PyBool_FromLong(0); @@ -7756,22 +10810,29 @@ at least one cased character in S, False otherwise."); static PyObject* unicode_isupper(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; int cased; + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); + /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1) - return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); cased = 0; - while (p < e) { - const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); + for (i = 0; i < length; i++) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) return PyBool_FromLong(0); @@ -7792,24 +10853,32 @@ Return False otherwise."); static PyObject* unicode_istitle(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; int cased, previous_is_cased; + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); + /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1) - return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || - (Py_UNICODE_ISUPPER(*p) != 0)); + if (length == 1) { + Py_UCS4 ch = PyUnicode_READ(kind, data, 0); + return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || + (Py_UNICODE_ISUPPER(ch) != 0)); + } /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); cased = 0; previous_is_cased = 0; - while (p < e) { - const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); + for (i = 0; i < length; i++) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { if (previous_is_cased) @@ -7838,21 +10907,27 @@ and there is at least one character in S, False otherwise."); static PyObject* unicode_isspace(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; + + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && - Py_UNICODE_ISSPACE(*p)) - return PyBool_FromLong(1); + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); + for (i = 0; i < length; i++) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (!Py_UNICODE_ISSPACE(ch)) return PyBool_FromLong(0); } @@ -7868,21 +10943,27 @@ and there is at least one character in S, False otherwise."); static PyObject* unicode_isalpha(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; + + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && - Py_UNICODE_ISALPHA(*p)) - return PyBool_FromLong(1); + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - if (!Py_UNICODE_ISALPHA(_Py_UNICODE_NEXT(p, e))) + for (i = 0; i < length; i++) { + if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) return PyBool_FromLong(0); } return PyBool_FromLong(1); @@ -7897,21 +10978,29 @@ and there is at least one character in S, False otherwise."); static PyObject* unicode_isalnum(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + int kind; + void *data; + Py_ssize_t len, i; + + if (PyUnicode_READY(self) == -1) + return NULL; + + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); + len = PyUnicode_GET_LENGTH(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && - Py_UNICODE_ISALNUM(*p)) - return PyBool_FromLong(1); + if (len == 1) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); + return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); + } /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (len == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e); + for (i = 0; i < len; i++) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (!Py_UNICODE_ISALNUM(ch)) return PyBool_FromLong(0); } @@ -7927,21 +11016,27 @@ False otherwise."); static PyObject* unicode_isdecimal(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; + + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && - Py_UNICODE_ISDECIMAL(*p)) - return PyBool_FromLong(1); + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - if (!Py_UNICODE_ISDECIMAL(_Py_UNICODE_NEXT(p, e))) + for (i = 0; i < length; i++) { + if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) return PyBool_FromLong(0); } return PyBool_FromLong(1); @@ -7956,21 +11051,28 @@ and there is at least one character in S, False otherwise."); static PyObject* unicode_isdigit(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; + + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && - Py_UNICODE_ISDIGIT(*p)) - return PyBool_FromLong(1); + if (length == 1) { + const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); + return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); + } /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - if (!Py_UNICODE_ISDIGIT(_Py_UNICODE_NEXT(p, e))) + for (i = 0; i < length; i++) { + if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) return PyBool_FromLong(0); } return PyBool_FromLong(1); @@ -7985,21 +11087,27 @@ False otherwise."); static PyObject* unicode_isnumeric(PyUnicodeObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; + + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && - Py_UNICODE_ISNUMERIC(*p)) - return PyBool_FromLong(1); + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (length == 0) return PyBool_FromLong(0); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - if (!Py_UNICODE_ISNUMERIC(_Py_UNICODE_NEXT(p, e))) + for (i = 0; i < length; i++) { + if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) return PyBool_FromLong(0); } return PyBool_FromLong(1); @@ -8008,13 +11116,21 @@ unicode_isnumeric(PyUnicodeObject *self) int PyUnicode_IsIdentifier(PyObject *self) { - const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); - const Py_UNICODE *e; + int kind; + void *data; + Py_ssize_t i; Py_UCS4 first; + if (PyUnicode_READY(self) == -1) { + Py_FatalError("identifier not ready"); + return 0; + } + /* Special case for empty strings */ - if (PyUnicode_GET_SIZE(self) == 0) + if (PyUnicode_GET_LENGTH(self) == 0) return 0; + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* PEP 3131 says that the first character must be in XID_Start and subsequent characters in XID_Continue, @@ -8024,13 +11140,12 @@ PyUnicode_IsIdentifier(PyObject *self) definition of XID_Start and XID_Continue, it is sufficient to check just for these, except that _ must be allowed as starting an identifier. */ - e = p + PyUnicode_GET_SIZE(self); - first = _Py_UNICODE_NEXT(p, e); + first = PyUnicode_READ(kind, data, 0); if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) return 0; - while (p < e) - if (!_PyUnicode_IsXidContinue(_Py_UNICODE_NEXT(p, e))) + for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) + if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) return 0; return 1; } @@ -8056,17 +11171,23 @@ printable in repr() or S is empty, False otherwise."); static PyObject* unicode_isprintable(PyObject *self) { - register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); - register const Py_UNICODE *e; + Py_ssize_t i, length; + int kind; + void *data; + + if (PyUnicode_READY(self) == -1) + return NULL; + length = PyUnicode_GET_LENGTH(self); + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); /* Shortcut for single character strings */ - if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { - Py_RETURN_TRUE; - } + if (length == 1) + return PyBool_FromLong( + Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); - e = p + PyUnicode_GET_SIZE(self); - while (p < e) { - if (!Py_UNICODE_ISPRINTABLE(_Py_UNICODE_NEXT(p, e))) { + for (i = 0; i < length; i++) { + if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { Py_RETURN_FALSE; } } @@ -8088,7 +11209,9 @@ unicode_join(PyObject *self, PyObject *data) static Py_ssize_t unicode_length(PyUnicodeObject *self) { - return self->length; + if (PyUnicode_READY(self) == -1) + return -1; + return PyUnicode_GET_LENGTH(self); } PyDoc_STRVAR(ljust__doc__, @@ -8098,20 +11221,23 @@ Return S left-justified in a Unicode string of length width. Padding is\n\ done using the specified fill character (default is a space)."); static PyObject * -unicode_ljust(PyUnicodeObject *self, PyObject *args) +unicode_ljust(PyObject *self, PyObject *args) { Py_ssize_t width; - Py_UNICODE fillchar = ' '; + Py_UCS4 fillchar = ' '; + + if (PyUnicode_READY(self) == -1) + return NULL; if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) return NULL; - if (self->length >= width && PyUnicode_CheckExact(self)) { + if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } - return (PyObject*) pad(self, 0, width - self->length, fillchar); + return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); } PyDoc_STRVAR(lower__doc__, @@ -8120,7 +11246,7 @@ PyDoc_STRVAR(lower__doc__, Return a copy of the string S converted to lowercase."); static PyObject* -unicode_lower(PyUnicodeObject *self) +unicode_lower(PyObject *self) { return fixup(self, fixlower); } @@ -8138,17 +11264,25 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; PyObject * _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) { - Py_UNICODE *s = PyUnicode_AS_UNICODE(self); - Py_ssize_t len = PyUnicode_GET_SIZE(self); - Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); - Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); - Py_ssize_t i, j; + void *data; + int kind; + Py_ssize_t i, j, len; + BLOOM_MASK sepmask; - BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); + if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) + return NULL; + + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); + len = PyUnicode_GET_LENGTH(self); + sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), + PyUnicode_DATA(sepobj), + PyUnicode_GET_LENGTH(sepobj)); i = 0; if (striptype != RIGHTSTRIP) { - while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { + while (i < len && + BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { i++; } } @@ -8157,28 +11291,76 @@ _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) if (striptype != LEFTSTRIP) { do { j--; - } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); + } while (j >= i && + BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); j++; } - if (i == 0 && j == len && PyUnicode_CheckExact(self)) { - Py_INCREF(self); - return (PyObject*)self; - } - else - return PyUnicode_FromUnicode(s+i, j-i); + return PyUnicode_Substring((PyObject*)self, i, j); } +PyObject* +PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) +{ + unsigned char *data; + int kind; + Py_ssize_t length; + + if (PyUnicode_READY(self) == -1) + return NULL; + + end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); + + if (start == 0 && end == PyUnicode_GET_LENGTH(self)) + { + if (PyUnicode_CheckExact(self)) { + Py_INCREF(self); + return self; + } + else + return PyUnicode_Copy(self); + } + + length = end - start; + if (length == 1) + return unicode_getitem(self, start); + + if (start < 0 || end < 0) { + PyErr_SetString(PyExc_IndexError, "string index out of range"); + return NULL; + } + + if (PyUnicode_IS_ASCII(self)) { + kind = PyUnicode_KIND(self); + data = PyUnicode_1BYTE_DATA(self); + return unicode_fromascii(data + start, length); + } + else { + kind = PyUnicode_KIND(self); + data = PyUnicode_1BYTE_DATA(self); + return PyUnicode_FromKindAndData(kind, + data + kind * start, + length); + } +} static PyObject * do_strip(PyUnicodeObject *self, int striptype) { - Py_UNICODE *s = PyUnicode_AS_UNICODE(self); - Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; + int kind; + void *data; + Py_ssize_t len, i, j; + + if (PyUnicode_READY(self) == -1) + return NULL; + + kind = PyUnicode_KIND(self); + data = PyUnicode_DATA(self); + len = PyUnicode_GET_LENGTH(self); i = 0; if (striptype != RIGHTSTRIP) { - while (i < len && Py_UNICODE_ISSPACE(s[i])) { + while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { i++; } } @@ -8187,16 +11369,11 @@ do_strip(PyUnicodeObject *self, int striptype) if (striptype != LEFTSTRIP) { do { j--; - } while (j >= i && Py_UNICODE_ISSPACE(s[j])); + } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); j++; } - if (i == 0 && j == len && PyUnicode_CheckExact(self)) { - Py_INCREF(self); - return (PyObject*)self; - } - else - return PyUnicode_FromUnicode(s+i, j-i); + return PyUnicode_Substring((PyObject*)self, i, j); } @@ -8276,13 +11453,11 @@ static PyObject* unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) { PyUnicodeObject *u; - Py_UNICODE *p; - Py_ssize_t nchars; - size_t nbytes; + Py_ssize_t nchars, n; if (len < 1) { Py_INCREF(unicode_empty); - return (PyObject *)unicode_empty; + return unicode_empty; } if (len == 1 && PyUnicode_CheckExact(str)) { @@ -8291,46 +11466,55 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) return (PyObject*) str; } - /* ensure # of chars needed doesn't overflow int and # of bytes - * needed doesn't overflow size_t - */ - nchars = len * str->length; - if (nchars / len != str->length) { - PyErr_SetString(PyExc_OverflowError, - "repeated string is too long"); + if (PyUnicode_READY(str) == -1) return NULL; - } - nbytes = (nchars + 1) * sizeof(Py_UNICODE); - if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { + + if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { PyErr_SetString(PyExc_OverflowError, "repeated string is too long"); return NULL; } - u = _PyUnicode_New(nchars); + nchars = len * PyUnicode_GET_LENGTH(str); + + u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); if (!u) return NULL; - - p = u->str; - - if (str->length == 1) { - Py_UNICODE_FILL(p, str->str[0], len); - } else { - Py_ssize_t done = str->length; /* number of characters copied this far */ - Py_UNICODE_COPY(p, str->str, str->length); + assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); + + if (PyUnicode_GET_LENGTH(str) == 1) { + const int kind = PyUnicode_KIND(str); + const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); + void *to = PyUnicode_DATA(u); + if (kind == PyUnicode_1BYTE_KIND) + memset(to, (unsigned char)fill_char, len); + else { + for (n = 0; n < len; ++n) + PyUnicode_WRITE(kind, to, n, fill_char); + } + } + else { + /* number of characters copied this far */ + Py_ssize_t done = PyUnicode_GET_LENGTH(str); + const Py_ssize_t char_size = PyUnicode_KIND(str); + char *to = (char *) PyUnicode_DATA(u); + Py_MEMCPY(to, PyUnicode_DATA(str), + PyUnicode_GET_LENGTH(str) * char_size); while (done < nchars) { - Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; - Py_UNICODE_COPY(p+done, p, n); + n = (done <= nchars-done) ? done : nchars-done; + Py_MEMCPY(to + (done * char_size), to, n * char_size); done += n; } } + assert(_PyUnicode_CheckConsistency(u, 1)); return (PyObject*) u; } -PyObject *PyUnicode_Replace(PyObject *obj, - PyObject *subobj, - PyObject *replobj, - Py_ssize_t maxcount) +PyObject * +PyUnicode_Replace(PyObject *obj, + PyObject *subobj, + PyObject *replobj, + Py_ssize_t maxcount) { PyObject *self; PyObject *str1; @@ -8338,23 +11522,20 @@ PyObject *PyUnicode_Replace(PyObject *obj, PyObject *result; self = PyUnicode_FromObject(obj); - if (self == NULL) + if (self == NULL || PyUnicode_READY(self) == -1) return NULL; str1 = PyUnicode_FromObject(subobj); - if (str1 == NULL) { + if (str1 == NULL || PyUnicode_READY(str1) == -1) { Py_DECREF(self); return NULL; } str2 = PyUnicode_FromObject(replobj); - if (str2 == NULL) { + if (str2 == NULL || PyUnicode_READY(str2)) { Py_DECREF(self); Py_DECREF(str1); return NULL; } - result = replace((PyUnicodeObject *)self, - (PyUnicodeObject *)str1, - (PyUnicodeObject *)str2, - maxcount); + result = replace(self, str1, str2, maxcount); Py_DECREF(self); Py_DECREF(str1); Py_DECREF(str2); @@ -8369,20 +11550,22 @@ old replaced by new. If the optional argument count is\n\ given, only the first count occurrences are replaced."); static PyObject* -unicode_replace(PyUnicodeObject *self, PyObject *args) +unicode_replace(PyObject *self, PyObject *args) { - PyUnicodeObject *str1; - PyUnicodeObject *str2; + PyObject *str1; + PyObject *str2; Py_ssize_t maxcount = -1; PyObject *result; if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) return NULL; - str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); - if (str1 == NULL) + if (!PyUnicode_READY(self) == -1) + return NULL; + str1 = PyUnicode_FromObject(str1); + if (str1 == NULL || PyUnicode_READY(str1) == -1) return NULL; - str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); - if (str2 == NULL) { + str2 = PyUnicode_FromObject(str2); + if (str2 == NULL || PyUnicode_READY(str2) == -1) { Py_DECREF(str1); return NULL; } @@ -8394,152 +11577,154 @@ unicode_replace(PyUnicodeObject *self, PyObject *args) return result; } -static -PyObject *unicode_repr(PyObject *unicode) +static PyObject * +unicode_repr(PyObject *unicode) { PyObject *repr; - Py_UNICODE *p; - Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); - Py_ssize_t size = PyUnicode_GET_SIZE(unicode); + Py_ssize_t isize; + Py_ssize_t osize, squote, dquote, i, o; + Py_UCS4 max, quote; + int ikind, okind; + void *idata, *odata; - /* XXX(nnorwitz): rather than over-allocating, it would be - better to choose a different scheme. Perhaps scan the - first N-chars of the string and allocate based on that size. - */ - /* Initial allocation is based on the longest-possible unichr - escape. + if (PyUnicode_READY(unicode) == -1) + return NULL; - In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source - unichr, so in this case it's the longest unichr escape. In - narrow (UTF-16) builds this is five chars per source unichr - since there are two unichrs in the surrogate pair, so in narrow - (UTF-16) builds it's not the longest unichr escape. + isize = PyUnicode_GET_LENGTH(unicode); + idata = PyUnicode_DATA(unicode); + + /* Compute length of output, quote characters, and + maximum character */ + osize = 2; /* quotes */ + max = 127; + squote = dquote = 0; + ikind = PyUnicode_KIND(unicode); + for (i = 0; i < isize; i++) { + Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); + switch (ch) { + case '\'': squote++; osize++; break; + case '"': dquote++; osize++; break; + case '\\': case '\t': case '\r': case '\n': + osize += 2; break; + default: + /* Fast-path ASCII */ + if (ch < ' ' || ch == 0x7f) + osize += 4; /* \xHH */ + else if (ch < 0x7f) + osize++; + else if (Py_UNICODE_ISPRINTABLE(ch)) { + osize++; + max = ch > max ? ch : max; + } + else if (ch < 0x100) + osize += 4; /* \xHH */ + else if (ch < 0x10000) + osize += 6; /* \uHHHH */ + else + osize += 10; /* \uHHHHHHHH */ + } + } - In wide or narrow builds '\uxxxx' is 6 chars per source unichr, - so in the narrow (UTF-16) build case it's the longest unichr - escape. - */ + quote = '\''; + if (squote) { + if (dquote) + /* Both squote and dquote present. Use squote, + and escape them */ + osize += squote; + else + quote = '"'; + } - repr = PyUnicode_FromUnicode(NULL, - 2 /* quotes */ -#ifdef Py_UNICODE_WIDE - + 10*size -#else - + 6*size -#endif - + 1); + repr = PyUnicode_New(osize, max); if (repr == NULL) return NULL; + okind = PyUnicode_KIND(repr); + odata = PyUnicode_DATA(repr); - p = PyUnicode_AS_UNICODE(repr); + PyUnicode_WRITE(okind, odata, 0, quote); + PyUnicode_WRITE(okind, odata, osize-1, quote); - /* Add quote */ - *p++ = (findchar(s, size, '\'') && - !findchar(s, size, '"')) ? '"' : '\''; - while (size-- > 0) { - Py_UNICODE ch = *s++; + for (i = 0, o = 1; i < isize; i++) { + Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); /* Escape quotes and backslashes */ - if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { - *p++ = '\\'; - *p++ = ch; + if ((ch == quote) || (ch == '\\')) { + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, ch); continue; } /* Map special whitespace to '\t', \n', '\r' */ if (ch == '\t') { - *p++ = '\\'; - *p++ = 't'; + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 't'); } else if (ch == '\n') { - *p++ = '\\'; - *p++ = 'n'; + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'n'); } else if (ch == '\r') { - *p++ = '\\'; - *p++ = 'r'; + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'r'); } /* Map non-printable US ASCII to '\xhh' */ else if (ch < ' ' || ch == 0x7F) { - *p++ = '\\'; - *p++ = 'x'; - *p++ = hexdigits[(ch >> 4) & 0x000F]; - *p++ = hexdigits[ch & 0x000F]; + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'x'); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); } /* Copy ASCII characters as-is */ else if (ch < 0x7F) { - *p++ = ch; + PyUnicode_WRITE(okind, odata, o++, ch); } /* Non-ASCII characters */ else { - Py_UCS4 ucs = ch; - -#ifndef Py_UNICODE_WIDE - Py_UNICODE ch2 = 0; - /* Get code point from surrogate pair */ - if (size > 0) { - ch2 = *s; - if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 - && ch2 <= 0xDFFF) { - ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) - + 0x00010000; - s++; - size--; - } - } -#endif /* Map Unicode whitespace and control characters (categories Z* and C* except ASCII space) */ - if (!Py_UNICODE_ISPRINTABLE(ucs)) { + if (!Py_UNICODE_ISPRINTABLE(ch)) { /* Map 8-bit characters to '\xhh' */ - if (ucs <= 0xff) { - *p++ = '\\'; - *p++ = 'x'; - *p++ = hexdigits[(ch >> 4) & 0x000F]; - *p++ = hexdigits[ch & 0x000F]; + if (ch <= 0xff) { + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'x'); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); } /* Map 21-bit characters to '\U00xxxxxx' */ - else if (ucs >= 0x10000) { - *p++ = '\\'; - *p++ = 'U'; - *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; - *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; - *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; - *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; - *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; - *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; - *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; - *p++ = hexdigits[ucs & 0x0000000F]; + else if (ch >= 0x10000) { + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'U'); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); } /* Map 16-bit characters to '\uxxxx' */ else { - *p++ = '\\'; - *p++ = 'u'; - *p++ = hexdigits[(ucs >> 12) & 0x000F]; - *p++ = hexdigits[(ucs >> 8) & 0x000F]; - *p++ = hexdigits[(ucs >> 4) & 0x000F]; - *p++ = hexdigits[ucs & 0x000F]; + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'u'); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); } } /* Copy characters as-is */ else { - *p++ = ch; -#ifndef Py_UNICODE_WIDE - if (ucs >= 0x10000) - *p++ = ch2; -#endif + PyUnicode_WRITE(okind, odata, o++, ch); } } } - /* Add quote */ - *p++ = PyUnicode_AS_UNICODE(repr)[0]; - - *p = '\0'; - PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); + /* Closing quote already added at the beginning */ + assert(_PyUnicode_CheckConsistency(repr, 1)); return repr; } @@ -8553,7 +11738,7 @@ arguments start and end are interpreted as in slice notation.\n\ Return -1 on failure."); static PyObject * -unicode_rfind(PyUnicodeObject *self, PyObject *args) +unicode_rfind(PyObject *self, PyObject *args) { PyUnicodeObject *substring; Py_ssize_t start; @@ -8564,14 +11749,22 @@ unicode_rfind(PyUnicodeObject *self, PyObject *args) &start, &end)) return NULL; - result = stringlib_rfind_slice( - PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), - PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), - start, end + if (PyUnicode_READY(self) == -1) + return NULL; + if (PyUnicode_READY(substring) == -1) + return NULL; + + result = any_find_slice( + asciilib_rfind_slice, ucs1lib_rfind_slice, + ucs2lib_rfind_slice, ucs4lib_rfind_slice, + self, (PyObject*)substring, start, end ); Py_DECREF(substring); + if (result == -2) + return NULL; + return PyLong_FromSsize_t(result); } @@ -8581,7 +11774,7 @@ PyDoc_STRVAR(rindex__doc__, Like S.rfind() but raise ValueError when the substring is not found."); static PyObject * -unicode_rindex(PyUnicodeObject *self, PyObject *args) +unicode_rindex(PyObject *self, PyObject *args) { PyUnicodeObject *substring; Py_ssize_t start; @@ -8592,18 +11785,27 @@ unicode_rindex(PyUnicodeObject *self, PyObject *args) &start, &end)) return NULL; - result = stringlib_rfind_slice( - PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), - PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), - start, end + if (PyUnicode_READY(self) == -1) + return NULL; + if (PyUnicode_READY(substring) == -1) + return NULL; + + result = any_find_slice( + asciilib_rfind_slice, ucs1lib_rfind_slice, + ucs2lib_rfind_slice, ucs4lib_rfind_slice, + self, (PyObject*)substring, start, end ); Py_DECREF(substring); + if (result == -2) + return NULL; + if (result < 0) { PyErr_SetString(PyExc_ValueError, "substring not found"); return NULL; } + return PyLong_FromSsize_t(result); } @@ -8614,25 +11816,27 @@ Return S right-justified in a string of length width. Padding is\n\ done using the specified fill character (default is a space)."); static PyObject * -unicode_rjust(PyUnicodeObject *self, PyObject *args) +unicode_rjust(PyObject *self, PyObject *args) { Py_ssize_t width; - Py_UNICODE fillchar = ' '; + Py_UCS4 fillchar = ' '; if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) return NULL; - if (self->length >= width && PyUnicode_CheckExact(self)) { + if (PyUnicode_READY(self) == -1) + return NULL; + + if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } - return (PyObject*) pad(self, width - self->length, 0, fillchar); + return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); } -PyObject *PyUnicode_Split(PyObject *s, - PyObject *sep, - Py_ssize_t maxsplit) +PyObject * +PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) { PyObject *result; @@ -8647,7 +11851,7 @@ PyObject *PyUnicode_Split(PyObject *s, } } - result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); + result = split(s, sep, maxsplit); Py_DECREF(s); Py_XDECREF(sep); @@ -8664,7 +11868,7 @@ whitespace string is a separator and empty strings are\n\ removed from the result."); static PyObject* -unicode_split(PyUnicodeObject *self, PyObject *args) +unicode_split(PyObject *self, PyObject *args) { PyObject *substring = Py_None; Py_ssize_t maxcount = -1; @@ -8675,7 +11879,7 @@ unicode_split(PyUnicodeObject *self, PyObject *args) if (substring == Py_None) return split(self, NULL, maxcount); else if (PyUnicode_Check(substring)) - return split(self, (PyUnicodeObject *)substring, maxcount); + return split(self, substring, maxcount); else return PyUnicode_Split((PyObject *)self, substring, maxcount); } @@ -8686,25 +11890,69 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) PyObject* str_obj; PyObject* sep_obj; PyObject* out; + int kind1, kind2, kind; + void *buf1 = NULL, *buf2 = NULL; + Py_ssize_t len1, len2; str_obj = PyUnicode_FromObject(str_in); - if (!str_obj) + if (!str_obj || PyUnicode_READY(str_obj) == -1) return NULL; sep_obj = PyUnicode_FromObject(sep_in); - if (!sep_obj) { + if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { Py_DECREF(str_obj); return NULL; } - out = stringlib_partition( - str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), - sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) - ); + kind1 = PyUnicode_KIND(str_obj); + kind2 = PyUnicode_KIND(sep_obj); + kind = Py_MAX(kind1, kind2); + buf1 = PyUnicode_DATA(str_obj); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind(str_obj, kind); + if (!buf1) + goto onError; + buf2 = PyUnicode_DATA(sep_obj); + if (kind2 != kind) + buf2 = _PyUnicode_AsKind(sep_obj, kind); + if (!buf2) + goto onError; + len1 = PyUnicode_GET_LENGTH(str_obj); + len2 = PyUnicode_GET_LENGTH(sep_obj); + + switch(PyUnicode_KIND(str_obj)) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) + out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); + else + out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); + break; + case PyUnicode_2BYTE_KIND: + out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); + break; + case PyUnicode_4BYTE_KIND: + out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); + break; + default: + assert(0); + out = 0; + } Py_DECREF(sep_obj); Py_DECREF(str_obj); + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); return out; + onError: + Py_DECREF(sep_obj); + Py_DECREF(str_obj); + if (kind1 != kind && buf1) + PyMem_Free(buf1); + if (kind2 != kind && buf2) + PyMem_Free(buf2); + return NULL; } @@ -8714,6 +11962,9 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) PyObject* str_obj; PyObject* sep_obj; PyObject* out; + int kind1, kind2, kind; + void *buf1 = NULL, *buf2 = NULL; + Py_ssize_t len1, len2; str_obj = PyUnicode_FromObject(str_in); if (!str_obj) @@ -8724,15 +11975,56 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) return NULL; } - out = stringlib_rpartition( - str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), - sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) - ); + kind1 = PyUnicode_KIND(str_in); + kind2 = PyUnicode_KIND(sep_obj); + kind = Py_MAX(kind1, kind2); + buf1 = PyUnicode_DATA(str_in); + if (kind1 != kind) + buf1 = _PyUnicode_AsKind(str_in, kind); + if (!buf1) + goto onError; + buf2 = PyUnicode_DATA(sep_obj); + if (kind2 != kind) + buf2 = _PyUnicode_AsKind(sep_obj, kind); + if (!buf2) + goto onError; + len1 = PyUnicode_GET_LENGTH(str_obj); + len2 = PyUnicode_GET_LENGTH(sep_obj); + + switch(PyUnicode_KIND(str_in)) { + case PyUnicode_1BYTE_KIND: + if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) + out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); + else + out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); + break; + case PyUnicode_2BYTE_KIND: + out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); + break; + case PyUnicode_4BYTE_KIND: + out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); + break; + default: + assert(0); + out = 0; + } Py_DECREF(sep_obj); Py_DECREF(str_obj); + if (kind1 != kind) + PyMem_Free(buf1); + if (kind2 != kind) + PyMem_Free(buf2); return out; + onError: + Py_DECREF(sep_obj); + Py_DECREF(str_obj); + if (kind1 != kind && buf1) + PyMem_Free(buf1); + if (kind2 != kind && buf2) + PyMem_Free(buf2); + return NULL; } PyDoc_STRVAR(partition__doc__, @@ -8743,9 +12035,9 @@ the separator itself, and the part after it. If the separator is not\n\ found, return S and two empty strings."); static PyObject* -unicode_partition(PyUnicodeObject *self, PyObject *separator) +unicode_partition(PyObject *self, PyObject *separator) { - return PyUnicode_Partition((PyObject *)self, separator); + return PyUnicode_Partition(self, separator); } PyDoc_STRVAR(rpartition__doc__, @@ -8756,14 +12048,13 @@ the part before it, the separator itself, and the part after it. If the\n\ separator is not found, return two empty strings and S."); static PyObject* -unicode_rpartition(PyUnicodeObject *self, PyObject *separator) +unicode_rpartition(PyObject *self, PyObject *separator) { - return PyUnicode_RPartition((PyObject *)self, separator); + return PyUnicode_RPartition(self, separator); } -PyObject *PyUnicode_RSplit(PyObject *s, - PyObject *sep, - Py_ssize_t maxsplit) +PyObject * +PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) { PyObject *result; @@ -8778,7 +12069,7 @@ PyObject *PyUnicode_RSplit(PyObject *s, } } - result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); + result = rsplit(s, sep, maxsplit); Py_DECREF(s); Py_XDECREF(sep); @@ -8795,7 +12086,7 @@ splits are done. If sep is not specified, any whitespace string\n\ is a separator."); static PyObject* -unicode_rsplit(PyUnicodeObject *self, PyObject *args) +unicode_rsplit(PyObject *self, PyObject *args) { PyObject *substring = Py_None; Py_ssize_t maxcount = -1; @@ -8806,9 +12097,9 @@ unicode_rsplit(PyUnicodeObject *self, PyObject *args) if (substring == Py_None) return rsplit(self, NULL, maxcount); else if (PyUnicode_Check(substring)) - return rsplit(self, (PyUnicodeObject *)substring, maxcount); + return rsplit(self, substring, maxcount); else - return PyUnicode_RSplit((PyObject *)self, substring, maxcount); + return PyUnicode_RSplit(self, substring, maxcount); } PyDoc_STRVAR(splitlines__doc__, @@ -8819,11 +12110,13 @@ Line breaks are not included in the resulting list unless keepends\n\ is given and true."); static PyObject* -unicode_splitlines(PyUnicodeObject *self, PyObject *args) +unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"keepends", 0}; int keepends = 0; - if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", + kwlist, &keepends)) return NULL; return PyUnicode_Splitlines((PyObject *)self, keepends); @@ -8837,8 +12130,7 @@ PyObject *unicode_str(PyObject *self) return self; } else /* Subtype -- return genuine unicode string with the same value. */ - return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), - PyUnicode_GET_SIZE(self)); + return PyUnicode_Copy(self); } PyDoc_STRVAR(swapcase__doc__, @@ -8848,7 +12140,7 @@ Return a copy of S with uppercase characters converted to lowercase\n\ and vice versa."); static PyObject* -unicode_swapcase(PyUnicodeObject *self) +unicode_swapcase(PyObject *self) { return fixup(self, fixswapcase); } @@ -8879,22 +12171,28 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args) if (!new) return NULL; if (y != NULL) { + int x_kind, y_kind, z_kind; + void *x_data, *y_data, *z_data; + /* x must be a string too, of equal length */ - Py_ssize_t ylen = PyUnicode_GET_SIZE(y); if (!PyUnicode_Check(x)) { PyErr_SetString(PyExc_TypeError, "first maketrans argument must " "be a string if there is a second argument"); goto err; } - if (PyUnicode_GET_SIZE(x) != ylen) { + if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { PyErr_SetString(PyExc_ValueError, "the first two maketrans " "arguments must have equal length"); goto err; } /* create entries for translating chars in x to those in y */ - for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { - key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); - value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); + x_kind = PyUnicode_KIND(x); + y_kind = PyUnicode_KIND(y); + x_data = PyUnicode_DATA(x); + y_data = PyUnicode_DATA(y); + for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { + key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); + value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); if (!key || !value) goto err; res = PyDict_SetItem(new, key, value); @@ -8905,8 +12203,10 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args) } /* create entries for deleting chars in z */ if (z != NULL) { + z_kind = PyUnicode_KIND(z); + z_data = PyUnicode_DATA(z); for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { - key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); + key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); if (!key) goto err; res = PyDict_SetItem(new, key, Py_None); @@ -8916,6 +12216,9 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args) } } } else { + int kind; + void *data; + /* x must be a dict */ if (!PyDict_CheckExact(x)) { PyErr_SetString(PyExc_TypeError, "if you give only one argument " @@ -8932,7 +12235,9 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args) "table must be of length 1"); goto err; } - newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); + kind = PyUnicode_KIND(key); + data = PyUnicode_DATA(key); + newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); if (!newkey) goto err; res = PyDict_SetItem(new, newkey, value); @@ -8966,9 +12271,9 @@ Unmapped characters are left untouched. Characters mapped to None\n\ are deleted."); static PyObject* -unicode_translate(PyUnicodeObject *self, PyObject *table) +unicode_translate(PyObject *self, PyObject *table) { - return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); + return _PyUnicode_TranslateCharmap(self, table, "ignore"); } PyDoc_STRVAR(upper__doc__, @@ -8977,7 +12282,7 @@ PyDoc_STRVAR(upper__doc__, Return a copy of S converted to uppercase."); static PyObject* -unicode_upper(PyUnicodeObject *self) +unicode_upper(PyObject *self) { return fixup(self, fixupper); } @@ -8989,55 +12294,56 @@ Pad a numeric string S with zeros on the left, to fill a field\n\ of the specified width. The string S is never truncated."); static PyObject * -unicode_zfill(PyUnicodeObject *self, PyObject *args) +unicode_zfill(PyObject *self, PyObject *args) { Py_ssize_t fill; - PyUnicodeObject *u; - + PyObject *u; Py_ssize_t width; + int kind; + void *data; + Py_UCS4 chr; + + if (PyUnicode_READY(self) == -1) + return NULL; + if (!PyArg_ParseTuple(args, "n:zfill", &width)) return NULL; - if (self->length >= width) { + if (PyUnicode_GET_LENGTH(self) >= width) { if (PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject*) self; } else - return PyUnicode_FromUnicode( - PyUnicode_AS_UNICODE(self), - PyUnicode_GET_SIZE(self) - ); + return PyUnicode_Copy((PyObject*)self); } - fill = width - self->length; + fill = width - _PyUnicode_LENGTH(self); u = pad(self, fill, 0, '0'); if (u == NULL) return NULL; - if (u->str[fill] == '+' || u->str[fill] == '-') { + kind = PyUnicode_KIND(u); + data = PyUnicode_DATA(u); + chr = PyUnicode_READ(kind, data, fill); + + if (chr == '+' || chr == '-') { /* move sign to beginning of string */ - u->str[0] = u->str[fill]; - u->str[fill] = '0'; + PyUnicode_WRITE(kind, data, 0, chr); + PyUnicode_WRITE(kind, data, fill, '0'); } + assert(_PyUnicode_CheckConsistency(u, 1)); return (PyObject*) u; } #if 0 -static PyObject* -unicode_freelistsize(PyUnicodeObject *self) -{ - return PyLong_FromLong(numfree); -} - static PyObject * unicode__decimal2ascii(PyObject *self) { - return PyUnicode_TransformDecimalToASCII(PyUnicode_AS_UNICODE(self), - PyUnicode_GET_SIZE(self)); + return PyUnicode_TransformDecimalAndSpaceToASCII(self); } #endif @@ -9137,7 +12443,7 @@ unicode_endswith(PyUnicodeObject *self, return PyBool_FromLong(result); } -#include "stringlib/string_format.h" +#include "stringlib/unicode_format.h" PyDoc_STRVAR(format__doc__, "S.format(*args, **kwargs) -> str\n\ @@ -9154,14 +12460,14 @@ The substitutions are identified by braces ('{' and '}')."); static PyObject * unicode__format__(PyObject* self, PyObject* args) { - PyObject *format_spec; + PyObject *format_spec, *out; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - return _PyUnicode_FormatAdvanced(self, - PyUnicode_AS_UNICODE(format_spec), - PyUnicode_GET_SIZE(format_spec)); + out = _PyUnicode_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); + return out; } PyDoc_STRVAR(p_format__doc__, @@ -9172,17 +12478,43 @@ Return a formatted version of S as described by format_spec."); static PyObject * unicode__sizeof__(PyUnicodeObject *v) { - return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + - sizeof(Py_UNICODE) * (v->length + 1)); + Py_ssize_t size; + + /* If it's a compact object, account for base structure + + character data. */ + if (PyUnicode_IS_COMPACT_ASCII(v)) + size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; + else if (PyUnicode_IS_COMPACT(v)) + size = sizeof(PyCompactUnicodeObject) + + (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); + else { + /* If it is a two-block object, account for base object, and + for character block if present. */ + size = sizeof(PyUnicodeObject); + if (_PyUnicode_DATA_ANY(v)) + size += (PyUnicode_GET_LENGTH(v) + 1) * + PyUnicode_KIND(v); + } + /* If the wstr pointer is present, account for it unless it is shared + with the data pointer. Check if the data is not shared. */ + if (_PyUnicode_HAS_WSTR_MEMORY(v)) + size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); + if (_PyUnicode_HAS_UTF8_MEMORY(v)) + size += PyUnicode_UTF8_LENGTH(v) + 1; + + return PyLong_FromSsize_t(size); } PyDoc_STRVAR(sizeof__doc__, "S.__sizeof__() -> size of S in memory, in bytes"); static PyObject * -unicode_getnewargs(PyUnicodeObject *v) +unicode_getnewargs(PyObject *v) { - return Py_BuildValue("(u#)", v->str, v->length); + PyObject *copy = PyUnicode_Copy(v); + if (!copy) + return NULL; + return Py_BuildValue("(N)", copy); } static PyMethodDef unicode_methods[] = { @@ -9211,7 +12543,7 @@ static PyMethodDef unicode_methods[] = { {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, - {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, + {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, @@ -9242,7 +12574,6 @@ static PyMethodDef unicode_methods[] = { #if 0 /* These methods are just used for debugging the implementation. */ - {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, #endif @@ -9253,10 +12584,8 @@ static PyMethodDef unicode_methods[] = { static PyObject * unicode_mod(PyObject *v, PyObject *w) { - if (!PyUnicode_Check(v)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PyUnicode_Check(v)) + Py_RETURN_NOTIMPLEMENTED; return PyUnicode_Format(v, w); } @@ -9281,48 +12610,64 @@ static PySequenceMethods unicode_as_sequence = { static PyObject* unicode_subscript(PyUnicodeObject* self, PyObject* item) { + if (PyUnicode_READY(self) == -1) + return NULL; + if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) - i += PyUnicode_GET_SIZE(self); - return unicode_getitem(self, i); + i += PyUnicode_GET_LENGTH(self); + return unicode_getitem((PyObject*)self, i); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength, cur, i; - Py_UNICODE* source_buf; - Py_UNICODE* result_buf; - PyObject* result; + PyObject *result; + void *src_data, *dest_data; + int src_kind, dest_kind; + Py_UCS4 ch, max_char, kind_limit; - if (PySlice_GetIndicesEx(item, PyUnicode_GET_SIZE(self), + if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), &start, &stop, &step, &slicelength) < 0) { return NULL; } if (slicelength <= 0) { - return PyUnicode_FromUnicode(NULL, 0); - } else if (start == 0 && step == 1 && slicelength == self->length && + return PyUnicode_New(0, 0); + } else if (start == 0 && step == 1 && + slicelength == PyUnicode_GET_LENGTH(self) && PyUnicode_CheckExact(self)) { Py_INCREF(self); return (PyObject *)self; } else if (step == 1) { - return PyUnicode_FromUnicode(self->str + start, slicelength); - } else { - source_buf = PyUnicode_AS_UNICODE((PyObject*)self); - result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* - sizeof(Py_UNICODE)); - - if (result_buf == NULL) - return PyErr_NoMemory(); - - for (cur = start, i = 0; i < slicelength; cur += step, i++) { - result_buf[i] = source_buf[cur]; + return PyUnicode_Substring((PyObject*)self, + start, start + slicelength); + } + /* General case */ + max_char = 0; + src_kind = PyUnicode_KIND(self); + kind_limit = kind_maxchar_limit(src_kind); + src_data = PyUnicode_DATA(self); + for (cur = start, i = 0; i < slicelength; cur += step, i++) { + ch = PyUnicode_READ(src_kind, src_data, cur); + if (ch > max_char) { + max_char = ch; + if (max_char >= kind_limit) + break; } + } + result = PyUnicode_New(slicelength, max_char); + if (result == NULL) + return NULL; + dest_kind = PyUnicode_KIND(result); + dest_data = PyUnicode_DATA(result); - result = PyUnicode_FromUnicode(result_buf, slicelength); - PyObject_FREE(result_buf); - return result; + for (cur = start, i = 0; i < slicelength; cur += step, i++) { + Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); + PyUnicode_WRITE(dest_kind, dest_data, i, ch); } + assert(_PyUnicode_CheckConsistency(result, 1)); + return result; } else { PyErr_SetString(PyExc_TypeError, "string indices must be integers"); return NULL; @@ -9374,7 +12719,7 @@ formatfloat(PyObject *v, int flags, int prec, int type) (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); if (p == NULL) return NULL; - result = PyUnicode_FromStringAndSize(p, strlen(p)); + result = PyUnicode_DecodeASCII(p, strlen(p), NULL); PyMem_Free(p); return result; } @@ -9390,37 +12735,19 @@ formatlong(PyObject *val, int flags, int prec, int type) str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); if (!str) return NULL; - result = PyUnicode_FromStringAndSize(buf, len); + result = PyUnicode_DecodeASCII(buf, len, NULL); Py_DECREF(str); return result; } -static int -formatchar(Py_UNICODE *buf, - size_t buflen, - PyObject *v) +static Py_UCS4 +formatchar(PyObject *v) { /* presume that the buffer is at least 3 characters long */ if (PyUnicode_Check(v)) { - if (PyUnicode_GET_SIZE(v) == 1) { - buf[0] = PyUnicode_AS_UNICODE(v)[0]; - buf[1] = '\0'; - return 1; + if (PyUnicode_GET_LENGTH(v) == 1) { + return PyUnicode_READ_CHAR(v, 0); } -#ifndef Py_UNICODE_WIDE - if (PyUnicode_GET_SIZE(v) == 2) { - /* Decode a valid surrogate pair */ - int c0 = PyUnicode_AS_UNICODE(v)[0]; - int c1 = PyUnicode_AS_UNICODE(v)[1]; - if (0xD800 <= c0 && c0 <= 0xDBFF && - 0xDC00 <= c1 && c1 <= 0xDFFF) { - buf[0] = c0; - buf[1] = c1; - buf[2] = '\0'; - return 2; - } - } -#endif goto onError; } else { @@ -9433,58 +12760,82 @@ formatchar(Py_UNICODE *buf, if (x < 0 || x > 0x10ffff) { PyErr_SetString(PyExc_OverflowError, "%c arg not in range(0x110000)"); - return -1; + return (Py_UCS4) -1; } -#ifndef Py_UNICODE_WIDE - if (x > 0xffff) { - x -= 0x10000; - buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); - buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); - return 2; - } -#endif - buf[0] = (Py_UNICODE) x; - buf[1] = '\0'; - return 1; + return (Py_UCS4) x; } onError: PyErr_SetString(PyExc_TypeError, "%c requires int or char"); - return -1; + return (Py_UCS4) -1; } -/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) - FORMATBUFLEN is the length of the buffer in which chars are formatted. -*/ -#define FORMATBUFLEN (size_t)10 +static int +repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) +{ + int r; + assert(count > 0); + assert(PyUnicode_Check(obj)); + if (count > 5) { + PyObject *repeated = unicode_repeat((PyUnicodeObject *) obj, count); + if (repeated == NULL) + return -1; + r = _PyAccu_Accumulate(acc, repeated); + Py_DECREF(repeated); + return r; + } + else { + do { + if (_PyAccu_Accumulate(acc, obj)) + return -1; + } while (--count); + return 0; + } +} -PyObject *PyUnicode_Format(PyObject *format, - PyObject *args) +PyObject * +PyUnicode_Format(PyObject *format, PyObject *args) { - Py_UNICODE *fmt, *res; - Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; + void *fmt; + int fmtkind; + PyObject *result; + int kind; + int r; + Py_ssize_t fmtcnt, fmtpos, arglen, argidx; int args_owned = 0; - PyUnicodeObject *result = NULL; PyObject *dict = NULL; - PyObject *uformat; + PyObject *temp = NULL; + PyObject *second = NULL; + PyUnicodeObject *uformat; + _PyAccu acc; + static PyObject *plus, *minus, *blank, *zero, *percent; + + if (!plus && !(plus = get_latin1_char('+'))) + return NULL; + if (!minus && !(minus = get_latin1_char('-'))) + return NULL; + if (!blank && !(blank = get_latin1_char(' '))) + return NULL; + if (!zero && !(zero = get_latin1_char('0'))) + return NULL; + if (!percent && !(percent = get_latin1_char('%'))) + return NULL; if (format == NULL || args == NULL) { PyErr_BadInternalCall(); return NULL; } - uformat = PyUnicode_FromObject(format); - if (uformat == NULL) + uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); + if (uformat == NULL || PyUnicode_READY(uformat) == -1) return NULL; - fmt = PyUnicode_AS_UNICODE(uformat); - fmtcnt = PyUnicode_GET_SIZE(uformat); - - reslen = rescnt = fmtcnt + 100; - result = _PyUnicode_New(reslen); - if (result == NULL) + if (_PyAccu_Init(&acc)) goto onError; - res = PyUnicode_AS_UNICODE(result); + fmt = PyUnicode_DATA(uformat); + fmtkind = PyUnicode_KIND(uformat); + fmtcnt = PyUnicode_GET_LENGTH(uformat); + fmtpos = 0; if (PyTuple_Check(args)) { arglen = PyTuple_Size(args); @@ -9499,35 +12850,39 @@ PyObject *PyUnicode_Format(PyObject *format, dict = args; while (--fmtcnt >= 0) { - if (*fmt != '%') { - if (--rescnt < 0) { - rescnt = fmtcnt + 100; - reslen += rescnt; - if (_PyUnicode_Resize(&result, reslen) < 0) - goto onError; - res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; - --rescnt; - } - *res++ = *fmt++; + if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { + PyObject *nonfmt; + Py_ssize_t nonfmtpos; + nonfmtpos = fmtpos++; + while (fmtcnt >= 0 && + PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { + fmtpos++; + fmtcnt--; + } + nonfmt = PyUnicode_Substring((PyObject *) uformat, nonfmtpos, fmtpos); + if (nonfmt == NULL) + goto onError; + r = _PyAccu_Accumulate(&acc, nonfmt); + Py_DECREF(nonfmt); + if (r) + goto onError; } else { /* Got a format specifier */ int flags = 0; Py_ssize_t width = -1; int prec = -1; - Py_UNICODE c = '\0'; - Py_UNICODE fill; + Py_UCS4 c = '\0'; + Py_UCS4 fill, sign; int isnumok; PyObject *v = NULL; - PyObject *temp = NULL; - Py_UNICODE *pbuf; - Py_UNICODE sign; - Py_ssize_t len; - Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ - - fmt++; - if (*fmt == '(') { - Py_UNICODE *keystart; + void *pbuf = NULL; + Py_ssize_t pindex, len; + PyObject *signobj = NULL, *fillobj = NULL; + + fmtpos++; + if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { + Py_ssize_t keystart; Py_ssize_t keylen; PyObject *key; int pcount = 1; @@ -9537,34 +12892,25 @@ PyObject *PyUnicode_Format(PyObject *format, "format requires a mapping"); goto onError; } - ++fmt; + ++fmtpos; --fmtcnt; - keystart = fmt; + keystart = fmtpos; /* Skip over balanced parentheses */ while (pcount > 0 && --fmtcnt >= 0) { - if (*fmt == ')') + if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') --pcount; - else if (*fmt == '(') + else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') ++pcount; - fmt++; + fmtpos++; } - keylen = fmt - keystart - 1; + keylen = fmtpos - keystart - 1; if (fmtcnt < 0 || pcount > 0) { PyErr_SetString(PyExc_ValueError, "incomplete format key"); goto onError; } -#if 0 - /* keys are converted to strings using UTF-8 and - then looked up since Python uses strings to hold - variables names etc. in its namespaces and we - wouldn't want to break common idioms. */ - key = PyUnicode_EncodeUTF8(keystart, - keylen, - NULL); -#else - key = PyUnicode_FromUnicode(keystart, keylen); -#endif + key = PyUnicode_Substring((PyObject*)uformat, + keystart, keystart + keylen); if (key == NULL) goto onError; if (args_owned) { @@ -9581,7 +12927,7 @@ PyObject *PyUnicode_Format(PyObject *format, argidx = -2; } while (--fmtcnt >= 0) { - switch (c = *fmt++) { + switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { case '-': flags |= F_LJUST; continue; case '+': flags |= F_SIGN; continue; case ' ': flags |= F_BLANK; continue; @@ -9607,12 +12953,12 @@ PyObject *PyUnicode_Format(PyObject *format, width = -width; } if (--fmtcnt >= 0) - c = *fmt++; + c = PyUnicode_READ(fmtkind, fmt, fmtpos++); } else if (c >= '0' && c <= '9') { width = c - '0'; while (--fmtcnt >= 0) { - c = *fmt++; + c = PyUnicode_READ(fmtkind, fmt, fmtpos++); if (c < '0' || c > '9') break; if ((width*10) / 10 != width) { @@ -9626,7 +12972,7 @@ PyObject *PyUnicode_Format(PyObject *format, if (c == '.') { prec = 0; if (--fmtcnt >= 0) - c = *fmt++; + c = PyUnicode_READ(fmtkind, fmt, fmtpos++); if (c == '*') { v = getnextarg(args, arglen, &argidx); if (v == NULL) @@ -9642,12 +12988,12 @@ PyObject *PyUnicode_Format(PyObject *format, if (prec < 0) prec = 0; if (--fmtcnt >= 0) - c = *fmt++; + c = PyUnicode_READ(fmtkind, fmt, fmtpos++); } else if (c >= '0' && c <= '9') { prec = c - '0'; while (--fmtcnt >= 0) { - c = *fmt++; + c = PyUnicode_READ(fmtkind, fmt, fmtpos++); if (c < '0' || c > '9') break; if ((prec*10) / 10 != prec) { @@ -9662,7 +13008,7 @@ PyObject *PyUnicode_Format(PyObject *format, if (fmtcnt >= 0) { if (c == 'h' || c == 'l' || c == 'L') { if (--fmtcnt >= 0) - c = *fmt++; + c = PyUnicode_READ(fmtkind, fmt, fmtpos++); } } if (fmtcnt < 0) { @@ -9677,14 +13023,12 @@ PyObject *PyUnicode_Format(PyObject *format, } sign = 0; fill = ' '; + fillobj = blank; switch (c) { case '%': - pbuf = formatbuf; - /* presume that buffer length is at least 1 */ - pbuf[0] = '%'; - len = 1; - break; + _PyAccu_Accumulate(&acc, percent); + continue; case 's': case 'r': @@ -9711,8 +13055,13 @@ PyObject *PyUnicode_Format(PyObject *format, goto onError; } } - pbuf = PyUnicode_AS_UNICODE(temp); - len = PyUnicode_GET_SIZE(temp); + if (PyUnicode_READY(temp) == -1) { + Py_CLEAR(temp); + goto onError; + } + pbuf = PyUnicode_DATA(temp); + kind = PyUnicode_KIND(temp); + len = PyUnicode_GET_LENGTH(temp); if (prec >= 0 && len > prec) len = prec; break; @@ -9741,8 +13090,13 @@ PyObject *PyUnicode_Format(PyObject *format, Py_DECREF(iobj); if (!temp) goto onError; - pbuf = PyUnicode_AS_UNICODE(temp); - len = PyUnicode_GET_SIZE(temp); + if (PyUnicode_READY(temp) == -1) { + Py_CLEAR(temp); + goto onError; + } + pbuf = PyUnicode_DATA(temp); + kind = PyUnicode_KIND(temp); + len = PyUnicode_GET_LENGTH(temp); sign = 1; } else { @@ -9756,8 +13110,10 @@ PyObject *PyUnicode_Format(PyObject *format, "not %.200s", (char)c, Py_TYPE(v)->tp_name); goto onError; } - if (flags & F_ZERO) + if (flags & F_ZERO) { fill = '0'; + fillobj = zero; + } break; case 'e': @@ -9769,19 +13125,33 @@ PyObject *PyUnicode_Format(PyObject *format, temp = formatfloat(v, flags, prec, c); if (!temp) goto onError; - pbuf = PyUnicode_AS_UNICODE(temp); - len = PyUnicode_GET_SIZE(temp); + if (PyUnicode_READY(temp) == -1) { + Py_CLEAR(temp); + goto onError; + } + pbuf = PyUnicode_DATA(temp); + kind = PyUnicode_KIND(temp); + len = PyUnicode_GET_LENGTH(temp); sign = 1; - if (flags & F_ZERO) + if (flags & F_ZERO) { fill = '0'; + fillobj = zero; + } break; case 'c': - pbuf = formatbuf; - len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); - if (len < 0) + { + Py_UCS4 ch = formatchar(v); + if (ch == (Py_UCS4) -1) + goto onError; + temp = _PyUnicode_FromUCS4(&ch, 1); + if (temp == NULL) goto onError; + pbuf = PyUnicode_DATA(temp); + kind = PyUnicode_KIND(temp); + len = PyUnicode_GET_LENGTH(temp); break; + } default: PyErr_Format(PyExc_ValueError, @@ -9789,90 +13159,108 @@ PyObject *PyUnicode_Format(PyObject *format, "at index %zd", (31<=c && c<=126) ? (char)c : '?', (int)c, - (Py_ssize_t)(fmt - 1 - - PyUnicode_AS_UNICODE(uformat))); + fmtpos - 1); goto onError; } + /* pbuf is initialized here. */ + pindex = 0; if (sign) { - if (*pbuf == '-' || *pbuf == '+') { - sign = *pbuf++; + if (PyUnicode_READ(kind, pbuf, pindex) == '-') { + signobj = minus; + len--; + pindex++; + } + else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { + signobj = plus; len--; + pindex++; } else if (flags & F_SIGN) - sign = '+'; + signobj = plus; else if (flags & F_BLANK) - sign = ' '; + signobj = blank; else sign = 0; } if (width < len) width = len; - if (rescnt - (sign != 0) < width) { - reslen -= rescnt; - rescnt = width + fmtcnt + 100; - reslen += rescnt; - if (reslen < 0) { - Py_XDECREF(temp); - PyErr_NoMemory(); - goto onError; - } - if (_PyUnicode_Resize(&result, reslen) < 0) { - Py_XDECREF(temp); - goto onError; - } - res = PyUnicode_AS_UNICODE(result) - + reslen - rescnt; - } if (sign) { - if (fill != ' ') - *res++ = sign; - rescnt--; + if (fill != ' ') { + assert(signobj != NULL); + if (_PyAccu_Accumulate(&acc, signobj)) + goto onError; + } if (width > len) width--; } if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { - assert(pbuf[0] == '0'); - assert(pbuf[1] == c); + assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); + assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); if (fill != ' ') { - *res++ = *pbuf++; - *res++ = *pbuf++; + second = get_latin1_char( + PyUnicode_READ(kind, pbuf, pindex + 1)); + pindex += 2; + if (second == NULL || + _PyAccu_Accumulate(&acc, zero) || + _PyAccu_Accumulate(&acc, second)) + goto onError; + Py_CLEAR(second); } - rescnt -= 2; width -= 2; if (width < 0) width = 0; len -= 2; } if (width > len && !(flags & F_LJUST)) { - do { - --rescnt; - *res++ = fill; - } while (--width > len); + assert(fillobj != NULL); + if (repeat_accumulate(&acc, fillobj, width - len)) + goto onError; + width = len; } if (fill == ' ') { - if (sign) - *res++ = sign; + if (sign) { + assert(signobj != NULL); + if (_PyAccu_Accumulate(&acc, signobj)) + goto onError; + } if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { - assert(pbuf[0] == '0'); - assert(pbuf[1] == c); - *res++ = *pbuf++; - *res++ = *pbuf++; + assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); + assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); + second = get_latin1_char( + PyUnicode_READ(kind, pbuf, pindex + 1)); + pindex += 2; + if (second == NULL || + _PyAccu_Accumulate(&acc, zero) || + _PyAccu_Accumulate(&acc, second)) + goto onError; + Py_CLEAR(second); } } - Py_UNICODE_COPY(res, pbuf, len); - res += len; - rescnt -= len; - while (--width >= len) { - --rescnt; - *res++ = ' '; + /* Copy all characters, preserving len */ + if (temp != NULL) { + assert(pbuf == PyUnicode_DATA(temp)); + v = PyUnicode_Substring(temp, pindex, pindex + len); + } + else { + const char *p = (const char *) pbuf; + assert(pbuf != NULL); + p += kind * pindex; + v = PyUnicode_FromKindAndData(kind, p, len); } + if (v == NULL) + goto onError; + r = _PyAccu_Accumulate(&acc, v); + Py_DECREF(v); + if (r) + goto onError; + if (width > len && repeat_accumulate(&acc, blank, width - len)) + goto onError; if (dict && (argidx < arglen) && c != '%') { PyErr_SetString(PyExc_TypeError, "not all arguments converted during string formatting"); - Py_XDECREF(temp); goto onError; } - Py_XDECREF(temp); + Py_CLEAR(temp); } /* '%' */ } /* until end */ if (argidx < arglen && !dict) { @@ -9881,17 +13269,20 @@ PyObject *PyUnicode_Format(PyObject *format, goto onError; } - if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) - goto onError; + result = _PyAccu_Finish(&acc); if (args_owned) { Py_DECREF(args); } Py_DECREF(uformat); + Py_XDECREF(temp); + Py_XDECREF(second); return (PyObject *)result; onError: - Py_XDECREF(result); Py_DECREF(uformat); + Py_XDECREF(temp); + Py_XDECREF(second); + _PyAccu_Destroy(&acc); if (args_owned) { Py_DECREF(args); } @@ -9915,7 +13306,7 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) kwlist, &x, &encoding, &errors)) return NULL; if (x == NULL) - return (PyObject *)_PyUnicode_New(0); + return (PyObject *)PyUnicode_New(0, 0); if (encoding == NULL && errors == NULL) return PyObject_Str(x); else @@ -9925,31 +13316,99 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject * unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyUnicodeObject *tmp, *pnew; - Py_ssize_t n; + PyUnicodeObject *unicode, *self; + Py_ssize_t length, char_size; + int share_wstr, share_utf8; + unsigned int kind; + void *data; assert(PyType_IsSubtype(type, &PyUnicode_Type)); - tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); - if (tmp == NULL) + + unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); + if (unicode == NULL) return NULL; - assert(PyUnicode_Check(tmp)); - pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); - if (pnew == NULL) { - Py_DECREF(tmp); + assert(_PyUnicode_CHECK(unicode)); + if (PyUnicode_READY(unicode)) + return NULL; + + self = (PyUnicodeObject *) type->tp_alloc(type, 0); + if (self == NULL) { + Py_DECREF(unicode); return NULL; } - pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); - if (pnew->str == NULL) { - _Py_ForgetReference((PyObject *)pnew); - PyObject_Del(pnew); - Py_DECREF(tmp); - return PyErr_NoMemory(); + kind = PyUnicode_KIND(unicode); + length = PyUnicode_GET_LENGTH(unicode); + + _PyUnicode_LENGTH(self) = length; +#ifdef Py_DEBUG + _PyUnicode_HASH(self) = -1; +#else + _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); +#endif + _PyUnicode_STATE(self).interned = 0; + _PyUnicode_STATE(self).kind = kind; + _PyUnicode_STATE(self).compact = 0; + _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; + _PyUnicode_STATE(self).ready = 1; + _PyUnicode_WSTR(self) = NULL; + _PyUnicode_UTF8_LENGTH(self) = 0; + _PyUnicode_UTF8(self) = NULL; + _PyUnicode_WSTR_LENGTH(self) = 0; + _PyUnicode_DATA_ANY(self) = NULL; + + share_utf8 = 0; + share_wstr = 0; + if (kind == PyUnicode_1BYTE_KIND) { + char_size = 1; + if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) + share_utf8 = 1; + } + else if (kind == PyUnicode_2BYTE_KIND) { + char_size = 2; + if (sizeof(wchar_t) == 2) + share_wstr = 1; + } + else { + assert(kind == PyUnicode_4BYTE_KIND); + char_size = 4; + if (sizeof(wchar_t) == 4) + share_wstr = 1; + } + + /* Ensure we won't overflow the length. */ + if (length > (PY_SSIZE_T_MAX / char_size - 1)) { + PyErr_NoMemory(); + goto onError; + } + data = PyObject_MALLOC((length + 1) * char_size); + if (data == NULL) { + PyErr_NoMemory(); + goto onError; + } + + _PyUnicode_DATA_ANY(self) = data; + if (share_utf8) { + _PyUnicode_UTF8_LENGTH(self) = length; + _PyUnicode_UTF8(self) = data; + } + if (share_wstr) { + _PyUnicode_WSTR_LENGTH(self) = length; + _PyUnicode_WSTR(self) = (wchar_t *)data; } - Py_UNICODE_COPY(pnew->str, tmp->str, n+1); - pnew->length = n; - pnew->hash = tmp->hash; - Py_DECREF(tmp); - return (PyObject *)pnew; + + Py_MEMCPY(data, PyUnicode_DATA(unicode), + kind * (length + 1)); + Py_DECREF(unicode); + assert(_PyUnicode_CheckConsistency(self, 1)); +#ifdef Py_DEBUG + _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); +#endif + return (PyObject *)self; + +onError: + Py_DECREF(unicode); + Py_DECREF(self); + return NULL; } PyDoc_STRVAR(unicode_doc, @@ -10012,7 +13471,7 @@ void _PyUnicode_Init(void) int i; /* XXX - move this array to unicodectype.c ? */ - Py_UNICODE linebreak[] = { + Py_UCS2 linebreak[] = { 0x000A, /* LINE FEED */ 0x000D, /* CARRIAGE RETURN */ 0x001C, /* FILE SEPARATOR */ @@ -10024,11 +13483,10 @@ void _PyUnicode_Init(void) }; /* Init the implementation */ - free_list = NULL; - numfree = 0; - unicode_empty = _PyUnicode_New(0); + unicode_empty = PyUnicode_New(0, 0); + assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); if (!unicode_empty) - return; + Py_FatalError("Can't create empty string"); for (i = 0; i < 256; i++) unicode_latin1[i] = NULL; @@ -10037,8 +13495,8 @@ void _PyUnicode_Init(void) /* initialize the linebreak bloom filter */ bloom_linebreak = make_bloom_mask( - linebreak, sizeof(linebreak) / sizeof(linebreak[0]) - ); + PyUnicode_2BYTE_KIND, linebreak, + Py_ARRAY_LENGTH(linebreak)); PyType_Ready(&EncodingMapType); } @@ -10048,21 +13506,7 @@ void _PyUnicode_Init(void) int PyUnicode_ClearFreeList(void) { - int freelist_size = numfree; - PyUnicodeObject *u; - - for (u = free_list; u != NULL;) { - PyUnicodeObject *v = u; - u = *(PyUnicodeObject **)u; - if (v->str) - PyObject_DEL(v->str); - Py_XDECREF(v->defenc); - PyObject_Del(v); - numfree--; - } - free_list = NULL; - assert(numfree == 0); - return freelist_size; + return 0; } void @@ -10087,15 +13531,24 @@ PyUnicode_InternInPlace(PyObject **p) { register PyUnicodeObject *s = (PyUnicodeObject *)(*p); PyObject *t; +#ifdef Py_DEBUG + assert(s != NULL); + assert(_PyUnicode_CHECK(s)); +#else if (s == NULL || !PyUnicode_Check(s)) - Py_FatalError( - "PyUnicode_InternInPlace: unicode strings only please!"); + return; +#endif /* If it's a subclass, we don't really know what putting it in the interned dict might do. */ if (!PyUnicode_CheckExact(s)) return; if (PyUnicode_CHECK_INTERNED(s)) return; + if (_PyUnicode_READY_REPLACE(p)) { + assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); + return; + } + s = (PyUnicodeObject *)(*p); if (interned == NULL) { interned = PyDict_New(); if (interned == NULL) { @@ -10127,15 +13580,17 @@ PyUnicode_InternInPlace(PyObject **p) /* The two references in interned are not counted by refcnt. The deallocator will take care of this */ Py_REFCNT(s) -= 2; - PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; + _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; } void PyUnicode_InternImmortal(PyObject **p) { + PyUnicodeObject *u = (PyUnicodeObject *)*p; + PyUnicode_InternInPlace(p); if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { - PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; + _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; Py_INCREF(*p); } } @@ -10150,7 +13605,8 @@ PyUnicode_InternFromString(const char *cp) return s; } -void _Py_ReleaseInternedUnicodeStrings(void) +void +_Py_ReleaseInternedUnicodeStrings(void) { PyObject *keys; PyUnicodeObject *s; @@ -10175,22 +13631,26 @@ void _Py_ReleaseInternedUnicodeStrings(void) n); for (i = 0; i < n; i++) { s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); - switch (s->state) { + if (PyUnicode_READY(s) == -1) { + assert(0 && "could not ready string"); + fprintf(stderr, "could not ready string\n"); + } + switch (PyUnicode_CHECK_INTERNED(s)) { case SSTATE_NOT_INTERNED: /* XXX Shouldn't happen */ break; case SSTATE_INTERNED_IMMORTAL: Py_REFCNT(s) += 1; - immortal_size += s->length; + immortal_size += PyUnicode_GET_LENGTH(s); break; case SSTATE_INTERNED_MORTAL: Py_REFCNT(s) += 2; - mortal_size += s->length; + mortal_size += PyUnicode_GET_LENGTH(s); break; default: Py_FatalError("Inconsistent interned string state."); } - s->state = SSTATE_NOT_INTERNED; + _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; } fprintf(stderr, "total size of all interned strings: " "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " @@ -10235,11 +13695,13 @@ unicodeiter_next(unicodeiterobject *it) seq = it->it_seq; if (seq == NULL) return NULL; - assert(PyUnicode_Check(seq)); + assert(_PyUnicode_CHECK(seq)); - if (it->it_index < PyUnicode_GET_SIZE(seq)) { - item = PyUnicode_FromUnicode( - PyUnicode_AS_UNICODE(seq)+it->it_index, 1); + if (it->it_index < PyUnicode_GET_LENGTH(seq)) { + int kind = PyUnicode_KIND(seq); + void *data = PyUnicode_DATA(seq); + Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); + item = PyUnicode_FromOrdinal(chr); if (item != NULL) ++it->it_index; return item; @@ -10309,6 +13771,8 @@ unicode_iter(PyObject *seq) PyErr_BadInternalCall(); return NULL; } + if (PyUnicode_READY(seq) == -1) + return NULL; it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); if (it == NULL) return NULL; @@ -10319,95 +13783,16 @@ unicode_iter(PyObject *seq) return (PyObject *)it; } -size_t -Py_UNICODE_strlen(const Py_UNICODE *u) -{ - int res = 0; - while(*u++) - res++; - return res; -} - -Py_UNICODE* -Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) -{ - Py_UNICODE *u = s1; - while ((*u++ = *s2++)); - return s1; -} - -Py_UNICODE* -Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) -{ - Py_UNICODE *u = s1; - while ((*u++ = *s2++)) - if (n-- == 0) - break; - return s1; -} - -Py_UNICODE* -Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) -{ - Py_UNICODE *u1 = s1; - u1 += Py_UNICODE_strlen(u1); - Py_UNICODE_strcpy(u1, s2); - return s1; -} - -int -Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) -{ - while (*s1 && *s2 && *s1 == *s2) - s1++, s2++; - if (*s1 && *s2) - return (*s1 < *s2) ? -1 : +1; - if (*s1) - return 1; - if (*s2) - return -1; - return 0; -} - -int -Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) -{ - register Py_UNICODE u1, u2; - for (; n != 0; n--) { - u1 = *s1; - u2 = *s2; - if (u1 != u2) - return (u1 < u2) ? -1 : +1; - if (u1 == '\0') - return 0; - s1++; - s2++; - } - return 0; -} - -Py_UNICODE* -Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) -{ - const Py_UNICODE *p; - for (p = s; *p; p++) - if (*p == c) - return (Py_UNICODE*)p; - return NULL; -} - -Py_UNICODE* -Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) -{ - const Py_UNICODE *p; - p = s + Py_UNICODE_strlen(s); - while (p != s) { - p--; - if (*p == c) - return (Py_UNICODE*)p; - } - return NULL; -} +#define UNIOP(x) Py_UNICODE_##x +#define UNIOP_t Py_UNICODE +#include "uniops.h" +#undef UNIOP +#undef UNIOP_t +#define UNIOP(x) Py_UCS4_##x +#define UNIOP_t Py_UCS4 +#include "uniops.h" +#undef UNIOP +#undef UNIOP_t Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *object) @@ -10416,6 +13801,10 @@ PyUnicode_AsUnicodeCopy(PyObject *object) Py_UNICODE *copy; Py_ssize_t size; + if (!PyUnicode_Check(unicode)) { + PyErr_BadArgument(); + return NULL; + } /* Ensure we won't overflow the size. */ if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { PyErr_NoMemory(); diff --git a/Objects/uniops.h b/Objects/uniops.h new file mode 100644 index 0000000..06a0b4e --- /dev/null +++ b/Objects/uniops.h @@ -0,0 +1,91 @@ + +size_t +UNIOP(strlen)(const UNIOP_t *u) +{ + int res = 0; + while(*u++) + res++; + return res; +} + +UNIOP_t* +UNIOP(strcpy)(UNIOP_t *s1, const UNIOP_t *s2) +{ + UNIOP_t *u = s1; + while ((*u++ = *s2++)); + return s1; +} + +UNIOP_t* +UNIOP(strncpy)(UNIOP_t *s1, const UNIOP_t *s2, size_t n) +{ + UNIOP_t *u = s1; + while ((*u++ = *s2++)) + if (n-- == 0) + break; + return s1; +} + +UNIOP_t* +UNIOP(strcat)(UNIOP_t *s1, const UNIOP_t *s2) +{ + UNIOP_t *u1 = s1; + u1 += UNIOP(strlen(u1)); + UNIOP(strcpy(u1, s2)); + return s1; +} + +int +UNIOP(strcmp)(const UNIOP_t *s1, const UNIOP_t *s2) +{ + while (*s1 && *s2 && *s1 == *s2) + s1++, s2++; + if (*s1 && *s2) + return (*s1 < *s2) ? -1 : +1; + if (*s1) + return 1; + if (*s2) + return -1; + return 0; +} + +int +UNIOP(strncmp)(const UNIOP_t *s1, const UNIOP_t *s2, size_t n) +{ + register UNIOP_t u1, u2; + for (; n != 0; n--) { + u1 = *s1; + u2 = *s2; + if (u1 != u2) + return (u1 < u2) ? -1 : +1; + if (u1 == '\0') + return 0; + s1++; + s2++; + } + return 0; +} + +UNIOP_t* +UNIOP(strchr)(const UNIOP_t *s, UNIOP_t c) +{ + const UNIOP_t *p; + for (p = s; *p; p++) + if (*p == c) + return (UNIOP_t*)p; + return NULL; +} + +UNIOP_t* +UNIOP(strrchr)(const UNIOP_t *s, UNIOP_t c) +{ + const UNIOP_t *p; + p = s + UNIOP(strlen)(s); + while (p != s) { + p--; + if (*p == c) + return (UNIOP_t*)p; + } + return NULL; +} + diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 13323cf..c99f6ba 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -156,28 +156,31 @@ weakref_hash(PyWeakReference *self) static PyObject * weakref_repr(PyWeakReference *self) { - char buffer[256]; - if (PyWeakref_GET_OBJECT(self) == Py_None) { - PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self); + PyObject *name, *repr; + + if (PyWeakref_GET_OBJECT(self) == Py_None) + return PyUnicode_FromFormat("<weakref at %p; dead>", self); + + name = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), "__name__"); + if (name == NULL || !PyUnicode_Check(name)) { + if (name == NULL) + PyErr_Clear(); + repr = PyUnicode_FromFormat( + "<weakref at %p; to '%s' at %p>", + self, + Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name, + PyWeakref_GET_OBJECT(self)); } else { - char *name = NULL; - PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), - "__name__"); - if (nameobj == NULL) - PyErr_Clear(); - else if (PyUnicode_Check(nameobj)) - name = _PyUnicode_AsString(nameobj); - PyOS_snprintf(buffer, sizeof(buffer), - name ? "<weakref at %p; to '%.50s' at %p (%s)>" - : "<weakref at %p; to '%.50s' at %p>", - self, - Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name, - PyWeakref_GET_OBJECT(self), - name); - Py_XDECREF(nameobj); + repr = PyUnicode_FromFormat( + "<weakref at %p; to '%s' at %p (%U)>", + self, + Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name, + PyWeakref_GET_OBJECT(self), + name); } - return PyUnicode_FromString(buffer); + Py_XDECREF(name); + return repr; } /* Weak references only support equality, not ordering. Two weak references @@ -190,8 +193,7 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op) if ((op != Py_EQ && op != Py_NE) || !PyWeakref_Check(self) || !PyWeakref_Check(other)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } if (PyWeakref_GET_OBJECT(self) == Py_None || PyWeakref_GET_OBJECT(other) == Py_None) { @@ -452,12 +454,11 @@ WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords) static PyObject * proxy_repr(PyWeakReference *proxy) { - char buf[160]; - PyOS_snprintf(buf, sizeof(buf), - "<weakproxy at %p to %.100s at %p>", proxy, - Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name, - PyWeakref_GET_OBJECT(proxy)); - return PyUnicode_FromString(buf); + return PyUnicode_FromFormat( + "<weakproxy at %p to %s at %p>", + proxy, + Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name, + PyWeakref_GET_OBJECT(proxy)); } |