1992-08-17 05:55:12 -03:00
|
|
|
/***********************************************************
|
1995-01-04 15:10:35 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1992-08-17 05:55:12 -03:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-08-17 05:55:12 -03:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* struct module -- pack values into and (out of) strings */
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
#include "Python.h"
|
1992-08-17 05:55:12 -03:00
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
static PyObject *StructError;
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Define various structs to figure out the alignments of types */
|
|
|
|
|
1995-02-02 10:29:10 -04:00
|
|
|
#ifdef __MWERKS__
|
|
|
|
/*
|
|
|
|
** XXXX We have a problem here. There are no unique alignment rules
|
|
|
|
** on the PowerPC mac.
|
|
|
|
*/
|
|
|
|
#ifdef __powerc
|
|
|
|
#pragma options align=mac68k
|
|
|
|
#endif
|
|
|
|
#endif /* __MWERKS__ */
|
|
|
|
|
1992-08-17 05:55:12 -03:00
|
|
|
typedef struct { char c; short x; } s_short;
|
|
|
|
typedef struct { char c; int x; } s_int;
|
|
|
|
typedef struct { char c; long x; } s_long;
|
|
|
|
typedef struct { char c; float x; } s_float;
|
|
|
|
typedef struct { char c; double x; } s_double;
|
|
|
|
|
|
|
|
#define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
|
|
|
|
#define INT_ALIGN (sizeof(s_int) - sizeof(int))
|
|
|
|
#define LONG_ALIGN (sizeof(s_long) - sizeof(long))
|
|
|
|
#define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
|
|
|
|
#define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
|
|
|
|
|
1995-02-02 10:29:10 -04:00
|
|
|
#ifdef __powerc
|
|
|
|
#pragma options align=reset
|
|
|
|
#endif
|
|
|
|
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
/* Align a size according to a format code */
|
|
|
|
|
|
|
|
static int
|
|
|
|
align(size, c)
|
|
|
|
int size;
|
|
|
|
int c;
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h': a = SHORT_ALIGN; break;
|
|
|
|
case 'i': a = INT_ALIGN; break;
|
|
|
|
case 'l': a = LONG_ALIGN; break;
|
|
|
|
case 'f': a = FLOAT_ALIGN; break;
|
|
|
|
case 'd': a = DOUBLE_ALIGN; break;
|
|
|
|
default: return size;
|
|
|
|
}
|
|
|
|
return (size + a - 1) / a * a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* calculate the size of a format string */
|
|
|
|
|
|
|
|
static int
|
|
|
|
calcsize(fmt)
|
|
|
|
char *fmt;
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char c;
|
|
|
|
int size, num, itemsize, x;
|
|
|
|
|
|
|
|
s = fmt;
|
|
|
|
size = 0;
|
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
if ('0' <= c && c <= '9') {
|
|
|
|
num = c - '0';
|
|
|
|
while ('0' <= (c = *s++) && c <= '9') {
|
|
|
|
x = num*10 + (c - '0');
|
|
|
|
if (x/10 != num) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"int overflow in fmt");
|
1992-08-17 05:55:12 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
num = x;
|
|
|
|
}
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
num = 1;
|
|
|
|
|
|
|
|
size = align(size, c);
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'x': /* pad byte */
|
|
|
|
case 'b': /* byte-sized int */
|
|
|
|
case 'c': /* char */
|
|
|
|
itemsize = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h': /* short ("half-size)" int */
|
|
|
|
itemsize = sizeof(short);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i': /* natural-size int */
|
|
|
|
itemsize = sizeof(int);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* long int */
|
|
|
|
itemsize = sizeof(long);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f': /* float */
|
|
|
|
itemsize = sizeof(float);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': /* double */
|
|
|
|
itemsize = sizeof(double);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError, "bad char in fmt");
|
1992-08-17 05:55:12 -03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
x = num * itemsize;
|
|
|
|
size += x;
|
|
|
|
if (x/itemsize != num || size < 0) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"total struct size too long");
|
1992-08-17 05:55:12 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* pack(fmt, v1, v2, ...) --> string */
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
static PyObject *
|
1992-08-17 05:55:12 -03:00
|
|
|
struct_calcsize(self, args)
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-08-17 05:55:12 -03:00
|
|
|
{
|
|
|
|
char *fmt;
|
|
|
|
int size;
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
if (!PyArg_Parse(args, "s", &fmt))
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
size = calcsize(fmt);
|
|
|
|
if (size < 0)
|
|
|
|
return NULL;
|
1996-12-12 19:32:31 -04:00
|
|
|
return PyInt_FromLong((long)size);
|
1992-08-17 05:55:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* pack(fmt, v1, v2, ...) --> string */
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
static PyObject *
|
1992-08-17 05:55:12 -03:00
|
|
|
struct_pack(self, args)
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-08-17 05:55:12 -03:00
|
|
|
{
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *format, *result, *v;
|
1992-08-17 05:55:12 -03:00
|
|
|
char *fmt;
|
|
|
|
int size, num;
|
|
|
|
int i, n;
|
|
|
|
char *s, *res, *restart;
|
|
|
|
char c;
|
|
|
|
long ival;
|
|
|
|
double fval;
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
if (args == NULL || !PyTuple_Check(args) ||
|
|
|
|
(n = PyTuple_Size(args)) < 1)
|
|
|
|
{
|
|
|
|
PyErr_BadArgument();
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
format = PyTuple_GetItem(args, 0);
|
|
|
|
if (!PyArg_Parse(format, "s", &fmt))
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
size = calcsize(fmt);
|
|
|
|
if (size < 0)
|
|
|
|
return NULL;
|
1996-12-12 19:32:31 -04:00
|
|
|
result = PyString_FromStringAndSize((char *)NULL, size);
|
1992-08-17 05:55:12 -03:00
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = fmt;
|
|
|
|
i = 1;
|
1996-12-12 19:32:31 -04:00
|
|
|
res = restart = PyString_AsString(result);
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
if ('0' <= c && c <= '9') {
|
|
|
|
num = c - '0';
|
|
|
|
while ('0' <= (c = *s++) && c <= '9')
|
|
|
|
num = num*10 + (c - '0');
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
num = 1;
|
|
|
|
|
|
|
|
res = restart + align((int)(res-restart), c);
|
|
|
|
|
|
|
|
while (--num >= 0) {
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'x': /* pad byte */
|
|
|
|
*res++ = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
case 'i':
|
|
|
|
case 'h':
|
|
|
|
case 'b':
|
|
|
|
if (i >= n) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"insufficient arguments to pack");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyTuple_GetItem(args, i++);
|
|
|
|
if (!PyInt_Check(v)) {
|
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"bad argument type to pack");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
ival = PyInt_AsLong(v);
|
1992-08-17 05:55:12 -03:00
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
*res++ = ival;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
*(short*)res = ival;
|
|
|
|
res += sizeof(short);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
*(int*)res = ival;
|
|
|
|
res += sizeof(int);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
*(long*)res = ival;
|
|
|
|
res += sizeof(long);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
if (i >= n) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"insufficient arguments to pack");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyTuple_GetItem(args, i++);
|
|
|
|
if (!PyString_Check(v) ||
|
|
|
|
PyString_Size(v) != 1)
|
|
|
|
{
|
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"bad argument type to pack");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
*res++ = PyString_AsString(v)[0];
|
1992-08-17 05:55:12 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
case 'f':
|
|
|
|
if (i >= n) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"insufficient arguments to pack");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyTuple_GetItem(args, i++);
|
|
|
|
if (!PyFloat_Check(v)) {
|
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"bad argument type to pack");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
fval = PyFloat_AsDouble(v);
|
1992-08-17 05:55:12 -03:00
|
|
|
switch (c) {
|
|
|
|
case 'f':
|
|
|
|
*(float*)res = (float)fval;
|
|
|
|
res += sizeof(float);
|
|
|
|
break;
|
|
|
|
case 'd':
|
1995-01-04 15:10:35 -04:00
|
|
|
memcpy(res, (char*)&fval, sizeof fval);
|
1992-08-17 05:55:12 -03:00
|
|
|
res += sizeof(double);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"bad char in fmt");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < n) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"too many arguments for pack fmt");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
1996-12-12 19:32:31 -04:00
|
|
|
Py_DECREF(result);
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-08-19 13:44:15 -03:00
|
|
|
/* Helper to convert a list to a tuple */
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
static PyObject *
|
1992-08-19 13:44:15 -03:00
|
|
|
totuple(list)
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *list;
|
1992-08-19 13:44:15 -03:00
|
|
|
{
|
1996-12-12 19:32:31 -04:00
|
|
|
int len = PyList_Size(list);
|
|
|
|
PyObject *tuple = PyTuple_New(len);
|
1992-08-19 13:44:15 -03:00
|
|
|
if (tuple != NULL) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *v = PyList_GetItem(list, i);
|
|
|
|
Py_INCREF(v);
|
|
|
|
PyTuple_SetItem(tuple, i, v);
|
1992-08-19 13:44:15 -03:00
|
|
|
}
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
Py_DECREF(list);
|
1992-08-19 13:44:15 -03:00
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-08-17 05:55:12 -03:00
|
|
|
/* unpack(fmt, string) --> (v1, v2, ...) */
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
static PyObject *
|
1992-08-17 05:55:12 -03:00
|
|
|
struct_unpack(self, args)
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-08-17 05:55:12 -03:00
|
|
|
{
|
|
|
|
char *str, *start, *fmt, *s;
|
|
|
|
char c;
|
|
|
|
int len, size, num, x;
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *res, *v;
|
1992-08-17 05:55:12 -03:00
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
if (!PyArg_Parse(args, "(ss#)", &fmt, &start, &len))
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
size = calcsize(fmt);
|
|
|
|
if (size != len) {
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"unpack str size does not match fmt");
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
res = PyList_New(0);
|
1992-08-17 05:55:12 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
str = start;
|
|
|
|
s = fmt;
|
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
if ('0' <= c && c <= '9') {
|
|
|
|
num = c - '0';
|
|
|
|
while ('0' <= (c = *s++) && c <= '9')
|
|
|
|
num = num*10 + (c - '0');
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
num = 1;
|
|
|
|
|
|
|
|
str = start + align((int)(str-start), c);
|
|
|
|
|
|
|
|
while (--num >= 0) {
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
str++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
x = *str++;
|
|
|
|
if (x >= 128)
|
|
|
|
x -= 256;
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyInt_FromLong((long)x);
|
1992-08-17 05:55:12 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyString_FromStringAndSize(str, 1);
|
1992-08-17 05:55:12 -03:00
|
|
|
str++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyInt_FromLong((long)*(short*)str);
|
1992-08-17 05:55:12 -03:00
|
|
|
str += sizeof(short);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyInt_FromLong((long)*(int*)str);
|
1992-08-17 05:55:12 -03:00
|
|
|
str += sizeof(int);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyInt_FromLong(*(long*)str);
|
1992-08-17 05:55:12 -03:00
|
|
|
str += sizeof(long);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyFloat_FromDouble((double)*(float*)str);
|
1992-08-17 05:55:12 -03:00
|
|
|
str += sizeof(float);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
1995-01-04 15:10:35 -04:00
|
|
|
{
|
|
|
|
double d;
|
|
|
|
memcpy((char *)&d, str, sizeof d);
|
1996-12-12 19:32:31 -04:00
|
|
|
v = PyFloat_FromDouble(d);
|
1992-08-17 05:55:12 -03:00
|
|
|
str += sizeof(double);
|
|
|
|
break;
|
1995-01-04 15:10:35 -04:00
|
|
|
}
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
default:
|
1996-12-12 19:32:31 -04:00
|
|
|
PyErr_SetString(StructError,
|
|
|
|
"bad char in fmt");
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
}
|
1996-12-12 19:32:31 -04:00
|
|
|
if (v == NULL || PyList_Append(res, v) < 0)
|
1992-08-17 05:55:12 -03:00
|
|
|
goto fail;
|
1996-12-12 19:32:31 -04:00
|
|
|
Py_DECREF(v);
|
1992-08-17 05:55:12 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-08-19 13:44:15 -03:00
|
|
|
return totuple(res);
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
fail:
|
1996-12-12 19:32:31 -04:00
|
|
|
Py_DECREF(res);
|
1992-08-17 05:55:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1992-08-19 13:44:15 -03:00
|
|
|
|
1992-08-17 05:55:12 -03:00
|
|
|
/* List of functions */
|
|
|
|
|
1996-12-12 19:32:31 -04:00
|
|
|
static PyMethodDef struct_methods[] = {
|
1992-08-17 05:55:12 -03:00
|
|
|
{"calcsize", struct_calcsize},
|
|
|
|
{"pack", struct_pack, 1/*varargs*/},
|
|
|
|
{"unpack", struct_unpack},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Module initialization */
|
|
|
|
|
|
|
|
void
|
|
|
|
initstruct()
|
|
|
|
{
|
1996-12-12 19:32:31 -04:00
|
|
|
PyObject *m, *d;
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
1996-12-12 19:32:31 -04:00
|
|
|
m = Py_InitModule("struct", struct_methods);
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
1996-12-12 19:32:31 -04:00
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
StructError = PyString_FromString("struct.error");
|
|
|
|
PyDict_SetItemString(d, "error", StructError);
|
1992-08-17 05:55:12 -03:00
|
|
|
|
|
|
|
/* Check for errors */
|
1996-12-12 19:32:31 -04:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
Py_FatalError("can't initialize module struct");
|
1992-08-17 05:55:12 -03:00
|
|
|
}
|