results = executor.map(exec, [code] * 5)
self.assertListEqual(list(results), [None] * 5)
+ def test_operations_on_half_initialized_Struct(self):
+ S = struct.Struct.__new__(struct.Struct)
+
+ spam = array.array('b', b' ')
+ self.assertRaises(RuntimeError, S.iter_unpack, spam)
+ self.assertRaises(RuntimeError, S.pack, 1)
+ self.assertRaises(RuntimeError, S.pack_into, spam, 1)
+ self.assertRaises(RuntimeError, S.unpack, spam)
+ self.assertRaises(RuntimeError, S.unpack_from, spam)
+ self.assertRaises(RuntimeError, getattr, S, 'format')
+ self.assertEqual(S.size, -1)
+
class UnpackIteratorTest(unittest.TestCase):
"""
return -1;
}
- self->s_size = size;
- self->s_len = len;
codes = PyMem_Malloc((ncodes + 1) * sizeof(formatcode));
if (codes == NULL) {
PyErr_NoMemory();
if (self->s_codes != NULL)
PyMem_Free(self->s_codes);
self->s_codes = codes;
+ self->s_size = size;
+ self->s_len = len;
s = fmt;
size = 0;
return NULL;
}
+#define ENSURE_STRUCT_IS_READY(self) \
+ do { \
+ if (!(self)->s_codes) { \
+ PyErr_SetString(PyExc_RuntimeError, \
+ "Struct object is not initialized"); \
+ return NULL; \
+ } \
+ } while (0);
/*[clinic input]
Struct.unpack
/*[clinic end generated code: output=873a24faf02e848a input=3113f8e7038b2f6c]*/
{
_structmodulestate *state = get_struct_state_structinst(self);
- assert(self->s_codes != NULL);
+ ENSURE_STRUCT_IS_READY(self);
if (buffer->len != self->s_size) {
PyErr_Format(state->StructError,
"unpack requires a buffer of %zd bytes",
/*[clinic end generated code: output=57fac875e0977316 input=cafd4851d473c894]*/
{
_structmodulestate *state = get_struct_state_structinst(self);
- assert(self->s_codes != NULL);
+ ENSURE_STRUCT_IS_READY(self);
if (offset < 0) {
if (offset + self->s_size > 0) {
{
_structmodulestate *state = get_struct_state_structinst(self);
unpackiterobject *iter;
-
- assert(self->s_codes != NULL);
+ ENSURE_STRUCT_IS_READY(self);
if (self->s_size == 0) {
PyErr_Format(state->StructError,
/* Validate arguments. */
soself = PyStructObject_CAST(self);
+ ENSURE_STRUCT_IS_READY(soself);
assert(PyStruct_Check(self, state));
- assert(soself->s_codes != NULL);
if (nargs != soself->s_len)
{
PyErr_Format(state->StructError,
/* Validate arguments. +1 is for the first arg as buffer. */
soself = PyStructObject_CAST(self);
+ ENSURE_STRUCT_IS_READY(soself);
assert(PyStruct_Check(self, state));
- assert(soself->s_codes != NULL);
if (nargs != (soself->s_len + 2))
{
if (nargs == 0) {
s_get_format(PyObject *op, void *Py_UNUSED(closure))
{
PyStructObject *self = PyStructObject_CAST(op);
+ ENSURE_STRUCT_IS_READY(self);
return PyUnicode_FromStringAndSize(PyBytes_AS_STRING(self->s_format),
PyBytes_GET_SIZE(self->s_format));
}