diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-01-25 17:15:10 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-01-25 17:15:10 (GMT) |
commit | 78821ddf8c8eeebf757d72c2989cc0accea155de (patch) | |
tree | c9ce0cdf40089bbc995de6138b237595b4cd51dd /Objects/bytearrayobject.c | |
parent | e52c31450dfbe649b559b3fa96630d348b838c19 (diff) | |
download | cpython-78821ddf8c8eeebf757d72c2989cc0accea155de.zip cpython-78821ddf8c8eeebf757d72c2989cc0accea155de.tar.gz cpython-78821ddf8c8eeebf757d72c2989cc0accea155de.tar.bz2 |
fix building the core with --disable-unicode
I changed some bytearray methods to use strings instead of unicode like bytes_repr
Also, bytearray.fromhex() can take strings as well as unicode
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 97b0502..4b8d585 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -803,6 +803,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) return 0; } +#ifdef Py_USING_UNICODE if (PyUnicode_Check(arg)) { /* Encode via the codec registry */ PyObject *encoded, *new; @@ -822,6 +823,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) Py_DECREF(new); return 0; } +#endif /* If it's not unicode, there can't be encoding or errors */ if (encoding != NULL || errors != NULL) { @@ -929,14 +931,14 @@ bytes_repr(PyByteArrayObject *self) "bytearray object is too large to make repr"); return NULL; } - v = PyUnicode_FromUnicode(NULL, newsize); + v = PyString_FromStringAndSize(NULL, newsize); if (v == NULL) { return NULL; } else { register Py_ssize_t i; - register Py_UNICODE c; - register Py_UNICODE *p; + register char c; + register char *p; int quote; /* Figure out which quote to use; single is preferred */ @@ -956,7 +958,7 @@ bytes_repr(PyByteArrayObject *self) ; } - p = PyUnicode_AS_UNICODE(v); + p = PyString_AS_STRING(v); while (*quote_prefix) *p++ = *quote_prefix++; *p++ = quote; @@ -964,7 +966,7 @@ bytes_repr(PyByteArrayObject *self) 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); + assert(newsize - (p - PyString_AS_STRING(v)) >= 5); c = self->ob_bytes[i]; if (c == '\'' || c == '\\') *p++ = '\\', *p++ = c; @@ -985,13 +987,13 @@ bytes_repr(PyByteArrayObject *self) else *p++ = c; } - assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); + assert(newsize - (p - PyString_AS_STRING(v)) >= 1); *p++ = quote; while (*quote_postfix) { *p++ = *quote_postfix++; } *p = '\0'; - if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) { + if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) { Py_DECREF(v); return NULL; } @@ -1025,6 +1027,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op) /* Bytes can be compared to anything that supports the (binary) buffer API. Except that a comparison with Unicode is always an error, even if the comparison is for equality. */ +#ifdef Py_USING_UNICODE if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { if (Py_BytesWarningFlag && op == Py_EQ) { @@ -1036,6 +1039,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op) Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } +#endif self_size = _getbuffer(self, &self_bytes); if (self_size < 0) { @@ -2939,8 +2943,14 @@ bytes_decode(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) return NULL; - if (encoding == NULL) + if (encoding == NULL) { +#ifdef Py_USING_UNICODE encoding = PyUnicode_GetDefaultEncoding(); +#else + PyErr_SetString(PyExc_ValueError, "no encoding specified"); + return NULL; +#endif + } return PyCodec_Decode(self, encoding, errors); } @@ -3038,10 +3048,8 @@ 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(char c) { - if (c >= 128) - return -1; if (ISDIGIT(c)) return c - '0'; else { @@ -3056,17 +3064,14 @@ hex_digit_to_int(Py_UNICODE c) static PyObject * bytes_fromhex(PyObject *cls, PyObject *args) { - PyObject *newbytes, *hexobj; + PyObject *newbytes; char *buf; - Py_UNICODE *hex; + char *hex; Py_ssize_t hexlen, byteslen, i, j; int top, bot; - if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) + if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &hexlen)) return NULL; - assert(PyUnicode_Check(hexobj)); - hexlen = PyUnicode_GET_SIZE(hexobj); - hex = PyUnicode_AS_UNICODE(hexobj); byteslen = hexlen/2; /* This overestimates if there are spaces */ newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); if (!newbytes) @@ -3104,10 +3109,18 @@ bytes_reduce(PyByteArrayObject *self) { PyObject *latin1, *dict; if (self->ob_bytes) +#ifdef Py_USING_UNICODE latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, Py_SIZE(self), NULL); +#else + latin1 = PyString_FromStringAndSize(self->ob_bytes, Py_SIZE(self)) +#endif else +#ifdef Py_USING_UNICODE latin1 = PyUnicode_FromString(""); +#else + latin1 = PyString_FromString(""); +#endif dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); if (dict == NULL) { |