diff -r 0d41a1b4c4fe Modules/_struct.c --- a/Modules/_struct.c Mon Oct 08 07:46:11 2012 +0200 +++ b/Modules/_struct.c Mon Oct 08 20:17:25 2012 +0300 @@ -1199,12 +1199,11 @@ case '!': /* Network byte order is big-endian */ return bigendian_table; case '=': { /* Host byte order -- different from native in alignment! */ - int n = 1; - char *p = (char *) &n; - if (*p == 1) - return lilendian_table; - else - return bigendian_table; +#ifdef WORDS_BIGENDIAN + return bigendian_table; +#else + return lilendian_table; +#endif } default: --*pfmt; /* Back out of pointer increment */ @@ -2088,13 +2087,13 @@ /* Check endian and swap in faster functions */ { - int one = 1; formatdef *native = native_table; formatdef *other, *ptr; - if ((int)*(unsigned char*)&one) - other = lilendian_table; - else - other = bigendian_table; +#ifdef WORDS_BIGENDIAN + other = bigendian_table; +#else + other = lilendian_table; +#endif /* Scan through the native table, find a matching entry in the endian table and swap in the native implementations whenever possible diff -r 0d41a1b4c4fe Objects/longobject.c --- a/Objects/longobject.c Mon Oct 08 07:46:11 2012 +0200 +++ b/Objects/longobject.c Mon Oct 08 20:17:25 2012 +0300 @@ -988,7 +988,11 @@ * rewritten to use the newer PyLong_{As,From}ByteArray API. */ -#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one +#ifdef WORDS_BIGENDIAN +#define IS_LITTLE_ENDIAN 0 +#else +#define IS_LITTLE_ENDIAN 1 +#endif #define PY_ABS_LLONG_MIN (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN) /* Create a new long int object from a C PY_LONG_LONG int. */ @@ -1141,7 +1145,6 @@ { PyLongObject *v; PY_LONG_LONG bytes; - int one = 1; int res; if (vv == NULL) { @@ -1193,7 +1196,6 @@ { PyLongObject *v; unsigned PY_LONG_LONG bytes; - int one = 1; int res; if (vv == NULL) { diff -r 0d41a1b4c4fe Objects/stringlib/codecs.h --- a/Objects/stringlib/codecs.h Mon Oct 08 07:46:11 2012 +0200 +++ b/Objects/stringlib/codecs.h Mon Oct 08 20:17:25 2012 +0300 @@ -47,18 +47,7 @@ unsigned long value = *(unsigned long *) _s; if (value & ASCII_CHAR_MASK) break; -#ifdef BYTEORDER_IS_LITTLE_ENDIAN - _p[0] = (STRINGLIB_CHAR)(value & 0xFFu); - _p[1] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu); - _p[2] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu); - _p[3] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu); -# if SIZEOF_LONG == 8 - _p[4] = (STRINGLIB_CHAR)((value >> 32) & 0xFFu); - _p[5] = (STRINGLIB_CHAR)((value >> 40) & 0xFFu); - _p[6] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu); - _p[7] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu); -# endif -#else +#ifdef WORDS_BIGENDIAN # if SIZEOF_LONG == 8 _p[0] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu); _p[1] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu); @@ -74,6 +63,17 @@ _p[2] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu); _p[3] = (STRINGLIB_CHAR)(value & 0xFFu); # endif +#else + _p[0] = (STRINGLIB_CHAR)(value & 0xFFu); + _p[1] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu); + _p[2] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu); + _p[3] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu); +# if SIZEOF_LONG == 8 + _p[4] = (STRINGLIB_CHAR)((value >> 32) & 0xFFu); + _p[5] = (STRINGLIB_CHAR)((value >> 40) & 0xFFu); + _p[6] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu); + _p[7] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu); +# endif #endif _s += SIZEOF_LONG; _p += SIZEOF_LONG; @@ -454,10 +454,10 @@ const unsigned char *q = *inptr; STRINGLIB_CHAR *p = dest + *outpos; /* Offsets from q for retrieving byte pairs in the right order. */ -#ifdef BYTEORDER_IS_LITTLE_ENDIAN +#ifdef WORDS_BIGENDIAN + int ihi = !native_ordering, ilo = !!native_ordering; +#else int ihi = !!native_ordering, ilo = !native_ordering; -#else - int ihi = !native_ordering, ilo = !!native_ordering; #endif --e; @@ -485,7 +485,17 @@ block = SWAB(block); #endif } -#ifdef BYTEORDER_IS_LITTLE_ENDIAN +#ifdef WORDS_BIGENDIAN +# if SIZEOF_LONG == 4 + p[0] = (STRINGLIB_CHAR)(block >> 16); + p[1] = (STRINGLIB_CHAR)(block & 0xFFFFu); +# elif SIZEOF_LONG == 8 + p[0] = (STRINGLIB_CHAR)(block >> 48); + p[1] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu); + p[2] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu); + p[3] = (STRINGLIB_CHAR)(block & 0xFFFFu); +# endif +#else # if SIZEOF_LONG == 4 p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu); p[1] = (STRINGLIB_CHAR)(block >> 16); @@ -495,16 +505,6 @@ p[2] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu); p[3] = (STRINGLIB_CHAR)(block >> 48); # endif -#else -# if SIZEOF_LONG == 4 - p[0] = (STRINGLIB_CHAR)(block >> 16); - p[1] = (STRINGLIB_CHAR)(block & 0xFFFFu); -# elif SIZEOF_LONG == 8 - p[0] = (STRINGLIB_CHAR)(block >> 48); - p[1] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu); - p[2] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu); - p[3] = (STRINGLIB_CHAR)(block & 0xFFFFu); -# endif #endif _q += SIZEOF_LONG; p += SIZEOF_LONG / 2; diff -r 0d41a1b4c4fe Objects/unicodeobject.c --- a/Objects/unicodeobject.c Mon Oct 08 07:46:11 2012 +0200 +++ b/Objects/unicodeobject.c Mon Oct 08 20:17:25 2012 +0300 @@ -47,14 +47,6 @@ #include #endif -/* Endianness switches; defaults to little endian */ - -#ifdef WORDS_BIGENDIAN -# define BYTEORDER_IS_BIG_ENDIAN -#else -# define BYTEORDER_IS_LITTLE_ENDIAN -#endif - /* --- Globals ------------------------------------------------------------ The globals are initialized by the _PyUnicode_Init() API and should @@ -4813,10 +4805,10 @@ int bo = 0; /* assume native ordering by default */ const char *errmsg = ""; /* Offsets from q for retrieving bytes in the right order. */ -#ifdef BYTEORDER_IS_LITTLE_ENDIAN +#ifdef WORDS_BIGENDIAN + int iorder[] = {3, 2, 1, 0}; +#else int iorder[] = {0, 1, 2, 3}; -#else - int iorder[] = {3, 2, 1, 0}; #endif PyObject *errorHandler = NULL; PyObject *exc = NULL; @@ -4835,7 +4827,16 @@ if (size >= 4) { const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | (q[iorder[1]] << 8) | q[iorder[0]]; -#ifdef BYTEORDER_IS_LITTLE_ENDIAN +#ifdef WORDS_BIGENDIAN + if (bom == 0x0000FEFF) { + q += 4; + bo = 1; + } + else if (bom == 0xFFFE0000) { + q += 4; + bo = -1; + } +#else if (bom == 0x0000FEFF) { q += 4; bo = -1; @@ -4844,15 +4845,6 @@ q += 4; bo = 1; } -#else - if (bom == 0x0000FEFF) { - q += 4; - bo = 1; - } - else if (bom == 0xFFFE0000) { - q += 4; - bo = -1; - } #endif } } @@ -4949,10 +4941,10 @@ unsigned char *p; Py_ssize_t nsize, i; /* Offsets from p for storing byte pairs in the right order. */ -#ifdef BYTEORDER_IS_LITTLE_ENDIAN +#ifdef WORDS_BIGENDIAN + int iorder[] = {3, 2, 1, 0}; +#else int iorder[] = {0, 1, 2, 3}; -#else - int iorder[] = {3, 2, 1, 0}; #endif #define STORECHAR(CH) \ @@ -5092,10 +5084,10 @@ return unicode_empty; } -#ifdef BYTEORDER_IS_LITTLE_ENDIAN +#ifdef WORDS_BIGENDIAN + native_ordering = bo >= 0; +#else native_ordering = bo <= 0; -#else - native_ordering = bo >= 0; #endif /* Note: size will always be longer than the resulting Unicode diff -r 0d41a1b4c4fe Python/marshal.c --- a/Python/marshal.c Mon Oct 08 07:46:11 2012 +0200 +++ b/Python/marshal.c Mon Oct 08 20:17:25 2012 +0300 @@ -570,17 +570,15 @@ result = PyLong_FromLong(x); #else unsigned char buf[8]; - int one = 1; - int is_little_endian = (int)*(char*)&one; - if (is_little_endian) { - memcpy(buf, &lo4, 4); - memcpy(buf+4, &hi4, 4); - } - else { - memcpy(buf, &hi4, 4); - memcpy(buf+4, &lo4, 4); - } - result = _PyLong_FromByteArray(buf, 8, is_little_endian, 1); +#ifdef WORDS_BIGENDIAN + memcpy(buf, &hi4, 4); + memcpy(buf+4, &lo4, 4); + result = _PyLong_FromByteArray(buf, 8, 0, 1); +#else + memcpy(buf, &lo4, 4); + memcpy(buf+4, &hi4, 4); + result = _PyLong_FromByteArray(buf, 8, 1, 1); +#endif #endif } return result; diff -r 0d41a1b4c4fe Python/sysmodule.c --- a/Python/sysmodule.c Mon Oct 08 07:46:11 2012 +0200 +++ b/Python/sysmodule.c Mon Oct 08 20:17:25 2012 +0300 @@ -1561,7 +1561,6 @@ _PySys_Init(void) { PyObject *m, *v, *sysdict, *version_info; - char *s; m = PyModule_Create(&sysmodule); if (m == NULL) @@ -1638,20 +1637,13 @@ PyLong_FromLong(0x10FFFF)); SET_SYS_FROM_STRING("builtin_module_names", list_builtin_module_names()); - { - /* Assumes that longs are at least 2 bytes long. - Should be safe! */ - unsigned long number = 1; - char *value; - - s = (char *) &number; - if (s[0] == 0) - value = "big"; - else - value = "little"; - SET_SYS_FROM_STRING("byteorder", - PyUnicode_FromString(value)); - } +#ifdef WORDS_BIGENDIAN + SET_SYS_FROM_STRING("byteorder", + PyUnicode_FromString("big")); +#else + SET_SYS_FROM_STRING("byteorder", + PyUnicode_FromString("little")); +#endif #ifdef MS_COREDLL SET_SYS_FROM_STRING("dllhandle", PyLong_FromVoidPtr(PyWin_DLLhModule));