Fix a couple of issues with the test_structmembersType class in _testcapimodule

- rename to _test_structmembersType to avoid the class being automatically
   called by test_capi

 - allow space for trailing NUL in inplace_member field of all_structmembers

 - use T_STRING_INPLACE instead of T_INPLACE_STRING as keyword argument
   to _test_structmembersType initializer

 - don't attempt to initialize inplace_member field if T_STRING_INPLACE
   argument wasn't supplied.
This commit is contained in:
Mark Dickinson 2010-04-03 10:27:05 +00:00
parent 34c35b2788
commit b05dc00fb7
2 changed files with 19 additions and 12 deletions

View File

@ -1,4 +1,4 @@
from _testcapi import test_structmembersType, \ from _testcapi import _test_structmembersType, \
CHAR_MAX, CHAR_MIN, UCHAR_MAX, \ CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
SHRT_MAX, SHRT_MIN, USHRT_MAX, \ SHRT_MAX, SHRT_MIN, USHRT_MAX, \
INT_MAX, INT_MIN, UINT_MAX, \ INT_MAX, INT_MIN, UINT_MAX, \
@ -8,7 +8,7 @@ from _testcapi import test_structmembersType, \
import unittest import unittest
from test import test_support from test import test_support
ts=test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8, ts=_test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8,
9.99999, 10.1010101010, "hi") 9.99999, 10.1010101010, "hi")
class ReadWriteTests(unittest.TestCase): class ReadWriteTests(unittest.TestCase):

View File

@ -1712,7 +1712,7 @@ typedef struct {
unsigned long ulong_member; unsigned long ulong_member;
float float_member; float float_member;
double double_member; double double_member;
char inplace_member[5]; char inplace_member[6];
#ifdef HAVE_LONG_LONG #ifdef HAVE_LONG_LONG
PY_LONG_LONG longlong_member; PY_LONG_LONG longlong_member;
unsigned PY_LONG_LONG ulonglong_member; unsigned PY_LONG_LONG ulonglong_member;
@ -1751,7 +1751,7 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
static char *keywords[] = { static char *keywords[] = {
"T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
"T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_INT", "T_UINT", "T_LONG", "T_ULONG",
"T_FLOAT", "T_DOUBLE", "T_INPLACE_STRING", "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
#ifdef HAVE_LONG_LONG #ifdef HAVE_LONG_LONG
"T_LONGLONG", "T_ULONGLONG", "T_LONGLONG", "T_ULONGLONG",
#endif #endif
@ -1762,7 +1762,7 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
#endif #endif
; ;
test_structmembers *ob; test_structmembers *ob;
const char *s; const char *s = NULL;
Py_ssize_t string_len = 0; Py_ssize_t string_len = 0;
ob = PyObject_New(test_structmembers, type); ob = PyObject_New(test_structmembers, type);
if (ob == NULL) if (ob == NULL)
@ -1789,12 +1789,17 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
Py_DECREF(ob); Py_DECREF(ob);
return NULL; return NULL;
} }
if (s != NULL) {
if (string_len > 5) { if (string_len > 5) {
Py_DECREF(ob); Py_DECREF(ob);
PyErr_SetString(PyExc_ValueError, "string too long"); PyErr_SetString(PyExc_ValueError, "string too long");
return NULL; return NULL;
} }
strcpy(ob->structmembers.inplace_member, s); strcpy(ob->structmembers.inplace_member, s);
}
else {
strcpy(ob->structmembers.inplace_member, "");
}
return (PyObject *)ob; return (PyObject *)ob;
} }
@ -1859,7 +1864,9 @@ init_testcapi(void)
Py_TYPE(&test_structmembersType)=&PyType_Type; Py_TYPE(&test_structmembersType)=&PyType_Type;
Py_INCREF(&test_structmembersType); Py_INCREF(&test_structmembersType);
PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); /* don't use a name starting with "test", since we don't want
test_capi to automatically call this */
PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));