11#include <Python.h>
22
33struct cstring {
4- PyObject_HEAD
4+ PyObject_VAR_HEAD
5+ char value [];
6+ };
7+
8+ #define CSTRING_VALUE (self ) (((struct cstring *)self)->value)
9+
10+ static PyObject * cstring_new (PyTypeObject * type , PyObject * args , PyObject * * kwargs ) {
11+ char * value = NULL ;
12+
13+ if (!PyArg_ParseTuple (args , "s" , & value ))
14+ return NULL ;
15+
16+ int size = strlen (value ) + 1 ;
17+
18+ struct cstring * new = type -> tp_alloc (type , size );
19+ memcpy (new -> value , value , size );
20+ return (PyObject * )new ;
21+ }
22+
23+ static void cstring_dealloc (PyObject * self ) {
24+ Py_TYPE (self )-> tp_free (self );
25+ }
26+
27+ static PyObject * cstring_str (PyObject * self ) {
28+ return PyUnicode_FromString (CSTRING_VALUE (self ));
29+ }
30+
31+ static PyTypeObject cstring_type = {
32+ PyVarObject_HEAD_INIT (NULL , 0 )
33+ .tp_name = "cstring" ,
34+ .tp_doc = "" ,
35+ .tp_basicsize = sizeof (struct cstring ),
36+ .tp_itemsize = sizeof (char ),
37+ .tp_flags = Py_TPFLAGS_DEFAULT ,
38+ .tp_new = cstring_new ,
39+ .tp_dealloc = cstring_dealloc ,
40+ .tp_str = cstring_str ,
541};
642
743static struct PyModuleDef module = {
@@ -13,5 +49,10 @@ static struct PyModuleDef module = {
1349};
1450
1551PyMODINIT_FUNC PyInit_cstring (void ) {
16- return PyModule_Create (& module );
52+ if (PyType_Ready (& cstring_type ) < 0 )
53+ return NULL ;
54+ Py_INCREF (& cstring_type );
55+ PyObject * m = PyModule_Create (& module );
56+ PyModule_AddObject (m , "cstring" , (PyObject * )& cstring_type );
57+ return m ;
1758}
0 commit comments