Skip to content

Commit 30aee00

Browse files
author
benjamin.peterson
committed
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 git-svn-id: http://svn.python.org/projects/python/trunk@68925 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent bb3ed39 commit 30aee00

7 files changed

Lines changed: 70 additions & 22 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ def test_fromhex(self):
250250
self.assertEquals(self.type2test.fromhex(u'1a2B30'), b)
251251
self.assertEquals(self.type2test.fromhex(u' 1A 2B 30 '), b)
252252
self.assertEquals(self.type2test.fromhex(u'0000'), b'\0\0')
253-
self.assertRaises(TypeError, self.type2test.fromhex, b'1B')
254253
self.assertRaises(ValueError, self.type2test.fromhex, u'a')
255254
self.assertRaises(ValueError, self.type2test.fromhex, u'rt')
256255
self.assertRaises(ValueError, self.type2test.fromhex, u'1a b cd')

Modules/gcmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,9 @@ clear_freelists(void)
784784
(void)PyFrame_ClearFreeList();
785785
(void)PyCFunction_ClearFreeList();
786786
(void)PyTuple_ClearFreeList();
787+
#ifdef Py_USING_UNICODE
787788
(void)PyUnicode_ClearFreeList();
789+
#endif
788790
(void)PyInt_ClearFreeList();
789791
(void)PyFloat_ClearFreeList();
790792
}

Objects/abstract.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,10 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
720720
static PyObject * str__format__ = NULL;
721721
PyObject *empty = NULL;
722722
PyObject *result = NULL;
723+
#ifdef Py_USING_UNICODE
723724
int spec_is_unicode;
724725
int result_is_unicode;
726+
#endif
725727

726728
/* Initialize cached value */
727729
if (str__format__ == NULL) {
@@ -738,11 +740,15 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
738740
}
739741

