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
This commit is contained in:
parent
e52c31450d
commit
78821ddf8c
|
@ -250,7 +250,6 @@ class BaseBytesTest(unittest.TestCase):
|
||||||
self.assertEquals(self.type2test.fromhex(u'1a2B30'), b)
|
self.assertEquals(self.type2test.fromhex(u'1a2B30'), b)
|
||||||
self.assertEquals(self.type2test.fromhex(u' 1A 2B 30 '), b)
|
self.assertEquals(self.type2test.fromhex(u' 1A 2B 30 '), b)
|
||||||
self.assertEquals(self.type2test.fromhex(u'0000'), b'\0\0')
|
self.assertEquals(self.type2test.fromhex(u'0000'), b'\0\0')
|
||||||
self.assertRaises(TypeError, self.type2test.fromhex, b'1B')
|
|
||||||
self.assertRaises(ValueError, self.type2test.fromhex, u'a')
|
self.assertRaises(ValueError, self.type2test.fromhex, u'a')
|
||||||
self.assertRaises(ValueError, self.type2test.fromhex, u'rt')
|
self.assertRaises(ValueError, self.type2test.fromhex, u'rt')
|
||||||
self.assertRaises(ValueError, self.type2test.fromhex, u'1a b cd')
|
self.assertRaises(ValueError, self.type2test.fromhex, u'1a b cd')
|
||||||
|
|
|
@ -784,7 +784,9 @@ clear_freelists(void)
|
||||||
(void)PyFrame_ClearFreeList();
|
(void)PyFrame_ClearFreeList();
|
||||||
(void)PyCFunction_ClearFreeList();
|
(void)PyCFunction_ClearFreeList();
|
||||||
(void)PyTuple_ClearFreeList();
|
(void)PyTuple_ClearFreeList();
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
(void)PyUnicode_ClearFreeList();
|
(void)PyUnicode_ClearFreeList();
|
||||||
|
#endif
|
||||||
(void)PyInt_ClearFreeList();
|
(void)PyInt_ClearFreeList();
|
||||||
(void)PyFloat_ClearFreeList();
|
(void)PyFloat_ClearFreeList();
|
||||||
}
|
}
|
||||||
|
|
|
@ -720,8 +720,10 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
|
||||||
static PyObject * str__format__ = NULL;
|
static PyObject * str__format__ = NULL;
|
||||||
PyObject *empty = NULL;
|
PyObject *empty = NULL;
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
int spec_is_unicode;
|
int spec_is_unicode;
|
||||||
int result_is_unicode;
|
int result_is_unicode;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Initialize cached value */
|
/* Initialize cached value */
|
||||||
if (str__format__ == NULL) {
|
if (str__format__ == NULL) {
|
||||||
|
@ -738,11 +740,15 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check the format_spec type, and make sure it's str or unicode */
|
/* Check the format_spec type, and make sure it's str or unicode */
|
||||||
|
#if Py_USING_UNICODE
|
||||||
if (PyUnicode_Check(format_spec))
|
if (PyUnicode_Check(format_spec))
|
||||||
spec_is_unicode = 1;
|
spec_is_unicode = 1;
|
||||||
else if (PyString_Check(format_spec))
|
else if (PyString_Check(format_spec))
|
||||||
spec_is_unicode = 0;
|
spec_is_unicode = 0;
|
||||||
else {
|
else {
|
||||||
|
#else
|
||||||
|
if (!PyString_Check(format_spec)) {
|
||||||
|
#endif
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"format expects arg 2 to be string "
|
"format expects arg 2 to be string "
|
||||||
"or unicode, not %.100s", Py_TYPE(format_spec)->tp_name);
|
"or unicode, not %.100s", Py_TYPE(format_spec)->tp_name);
|
||||||
|
@ -773,9 +779,11 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
|
||||||
depending on the type of the format
|
depending on the type of the format
|
||||||
specifier). For new-style classes, this
|
specifier). For new-style classes, this
|
||||||
logic is done by object.__format__(). */
|
logic is done by object.__format__(). */
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (spec_is_unicode)
|
if (spec_is_unicode)
|
||||||
self_as_str = PyObject_Unicode(obj);
|
self_as_str = PyObject_Unicode(obj);
|
||||||
else
|
else
|
||||||
|
#endif
|
||||||
self_as_str = PyObject_Str(obj);
|
self_as_str = PyObject_Str(obj);
|
||||||
if (self_as_str == NULL)
|
if (self_as_str == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
|
@ -818,11 +826,15 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* Check the result type, and make sure it's str or unicode */
|
/* Check the result type, and make sure it's str or unicode */
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (PyUnicode_Check(result))
|
if (PyUnicode_Check(result))
|
||||||
result_is_unicode = 1;
|
result_is_unicode = 1;
|
||||||
else if (PyString_Check(result))
|
else if (PyString_Check(result))
|
||||||
result_is_unicode = 0;
|
result_is_unicode = 0;
|
||||||
else {
|
else {
|
||||||
|
#else
|
||||||
|
if (!PyString_Check(result)) {
|
||||||
|
#endif
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"%.100s.__format__ must return string or "
|
"%.100s.__format__ must return string or "
|
||||||
"unicode, not %.100s", Py_TYPE(obj)->tp_name,
|
"unicode, not %.100s", Py_TYPE(obj)->tp_name,
|
||||||
|
@ -834,12 +846,14 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
|
||||||
|
|
||||||
/* Convert to unicode, if needed. Required if spec is unicode
|
/* Convert to unicode, if needed. Required if spec is unicode
|
||||||
and result is str */
|
and result is str */
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (spec_is_unicode && !result_is_unicode) {
|
if (spec_is_unicode && !result_is_unicode) {
|
||||||
PyObject *tmp = PyObject_Unicode(result);
|
PyObject *tmp = PyObject_Unicode(result);
|
||||||
/* This logic works whether or not tmp is NULL */
|
/* This logic works whether or not tmp is NULL */
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
result = tmp;
|
result = tmp;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
done:
|
done:
|
||||||
Py_XDECREF(empty);
|
Py_XDECREF(empty);
|
||||||
|
|
|
@ -803,6 +803,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (PyUnicode_Check(arg)) {
|
if (PyUnicode_Check(arg)) {
|
||||||
/* Encode via the codec registry */
|
/* Encode via the codec registry */
|
||||||
PyObject *encoded, *new;
|
PyObject *encoded, *new;
|
||||||
|
@ -822,6 +823,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* If it's not unicode, there can't be encoding or errors */
|
/* If it's not unicode, there can't be encoding or errors */
|
||||||
if (encoding != NULL || errors != NULL) {
|
if (encoding != NULL || errors != NULL) {
|
||||||
|
@ -929,14 +931,14 @@ bytes_repr(PyByteArrayObject *self)
|
||||||
"bytearray object is too large to make repr");
|
"bytearray object is too large to make repr");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
v = PyUnicode_FromUnicode(NULL, newsize);
|
v = PyString_FromStringAndSize(NULL, newsize);
|
||||||
if (v == NULL) {
|
if (v == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
register Py_ssize_t i;
|
register Py_ssize_t i;
|
||||||
register Py_UNICODE c;
|
register char c;
|
||||||
register Py_UNICODE *p;
|
register char *p;
|
||||||
int quote;
|
int quote;
|
||||||
|
|
||||||
/* Figure out which quote to use; single is preferred */
|
/* 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)
|
while (*quote_prefix)
|
||||||
*p++ = *quote_prefix++;
|
*p++ = *quote_prefix++;
|
||||||
*p++ = quote;
|
*p++ = quote;
|
||||||
|
@ -964,7 +966,7 @@ bytes_repr(PyByteArrayObject *self)
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
/* There's at least enough room for a hex escape
|
/* There's at least enough room for a hex escape
|
||||||
and a closing quote. */
|
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];
|
c = self->ob_bytes[i];
|
||||||
if (c == '\'' || c == '\\')
|
if (c == '\'' || c == '\\')
|
||||||
*p++ = '\\', *p++ = c;
|
*p++ = '\\', *p++ = c;
|
||||||
|
@ -985,13 +987,13 @@ bytes_repr(PyByteArrayObject *self)
|
||||||
else
|
else
|
||||||
*p++ = c;
|
*p++ = c;
|
||||||
}
|
}
|
||||||
assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
|
assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
|
||||||
*p++ = quote;
|
*p++ = quote;
|
||||||
while (*quote_postfix) {
|
while (*quote_postfix) {
|
||||||
*p++ = *quote_postfix++;
|
*p++ = *quote_postfix++;
|
||||||
}
|
}
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
|
if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) {
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1025,6 +1027,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
/* Bytes can be compared to anything that supports the (binary)
|
/* Bytes can be compared to anything that supports the (binary)
|
||||||
buffer API. Except that a comparison with Unicode is always an
|
buffer API. Except that a comparison with Unicode is always an
|
||||||
error, even if the comparison is for equality. */
|
error, even if the comparison is for equality. */
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
|
if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
|
||||||
PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
|
PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
|
||||||
if (Py_BytesWarningFlag && op == Py_EQ) {
|
if (Py_BytesWarningFlag && op == Py_EQ) {
|
||||||
|
@ -1036,6 +1039,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
Py_INCREF(Py_NotImplemented);
|
Py_INCREF(Py_NotImplemented);
|
||||||
return Py_NotImplemented;
|
return Py_NotImplemented;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
self_size = _getbuffer(self, &self_bytes);
|
self_size = _getbuffer(self, &self_bytes);
|
||||||
if (self_size < 0) {
|
if (self_size < 0) {
|
||||||
|
@ -2939,8 +2943,14 @@ bytes_decode(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
|
if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (encoding == NULL)
|
if (encoding == NULL) {
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
encoding = PyUnicode_GetDefaultEncoding();
|
encoding = PyUnicode_GetDefaultEncoding();
|
||||||
|
#else
|
||||||
|
PyErr_SetString(PyExc_ValueError, "no encoding specified");
|
||||||
|
return NULL;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
return PyCodec_Decode(self, encoding, errors);
|
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').");
|
Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
|
||||||
|
|
||||||
static int
|
static int
|
||||||
hex_digit_to_int(Py_UNICODE c)
|
hex_digit_to_int(char c)
|
||||||
{
|
{
|
||||||
if (c >= 128)
|
|
||||||
return -1;
|
|
||||||
if (ISDIGIT(c))
|
if (ISDIGIT(c))
|
||||||
return c - '0';
|
return c - '0';
|
||||||
else {
|
else {
|
||||||
|
@ -3056,17 +3064,14 @@ hex_digit_to_int(Py_UNICODE c)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
bytes_fromhex(PyObject *cls, PyObject *args)
|
bytes_fromhex(PyObject *cls, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *newbytes, *hexobj;
|
PyObject *newbytes;
|
||||||
char *buf;
|
char *buf;
|
||||||
Py_UNICODE *hex;
|
char *hex;
|
||||||
Py_ssize_t hexlen, byteslen, i, j;
|
Py_ssize_t hexlen, byteslen, i, j;
|
||||||
int top, bot;
|
int top, bot;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
|
if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &hexlen))
|
||||||
return NULL;
|
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 */
|
byteslen = hexlen/2; /* This overestimates if there are spaces */
|
||||||
newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
|
newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
|
||||||
if (!newbytes)
|
if (!newbytes)
|
||||||
|
@ -3104,10 +3109,18 @@ bytes_reduce(PyByteArrayObject *self)
|
||||||
{
|
{
|
||||||
PyObject *latin1, *dict;
|
PyObject *latin1, *dict;
|
||||||
if (self->ob_bytes)
|
if (self->ob_bytes)
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
|
latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
|
||||||
Py_SIZE(self), NULL);
|
Py_SIZE(self), NULL);
|
||||||
|
#else
|
||||||
|
latin1 = PyString_FromStringAndSize(self->ob_bytes, Py_SIZE(self))
|
||||||
|
#endif
|
||||||
else
|
else
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
latin1 = PyUnicode_FromString("");
|
latin1 = PyUnicode_FromString("");
|
||||||
|
#else
|
||||||
|
latin1 = PyString_FromString("");
|
||||||
|
#endif
|
||||||
|
|
||||||
dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
|
dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
|
||||||
if (dict == NULL) {
|
if (dict == NULL) {
|
||||||
|
|
|
@ -3418,9 +3418,13 @@ object_format(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
|
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (PyUnicode_Check(format_spec)) {
|
if (PyUnicode_Check(format_spec)) {
|
||||||
self_as_str = PyObject_Unicode(self);
|
self_as_str = PyObject_Unicode(self);
|
||||||
} else if (PyString_Check(format_spec)) {
|
} else if (PyString_Check(format_spec)) {
|
||||||
|
#else
|
||||||
|
if (PyString_Check(format_spec)) {
|
||||||
|
#endif
|
||||||
self_as_str = PyObject_Str(self);
|
self_as_str = PyObject_Str(self);
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str");
|
PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str");
|
||||||
|
|
|
@ -2933,8 +2933,11 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
||||||
PyObject *keyword = kws[2*i];
|
PyObject *keyword = kws[2*i];
|
||||||
PyObject *value = kws[2*i + 1];
|
PyObject *value = kws[2*i + 1];
|
||||||
int j;
|
int j;
|
||||||
if (keyword == NULL || !(PyString_Check(keyword) ||
|
if (keyword == NULL || !(PyString_Check(keyword)
|
||||||
PyUnicode_Check(keyword))) {
|
#ifdef Py_USING_UNICODE
|
||||||
|
|| PyUnicode_Check(keyword)
|
||||||
|
#endif
|
||||||
|
)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"%.200s() keywords must be strings",
|
"%.200s() keywords must be strings",
|
||||||
PyString_AsString(co->co_name));
|
PyString_AsString(co->co_name));
|
||||||
|
@ -3115,14 +3118,20 @@ fail: /* Jump here from prelude on failure */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
kwd_as_string(PyObject *kwd) {
|
kwd_as_string(PyObject *kwd) {
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
if (PyString_Check(kwd)) {
|
if (PyString_Check(kwd)) {
|
||||||
|
#else
|
||||||
|
assert(PyString_Check(kwd));
|
||||||
|
#endif
|
||||||
Py_INCREF(kwd);
|
Py_INCREF(kwd);
|
||||||
return kwd;
|
return kwd;
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
}
|
}
|
||||||
else
|
return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
|
||||||
return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4503,7 +4512,9 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
|
||||||
else if (locals == Py_None)
|
else if (locals == Py_None)
|
||||||
locals = globals;
|
locals = globals;
|
||||||
if (!PyString_Check(prog) &&
|
if (!PyString_Check(prog) &&
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
!PyUnicode_Check(prog) &&
|
!PyUnicode_Check(prog) &&
|
||||||
|
#endif
|
||||||
!PyCode_Check(prog) &&
|
!PyCode_Check(prog) &&
|
||||||
!PyFile_Check(prog)) {
|
!PyFile_Check(prog)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
built-in formatter for unicode. That is, unicode.__format__(). */
|
built-in formatter for unicode. That is, unicode.__format__(). */
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
|
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
|
|
||||||
#include "../Objects/stringlib/unicodedefs.h"
|
#include "../Objects/stringlib/unicodedefs.h"
|
||||||
|
|
||||||
#define FORMAT_STRING _PyUnicode_FormatAdvanced
|
#define FORMAT_STRING _PyUnicode_FormatAdvanced
|
||||||
|
@ -11,3 +14,5 @@
|
||||||
will convert them to unicode. */
|
will convert them to unicode. */
|
||||||
|
|
||||||
#include "../Objects/stringlib/formatter.h"
|
#include "../Objects/stringlib/formatter.h"
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue