7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- Main code ---------------------------------------------------------*/
35 PyTypeObject *bytestring_pytype;
37 PyObject *bytestring_pywrap(const void *p, size_t n)
39 PyStringObject *x = PyObject_NewVar(PyStringObject, bytestring_pytype, n);
40 if (p) memcpy(x->ob_sval, p, n);
46 x->ob_sinterned = NULL;
48 return ((PyObject *)x);
51 PyObject *bytestring_pywrapbuf(buf *b)
52 { return bytestring_pywrap(BCUR(b), BLEFT(b)); }
54 #define BINOP(name, op) \
55 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
56 const void *xv, *yv; \
57 const unsigned char *xp, *yp; \
62 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
63 PyObject_AsReadBuffer(y, &yv, &ysz)) \
65 if (xsz != ysz) VALERR("length mismatch"); \
66 rc = bytestring_pywrap(0, xsz); \
67 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
68 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
76 #define UNOP(name, op) \
77 static PyObject *bytestring_py##name(PyObject *x) { \
79 const unsigned char *xp; \
84 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
85 rc = bytestring_pywrap(0, xsz); \
86 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
87 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
93 static PyNumberMethods bytestring_pynumber = {
95 0, /* @nb_subtract@ */
96 0, /* @nb_multiply@ */
98 0, /* @nb_remainder@ */
101 0, /* @nb_negative@ */
102 0, /* @nb_positive@ */
103 0, /* @nb_absolute@ */
104 0, /* @nb_nonzero@ */
105 bytestring_pynot, /* @nb_invert@ */
108 bytestring_pyand, /* @nb_and@ */
109 bytestring_pyxor, /* @nb_xor@ */
110 bytestring_pyor, /* @nb_or@ */
119 static PyBufferProcs bytestring_pybuffer;
121 static PyTypeObject bytestring_pytype_skel = {
122 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
123 "catacomb.ByteString", /* @tp_name@ */
124 0, /* @tp_basicsize@ */
125 0, /* @tp_itemsize@ */
127 0, /* @tp_dealloc@ */
129 0, /* @tp_getattr@ */
130 0, /* @tp_setattr@ */
131 0, /* @tp_compare@ */
133 &bytestring_pynumber, /* @tp_as_number@ */
134 0, /* @tp_as_sequence@ */
135 0, /* @tp_as_mapping@ */
139 0, /* @tp_getattro@ */
140 0, /* @tp_setattro@ */
141 &bytestring_pybuffer, /* @tp_as_buffer@ */
142 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
143 Py_TPFLAGS_CHECKTYPES |
147 "Byte string class.",
149 0, /* @tp_traverse@ */
151 0, /* @tp_richcompare@ */
152 0, /* @tp_weaklistoffset@ */
154 0, /* @tp_iternexr@ */
155 0, /* @tp_methods@ */
156 0, /* @tp_members@ */
160 0, /* @tp_descr_get@ */
161 0, /* @tp_descr_set@ */
162 0, /* @tp_dictoffset@ */
164 PyType_GenericAlloc, /* @tp_alloc@ */
166 _PyObject_Del, /* @tp_free@ */
170 /*----- Initialization ----------------------------------------------------*/
172 #define string_pytype &PyString_Type
173 void bytestring_pyinit(void)
175 INITTYPE(bytestring, string);
178 void bytestring_pyinsert(PyObject *mod)
180 INSERT("ByteString", bytestring_pytype);
183 /*----- That's all, folks -------------------------------------------------*/