740742
/* Check the format_spec type, and make sure it's str or unicode */
743+
#if Py_USING_UNICODE
741744
if (PyUnicode_Check(format_spec))
742745
spec_is_unicode = 1;
743746
else if (PyString_Check(format_spec))
744747
spec_is_unicode = 0;
745748
else {
749+
#else
750+
if (!PyString_Check(format_spec)) {
751+
#endif
746752
PyErr_Format(PyExc_TypeError,
747753
"format expects arg 2 to be string "
748754
"or unicode, not %.100s", Py_TYPE(format_spec)->tp_name);
@@ -773,9 +779,11 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
773779
depending on the type of the format
774780
specifier). For new-style classes, this
775781
logic is done by object.__format__(). */
782+
#ifdef Py_USING_UNICODE
776783
if (spec_is_unicode)
777784
self_as_str = PyObject_Unicode(obj);
778785
else
786+
#endif
779787
self_as_str = PyObject_Str(obj);
780788
if (self_as_str == NULL)
781789
goto done;
@@ -818,11 +826,15 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
818826
goto done;
819827

820828
/* Check the result type, and make sure it's str or unicode */
829+
#ifdef Py_USING_UNICODE
821830
if (PyUnicode_Check(result))
822831
result_is_unicode = 1;
823832
else if (PyString_Check(result))
824833
result_is_unicode = 0;
825834
else {
835+
#else
836+
if (!PyString_Check(result)) {
837+
#endif
826838
PyErr_Format(PyExc_TypeError,
827839
"%.100s.__format__ must return string or "
828840
"unicode, not %.100s", Py_TYPE(obj)->tp_name,
@@ -834,12 +846,14 @@ PyObject_Format(PyObject* obj, PyObject *format_spec)
834846

835847
/* Convert to unicode, if needed. Required if spec is unicode
836848
and result is str */
849+
#ifdef Py_USING_UNICODE
837850
if (spec_is_unicode && !result_is_unicode) {
838851
PyObject *tmp = PyObject_Unicode(result);
839852
/* This logic works whether or not tmp is NULL */
840853
Py_DECREF(result);
841854
result = tmp;
842855
}
856+
#endif
843857

844858
done:
845859
Py_XDECREF(empty);

Objects/bytearrayobject.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
803803
return 0;
804804
}
805805

806+
#ifdef Py_USING_UNICODE
806807
if (PyUnicode_Check(arg)) {
807808
/* Encode via the codec registry */
808809
PyObject *encoded, *new;
@@ -822,6 +823,7 @@ bytes_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
822823
Py_DECREF(new);
823824
return 0;
824825
}
826+
#endif
825827

826828
/* If it's not unicode, there can't be encoding or errors */
827829
if (encoding != NULL || errors != NULL) {
@@ -929,14 +931,14 @@ bytes_repr(PyByteArrayObject *self)
929931
"bytearray object is too large to make repr");
930932
return NULL;
931933
}
932-
v = PyUnicode_FromUnicode(NULL, newsize);
934+
v = PyString_FromStringAndSize(NULL, newsize);
933935
if (v == NULL) {
934936
return NULL;
935937
}
936938
else {
937939
register Py_ssize_t i;
938-
register Py_UNICODE c;
939-
register Py_UNICODE *p;
940+
register char c;
941+
register char *p;
940942
int quote;
941943

942944
/* Figure out which quote to use; single is preferred */
@@ -956,15 +958,15 @@ bytes_repr(PyByteArrayObject *self)
956958
;
957959
}
958960

959-
p = PyUnicode_AS_UNICODE(v);
961+
p = PyString_AS_STRING(v);
960962
while (*quote_prefix)
961963
*p++ = *quote_prefix++;
962964
*p++ = quote;
963965

964966
for (i = 0; i < length; i++) {
965967
/* There's at least enough room for a hex escape
966968
and a closing quote. */
967-
assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
969+
assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
968970
c = self->ob_bytes[i];
969971
if (c == '\'' || c == '\\')
970972
*p++ = '\\', *p++ = c;
@@ -985,13 +987,13 @@ bytes_repr(PyByteArrayObject *self)
985987
else
986988
*p++ = c;
987989
}
988-
assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
990+
assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
989991
*p++ = quote;
990992
while (*quote_postfix) {
991993
*p++ = *quote_postfix++;
992994
}
993995
*p = '\0';
994-
if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
996+
if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) {
995997
Py_DECREF(v);
996998
return NULL;
997999
}
@@ -1025,6 +1027,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
10251027
/* Bytes can be compared to anything that supports the (binary)
10261028
buffer API. Except that a comparison with Unicode is always an
10271029
error, even if the comparison is for equality. */
1030+
#ifdef Py_USING_UNICODE
10281031
if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
10291032
PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
10301033
if (Py_BytesWarningFlag && op == Py_EQ) {
@@ -1036,6 +1039,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
10361039
Py_INCREF(Py_NotImplemented);
10371040
return Py_NotImplemented;
10381041
}
1042+
#endif
10391043

10401044
self_size = _getbuffer(self, &self_bytes);
10411045
if (self_size < 0) {
@@ -2939,8 +2943,14 @@ bytes_decode(PyObject *self, PyObject *args)
29392943

29402944
if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
29412945
return NULL;
2942-
if (encoding == NULL)
2946+
if (encoding == NULL) {
2947+
#ifdef Py_USING_UNICODE
29432948
encoding = PyUnicode_GetDefaultEncoding();
2949+
#else
2950+
PyErr_SetString(PyExc_ValueError, "no encoding specified");
2951+
return NULL;
2952+
#endif
2953+
}
29442954
return PyCodec_Decode(self, encoding, errors);
29452955
}
29462956

@@ -3038,10 +3048,8 @@ Spaces between two numbers are accepted.\n\
30383048
Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
30393049

30403050
static int
3041-
hex_digit_to_int(Py_UNICODE c)
3051+
hex_digit_to_int(char c)
30423052
{
3043-
if (c >= 128)
3044-
return -1;
30453053
if (ISDIGIT(c))
30463054
return c - '0';
30473055
else {
@@ -3056,17 +3064,14 @@ hex_digit_to_int(Py_UNICODE c)
30563064
static PyObject *
30573065
bytes_fromhex(PyObject *cls, PyObject *args)
30583066
{
3059-
PyObject *newbytes, *hexobj;
3067+
PyObject *newbytes;
30603068
char *buf;
3061-
Py_UNICODE *hex;
3069+
char *hex;
30623070
Py_ssize_t hexlen, byteslen, i, j;
30633071
int top, bot;
30643072

3065-
if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
3073+
if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &hexlen))
30663074
return NULL;
3067-
assert(PyUnicode_Check(hexobj));
3068-
hexlen = PyUnicode_GET_SIZE(hexobj);
3069-
hex = PyUnicode_AS_UNICODE(hexobj);
30703075
byteslen = hexlen/2; /* This overestimates if there are spaces */
30713076
newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
30723077
if (!newbytes)
@@ -3104,10 +3109,18 @@ bytes_reduce(PyByteArrayObject *self)
31043109
{
31053110
PyObject *latin1, *dict;
31063111
if (self->ob_bytes)
3112+
#ifdef Py_USING_UNICODE
31073113
latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
31083114
Py_SIZE(self), NULL);
3115+
#else
3116+
latin1 = PyString_FromStringAndSize(self->ob_bytes, Py_SIZE(self))
3117+
#endif
31093118
else
3119+
#ifdef Py_USING_UNICODE
31103120
latin1 = PyUnicode_FromString("");
3121+
#else
3122+
latin1 = PyString_FromString("");
3123+
#endif
31113124

31123125
dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
31133126
if (dict == NULL) {

Objects/typeobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,9 +3418,13 @@ object_format(PyObject *self, PyObject *args)
34183418

34193419
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
34203420
return NULL;
3421+
#ifdef Py_USING_UNICODE
34213422
if (PyUnicode_Check(format_spec)) {
34223423
self_as_str = PyObject_Unicode(self);
34233424
} else if (PyString_Check(format_spec)) {
3425+
#else
3426+
if (PyString_Check(format_spec)) {
3427+
#endif
34243428
self_as_str = PyObject_Str(self);
34253429
} else {
34263430
PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str");

Python/ceval.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,8 +2933,11 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
29332933
PyObject *keyword = kws[2*i];
29342934
PyObject *value = kws[2*i + 1];
29352935
int j;
2936-
if (keyword == NULL || !(PyString_Check(keyword) ||
2937-
PyUnicode_Check(keyword))) {
2936+
if (keyword == NULL || !(PyString_Check(keyword)
2937+
#ifdef Py_USING_UNICODE
2938+
|| PyUnicode_Check(keyword)
2939+
#endif
2940+
)) {
29382941
PyErr_Format(PyExc_TypeError,
29392942
"%.200s() keywords must be strings",
29402943
PyString_AsString(co->co_name));
@@ -3115,14 +3118,20 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
31153118
}
31163119

31173120

3121+
31183122
static PyObject *
31193123
kwd_as_string(PyObject *kwd) {
3124+
#ifdef Py_USING_UNICODE
31203125
if (PyString_Check(kwd)) {
3126+
#else
3127+
assert(PyString_Check(kwd));
3128+
#endif
31213129
Py_INCREF(kwd);
31223130
return kwd;
3131+
#ifdef Py_USING_UNICODE
31233132
}
3124-
else
3125-
return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3133+
return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3134+
#endif
31263135
}
31273136

31283137

@@ -4503,7 +4512,9 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
45034512
else if (locals == Py_None)
45044513
locals = globals;
45054514
if (!PyString_Check(prog) &&
4515+
#ifdef Py_USING_UNICODE
45064516
!PyUnicode_Check(prog) &&
4517+
#endif
45074518
!PyCode_Check(prog) &&
45084519
!PyFile_Check(prog)) {
45094520
PyErr_SetString(PyExc_TypeError,

Python/formatter_unicode.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
built-in formatter for unicode. That is, unicode.__format__(). */
33

44
#include "Python.h"
5+
6+
#ifdef Py_USING_UNICODE
7+
58
#include "../Objects/stringlib/unicodedefs.h"
69

710
#define FORMAT_STRING _PyUnicode_FormatAdvanced
@@ -11,3 +14,5 @@
1114
will convert them to unicode. */
1215

1316
#include "../Objects/stringlib/formatter.h"
17+
18+
#endif

0 commit comments

Comments
 (0)