diff options
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index 22d0e03..059d988 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -104,6 +104,15 @@ typedef struct { char c; PY_LONG_LONG x; } s_long_long; #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) #endif +#ifdef HAVE_C99_BOOL +#define BOOL_TYPE _Bool +typedef struct { char c; _Bool x; } s_bool; +#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE)) +#else +#define BOOL_TYPE char +#define BOOL_ALIGN 0 +#endif + #define STRINGIFY(x) #x #ifdef __powerc @@ -536,6 +545,15 @@ nu_ulonglong(const char *p, const formatdef *f) #endif static PyObject * +nu_bool(const char *p, const formatdef *f) +{ + BOOL_TYPE x; + memcpy((char *)&x, p, sizeof x); + return PyBool_FromLong(x != 0); +} + + +static PyObject * nu_float(const char *p, const formatdef *f) { float x; @@ -711,6 +729,16 @@ np_ulonglong(char *p, PyObject *v, const formatdef *f) } #endif + +static int +np_bool(char *p, PyObject *v, const formatdef *f) +{ + BOOL_TYPE y; + y = PyObject_IsTrue(v); + memcpy(p, (char *)&y, sizeof y); + return 0; +} + static int np_float(char *p, PyObject *v, const formatdef *f) { @@ -771,6 +799,7 @@ static formatdef native_table[] = { {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, #endif + {'t', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool}, {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, @@ -865,6 +894,14 @@ bu_double(const char *p, const formatdef *f) return unpack_double(p, 0); } +static PyObject * +bu_bool(const char *p, const formatdef *f) +{ + char x; + memcpy((char *)&x, p, sizeof x); + return PyBool_FromLong(x != 0); +} + static int bp_int(char *p, PyObject *v, const formatdef *f) { @@ -969,6 +1006,15 @@ bp_double(char *p, PyObject *v, const formatdef *f) return _PyFloat_Pack8(x, (unsigned char *)p, 0); } +static int +bp_bool(char *p, PyObject *v, const formatdef *f) +{ + char y; + y = PyObject_IsTrue(v); + memcpy(p, (char *)&y, sizeof y); + return 0; +} + static formatdef bigendian_table[] = { {'x', 1, 0, NULL}, #ifdef PY_STRUCT_OVERFLOW_MASKING @@ -990,6 +1036,7 @@ static formatdef bigendian_table[] = { {'L', 4, 0, bu_uint, bp_uint}, {'q', 8, 0, bu_longlong, bp_longlong}, {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, + {'t', 1, 0, bu_bool, bp_bool}, {'f', 4, 0, bu_float, bp_float}, {'d', 8, 0, bu_double, bp_double}, {0} @@ -1208,6 +1255,8 @@ static formatdef lilendian_table[] = { {'L', 4, 0, lu_uint, lp_uint}, {'q', 8, 0, lu_longlong, lp_longlong}, {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, + {'t', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep, + but potentially different from native rep -- reuse bx_bool funcs. */ {'f', 4, 0, lu_float, lp_float}, {'d', 8, 0, lu_double, lp_double}, {0} |