-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyword.c
More file actions
117 lines (102 loc) · 2.92 KB
/
pyword.c
File metadata and controls
117 lines (102 loc) · 2.92 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "pyword.h"
/* Python object wrapper */
typedef struct {
PyObject_HEAD
PyWord word;
} PyWordObject;
/* ----- constructors / destructors ----- */
static PyObject *
pyword_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyWordObject *self = (PyWordObject *)type->tp_alloc(type, 0);
if (self) {
self->word.len = 0; /* start empty */
}
return (PyObject *)self;
}
static void
pyword_dealloc(PyWordObject *self)
{
Py_TYPE(self)->tp_free((PyObject *)self);
}
/* ----- methods ----- */
PyDoc_STRVAR(pyword_set_bytes_doc,
"set_bytes(data) -> None\n"
"Copy *data* (bytes-like) into the PyWord buffer (truncated to 64 B).");
static PyObject *
pyword_set_bytes(PyWordObject *self, PyObject *obj)
{
Py_buffer view;
if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) < 0)
return NULL;
uint8_t n = view.len > PYWORD_SIZE ? PYWORD_SIZE : view.len;
memcpy(self->word.data, view.buf, n);
self->word.len = n;
PyBuffer_Release(&view);
Py_RETURN_NONE;
}
PyDoc_STRVAR(pyword_get_bytes_doc,
"get_bytes() -> bytes\n"
"Return the current buffer as a Python bytes object.");
static PyObject *
pyword_get_bytes(PyWordObject *self)
{
return PyBytes_FromStringAndSize((char *)self->word.data, self->word.len);
}
PyDoc_STRVAR(pyword_len_doc,
"__len__() -> int\n"
"Number of bytes currently stored (0..64).");
static Py_ssize_t
slot_pyword_len(PyWordObject *self)
{
return self->word.len;
}
static PyMethodDef pyword_methods[] = {
{"set_bytes", (PyCFunction)pyword_set_bytes, METH_O, pyword_set_bytes_doc},
{"get_bytes", (PyCFunction)pyword_get_bytes, METH_NOARGS, pyword_get_bytes_doc},
{NULL, NULL} /* sentinel */
};
static PyTypeObject PyWordType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pyword.PyWord",
.tp_doc = "64-byte opaque blob with Python buffer protocol",
.tp_basicsize = sizeof(PyWordObject),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = pyword_new,
.tp_dealloc = (destructor)pyword_dealloc,
.tp_methods = pyword_methods,
.tp_as_sequence = NULL,
.tp_as_mapping = NULL,
.tp_as_buffer = NULL,
.tp_methods = pyword_methods,
.tp_dealloc = (destructor)pyword_dealloc,
.tp_as_sequence = &(PySequenceMethods){
.sq_length = slot_pyword_len,
},
};
/* ---------- module ---------- */
static struct PyModuleDef pywordmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "pyword",
.m_doc = "Minimal PyWord C extension",
.m_size = -1,
};
PyMODINIT_FUNC
PyInit_pyword(void)
{
PyObject *m;
if (PyType_Ready(&PyWordType) < 0)
return NULL;
m = PyModule_Create(&pywordmodule);
if (m == NULL)
return NULL;
Py_INCREF(&PyWordType);
if (PyModule_AddObject(m, "PyWord", (PyObject *)&PyWordType) < 0) {
Py_DECREF(&PyWordType);
Py_DECREF(m);
return NULL;
}
return m;
}