Skip to content

Commit 3a3959a

Browse files
committed
Enable more structseq tests
1 parent 0105f05 commit 3a3959a

File tree

2 files changed

+82
-47
lines changed

2 files changed

+82
-47
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,16 +574,15 @@ def CPyExtType(name, code, **kwargs):
574574
PyMODINIT_FUNC
575575
PyInit_{name}(void)
576576
{{
577-
PyObject* m;
577+
PyObject* m = PyModule_Create(&{name}module);
578+
if (m == NULL)
579+
return NULL;
578580
579581
{ready_code}
580582
if (PyType_Ready(&{name}Type) < 0)
581583
return NULL;
582584
{post_ready_code}
583585
584-
m = PyModule_Create(&{name}module);
585-
if (m == NULL)
586-
return NULL;
587586
588587
Py_INCREF(&{name}Type);
589588
PyModule_AddObject(m, "{name}", (PyObject *)&{name}Type);

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_structseq.py

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,64 +39,104 @@
3939

4040
import sys
4141
from . import CPyExtType, CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
42+
4243
__dir__ = __file__.rpartition("/")[0]
4344

44-
#
45-
# class TestPyStructSequenceTypes(object):
46-
# def test_properties(self):
47-
# TestPyStructSequence = CPyExtType("TestPyStructSequence",
48-
# """
49-
# static PyStructSequence_Field typeinfo_fields[] = {
50-
# {"element0", "The first element."},
51-
# {"element1", "The second element."},
52-
# {NULL, NULL,}
53-
# };
54-
#
55-
# static PyStructSequence_Desc typeinfo_desc = {
56-
# "TestPyStructSequenceTypes.TestPyStructSequence",
57-
# "Information about some custom struct type",
58-
# typeinfo_fields,
59-
# 2,
60-
# };
61-
# """,
62-
# ready_code="""if(PyStructSequence_InitType2(&TestPyStructSequenceType, &typeinfo_desc) < 0) {
63-
# return NULL;
64-
# }
65-
# Py_INCREF(&TestPyStructSequenceType);
66-
# """,
67-
# )
68-
# assert TestPyStructSequence.__doc__ == "Information about some custom struct type"
69-
#
70-
# tester = TestPyStructSequence()
71-
# assert hasattr(tester.element0)
72-
# assert hasattr(tester.element1)
45+
46+
class TestPyStructSequenceTypes(object):
47+
def test_properties(self):
48+
TestPyStructSequence = CPyExtType("TestPyStructSequence",
49+
"""
50+
static PyStructSequence_Field typeinfo_fields[] = {
51+
{"element0", "The first element."},
52+
{"element1", "The second element."},
53+
{NULL, NULL,}
54+
};
55+
56+
static PyStructSequence_Desc typeinfo_desc = {
57+
"TestPyStructSequenceTypes.TestPyStructSequence",
58+
"Information about some custom struct type",
59+
typeinfo_fields,
60+
2,
61+
};
62+
""",
63+
ready_code="""
64+
/* our template initializes refcnt to 1; so reset */
65+
Py_REFCNT(&TestPyStructSequenceType) = 0;
66+
if(PyStructSequence_InitType2(&TestPyStructSequenceType, &typeinfo_desc) < 0) {
67+
return NULL;
68+
}
69+
Py_INCREF(&TestPyStructSequenceType);
70+
""",
71+
)
72+
assert TestPyStructSequence.__doc__ == "Information about some custom struct type"
73+
74+
tester = TestPyStructSequence(("hello", "world"))
75+
assert hasattr(tester, "element0")
76+
assert hasattr(tester, "element1")
77+
assert tester.element0 == "hello"
78+
assert tester.element1 == "world"
79+
80+
def test_structseq_newtype(self):
81+
TestPyStructSequenceNewType = CPyExtType("TestPyStructSequenceNewType", """
82+
static PyStructSequence_Field typeinfo_fields[] = {
83+
{"element0", "The first element."},
84+
{"element1", "The second element."},
85+
{"element2", "The third element."},
86+
{NULL, NULL,}
87+
};
88+
89+
static PyStructSequence_Desc typeinfo_desc = {
90+
"TestPyStructSequenceNewType.TestPyStructSequenceNewType",
91+
"Information about some new struct type",
92+
typeinfo_fields,
93+
3,
94+
};
95+
""",
96+
post_ready_code="""
97+
PyTypeObject *new_type = PyStructSequence_NewType(&typeinfo_desc);
98+
if (new_type == NULL)
99+
return NULL;
100+
PyModule_AddObject(m, "TestPyStructSequenceNewType", (PyObject *)new_type);
101+
return m;
102+
""",
103+
)
104+
assert TestPyStructSequenceNewType.__doc__ == "Information about some new struct type"
105+
106+
tester = TestPyStructSequenceNewType(("hello", "beautiful", "world"))
107+
assert hasattr(tester, "element0")
108+
assert hasattr(tester, "element1")
109+
assert hasattr(tester, "element2")
110+
assert tester.element0 == "hello"
111+
assert tester.element1 == "beautiful"
112+
assert tester.element2 == "world"
73113

74114

75115
class TestPyStructSequence(CPyExtTestCase):
76-
116+
77117
def compile_module(self, name):
78118
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
79119
super(TestPyStructSequence, self).compile_module(name)
80-
120+
81121
test_PyStructSequence_InitType2 = CPyExtFunction(
82122
lambda args: 0,
83-
lambda: ( (0,), ),
123+
lambda: ((0,),),
84124
code='''
85125
static PyTypeObject CustomStructSeqType;
86-
126+
87127
static PyStructSequence_Field typeinfo_fields[] = {
88128
{"element0", "The first element."},
89129
{"element1", "The second element."},
90130
{NULL, NULL,}
91131
};
92-
132+
93133
static PyStructSequence_Desc typeinfo_desc = {
94134
"custom.named.tuple",
95135
"Information about some custom struct type",
96136
typeinfo_fields,
97137
2,
98138
};
99-
139+
100140
int wrap_PyStructSequence_InitType2(int n) {
101141
return PyStructSequence_InitType2(&CustomStructSeqType, &typeinfo_desc);
102142
}
@@ -110,12 +150,8 @@ def compile_module(self, name):
110150

111151
test_PyStructSequence_Usage = CPyExtFunction(
112152
lambda args: args[0],
113-
lambda: (
153+
lambda: (
114154
(("hello", "world",),),
115-
# ("john", "doe",),
116-
# (1, "doe",),
117-
# ("john", False,),
118-
# ("john", None,),
119155
),
120156
code='''
121157
static PyTypeObject CustomStructSeqType;
@@ -125,14 +161,14 @@ def compile_module(self, name):
125161
{"element1", "The second element."},
126162
{NULL, NULL,}
127163
};
128-
129-
static PyStructSequence_Desc typeinfo_desc = {
164+
165+
static PyStructSequence_Desc typeinfo_desc = {
130166
"custom.named.tuple",
131167
"Information about some custom struct type",
132168
typeinfo_fields,
133169
2,
134170
};
135-
171+
136172
PyObject* wrap_PyStructSequence_Usage(PyObject* elements) {
137173
Py_ssize_t i = 0;
138174
PyObject* elem = NULL;

0 commit comments

Comments
 (0)