Skip to content

Commit

Permalink
Updated stub file
Browse files Browse the repository at this point in the history
---

+ Removed field related tests
  • Loading branch information
MatrixEditor committed Sep 28, 2024
1 parent 2ec7343 commit a1cde82
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 267 deletions.
333 changes: 180 additions & 153 deletions src/caterpillar/_C.pyi

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/ccaterpillar/atomimpl/builtins/atoffset.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ CpOffsetAtom_Unpack(CpOffsetAtomObject* self, CpLayerObject* layer)
static PyMemberDef CpOffsetAtom_Members[] = {
{ "offset", T_OBJECT_EX, offsetof(CpOffsetAtomObject, m_offset), READONLY },
{ "whence", T_OBJECT_EX, offsetof(CpOffsetAtomObject, m_whence), READONLY },
{ "atom", T_OBJECT_EX, offsetof(CpOffsetAtomObject, m_atom), READONLY },
{ NULL } /* Sentinel */
};

Expand Down
2 changes: 1 addition & 1 deletion src/ccaterpillar/atomimpl/builtins/switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ _CpEndian_ImplSetByteorder(CpSwitchAtomObject, switchatom, self->m_atom);
static PyObject*
cp_switchatom_get_next(CpSwitchAtomObject* self, PyObject* args, PyObject* kw)
{
static char* kwlist[] = { "atom", "context", NULL };
static char* kwlist[] = { "obj", "context", NULL };
PyObject* op = NULL;
PyObject* context = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO", kwlist, &op, &context)) {
Expand Down
7 changes: 6 additions & 1 deletion src/ccaterpillar/atomimpl/cstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ static PyMemberDef CpCStringAtom_Members[] = {
offsetof(CpCStringAtomObject, s_keep_terminator),
0,
"whether to keep the terminator" },
{ "length",
T_OBJECT_EX,
offsetof(CpCStringAtomObject, m_length),
0,
"the length of the string" },
{ NULL }
};

Expand All @@ -304,7 +309,7 @@ PyTypeObject CpCStringAtom_Type = {
.tp_itemsize = 0,
.tp_dealloc = (destructor)cp_cstringatom_dealloc,
.tp_repr = (reprfunc)cp_cstringatom_repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_new = (newfunc)cp_cstringatom_new,
.tp_members = CpCStringAtom_Members,
.tp_init = (initproc)cp_cstringatom_init,
Expand Down
2 changes: 1 addition & 1 deletion src/ccaterpillar/atomimpl/pstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ PyTypeObject CpPStringAtom_Type = {
PyVarObject_HEAD_INIT(NULL, 0) _Cp_NameStr(CpPStringAtom_NAME),
.tp_basicsize = sizeof(CpPStringAtomObject),
.tp_dealloc = (destructor)cp_pstringatom_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = NULL,
.tp_members = CpPStringAtom_Members,
.tp_init = (initproc)cp_pstringatom_init,
Expand Down
4 changes: 1 addition & 3 deletions test/_C/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

if caterpillar.native_support():

from caterpillar._C import atom, catom, fieldatom, fieldcatom
from caterpillar._C import atom, catom


def test_atom_init():
Expand All @@ -26,8 +26,6 @@ class Foo(atom):

assert issubclass(Foo, atom)
assert issubclass(catom, atom)
assert issubclass(fieldatom, atom)
assert issubclass(fieldcatom, catom)


def test_atom_methods():
Expand Down
100 changes: 0 additions & 100 deletions test/_C/test_field.py

This file was deleted.

19 changes: 11 additions & 8 deletions test/_C/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

if caterpillar.native_support():

from caterpillar._C import atom, typeof, Field, sizeof, patom
from caterpillar._C import atom, typeof, sizeof, patom, repeated
from caterpillar._C import switch
from caterpillar._C import unpack, layer, Struct, pack, ContextPath


Expand Down Expand Up @@ -42,16 +43,16 @@ def test_typeof():

# That will change if we have a field with a length or a
# switch statement.
field = Field(f, length=2)
field = repeated(f, 2)
assert typeof(field) == typing.List[str]

field2 = Field(f, switch={1: atom()})
field2 = switch(f, {1: atom()})[2]
# We can't know the type of a switch atom which does
# not implement the __type__ method
assert typeof(Field(atom())) == typing.Any
assert typeof(field2) == typing.Union[typing.Any, str]
assert typeof(atom()) == typing.Any
assert typeof(field2) == typing.List[typing.Any]
# switch and length can be combined as well
assert typeof(field2[2]) == typing.List[typing.Union[typing.Any, str]]
assert typeof(field2[2]) == typing.List[typing.List[typing.Any]]


def test_sizeof():
Expand All @@ -73,8 +74,10 @@ def test_sizeof():
# Calculation is done by first evaluating the length of
# the field's atom and then multiply the length of the evalutated
# switch atom by the field's length.
field = Field(b, length=2, switch=(lambda ctx: Bar()))
assert sizeof(field) == 2 + (2 * 2)
# REVISIT: switch atoms does not have a static size
with pytest.raises(TypeError):
field = switch(b, (lambda ctx: Bar()))[2]
assert sizeof(field) == 2 + (2 * 2)


def test_unpack_basic():
Expand Down

0 comments on commit a1cde82

Please sign in to comment.