File: test_structseq.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (62 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from rpython.rtyper.lltypesystem import rffi, lltype
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.api import PyObject

class AppTestStructSeq(AppTestCpythonExtensionBase):
    def test_StructSeq(self):
        module = self.import_extension('foo',
        prologue="""
            #include <structseq.h>
            static PyTypeObject* PyDatatype;

            static PyStructSequence_Field Data_fields[] = {
                {"value", "value_doc"},
                {"value2", "value_doc"},
                {"text",  "text_doc"},
                {"other", "other_doc"},
                {NULL}  /* Sentinel */
            };
            static PyStructSequence_Desc Data_desc = {
                "cpyext_test.data",           /*name*/
                "data_doc",                   /*doc*/
                Data_fields,                  /*fields*/
                3,                            /*n_in_sequence*/
            };
            """, more_init = """
            """,
        functions=[
            ("new_structdata", "METH_NOARGS",
             """
                 PyObject *seq;
                 PyDatatype = PyStructSequence_NewType(&Data_desc);
                 seq = PyStructSequence_New(PyDatatype);
                 if (!seq) return NULL;
                 PyStructSequence_SET_ITEM(seq, 0, PyLong_FromLong(42));
                 PyStructSequence_SET_ITEM(seq, 1, PyLong_FromLong(43));
                 PyStructSequence_SetItem(seq, 2, PyUnicode_FromString("hello"));
                 PyStructSequence_SetItem(seq, 3, PyUnicode_FromString("other"));
                 Py_DECREF(PyDatatype);
                 return seq;
             """),
            ("getitem", "METH_VARARGS",
             """
                PyObject *obj, *result=NULL;
                int i;
                if (PyArg_ParseTuple(args, "Oi:test_get_item", &obj, &i)) {
                    result = PyStructSequence_GetItem(obj, i);
                    Py_INCREF(result);
                };
                return result;
             """),
            ])
        s = module.new_structdata()
        assert tuple(s) == (42, 43, 'hello')
        assert s.value == 42
        assert s.text == 'hello'
        assert s.other == 'other'
        assert 'hello' in s
        assert 'other' not in s
        assert module.getitem(s, 1) == 43
        del s