Commit a2dae6cb authored by Barry Warsaw's avatar Barry Warsaw

Updated to standard Python C coding style, and fixed a few error

checking nits.
parent b1a7d709
/* This module exports part of the C API to the XDR routines into Python. /* This module exports part of the C API to the XDR routines into Python.
* XDR is Sun's eXternal Data Representation, as described in RFC 1014. This * XDR is Sun's eXternal Data Representation, as described in RFC 1014.
* module is used by xdrlib.py to support the float and double data types * This module is used by xdrlib.py to support the float and double data
* which are too much of a pain to support in Python directly. It is * types which are too much of a pain to support in Python directly. It is
* not required by xdrlib.py -- when not available, these types aren't * not required by xdrlib.py -- when not available, these types aren't
* supported at the Python layer. Note that representations that can be * supported at the Python layer. Note that representations that can be
* implemented solely in Python, are *not* reproduced here. * implemented solely in Python, are *not* reproduced here.
...@@ -26,143 +26,147 @@ static PyObject* xdr_error; ...@@ -26,143 +26,147 @@ static PyObject* xdr_error;
static PyObject* static PyObject*
pack_float(self, args) pack_float(self, args)
PyObject* self; PyObject* self;
PyObject* args; PyObject* args;
{ {
XDR xdr; XDR xdr;
float value; float value;
union { /* guarantees proper alignment */ union { /* guarantees proper alignment */
long dummy; long dummy;
char buffer[4]; char buffer[4];
} addr; } addr;
PyObject* rtn = NULL; PyObject* rtn = NULL;
if (!PyArg_ParseTuple(args, "f", &value)) if (!PyArg_ParseTuple(args, "f", &value))
return NULL; return NULL;
xdr.x_ops = NULL; xdr.x_ops = NULL;
xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE); xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE);
if( xdr.x_ops == NULL ) if (xdr.x_ops == NULL)
PyErr_SetString(xdr_error, "XDR stream initialization failed."); PyErr_SetString(xdr_error,
else if (xdr_float(&xdr, &value)) "XDR stream initialization failed.");
rtn = PyString_FromStringAndSize(addr.buffer, 4); else if (xdr_float(&xdr, &value))
else rtn = PyString_FromStringAndSize(addr.buffer, 4);
PyErr_SetString(xdr_error, "conversion from float failed"); else
PyErr_SetString(xdr_error, "conversion from float failed");
xdr_destroy(&xdr);
return rtn; xdr_destroy(&xdr);
return rtn;
} }
static PyObject* static PyObject*
pack_double(self, args) pack_double(self, args)
PyObject* self; PyObject* self;
PyObject* args; PyObject* args;
{ {
XDR xdr; XDR xdr;
double value; double value;
union { /* guarantees proper alignment */ union { /* guarantees proper alignment */
long dummy; long dummy;
char buffer[8]; char buffer[8];
} addr; } addr;
PyObject* rtn = NULL; PyObject* rtn = NULL;
if (!PyArg_ParseTuple(args, "d", &value)) if (!PyArg_ParseTuple(args, "d", &value))
return NULL; return NULL;
xdr.x_ops = NULL; xdr.x_ops = NULL;
xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE); xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE);
if( xdr.x_ops == NULL ) if (xdr.x_ops == NULL)
PyErr_SetString(xdr_error, "XDR stream initialization failed."); PyErr_SetString(xdr_error,
else if (xdr_double(&xdr, &value)) "XDR stream initialization failed.");
rtn = PyString_FromStringAndSize(addr.buffer, 8); else if (xdr_double(&xdr, &value))
else rtn = PyString_FromStringAndSize(addr.buffer, 8);
PyErr_SetString(xdr_error, "conversion from double failed"); else
PyErr_SetString(xdr_error, "conversion from double failed");
xdr_destroy(&xdr);
return rtn; xdr_destroy(&xdr);
return rtn;
} }
static PyObject* static PyObject*
unpack_float(self, args) unpack_float(self, args)
PyObject* self; PyObject* self;
PyObject* args; PyObject* args;
{ {
XDR xdr; XDR xdr;
float value; float value;
char* string; char* string;
int strlen; int strlen;
PyObject* rtn = NULL; PyObject* rtn = NULL;
if (!PyArg_ParseTuple(args, "s#", &string, &strlen)) if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
return NULL; return NULL;
if (strlen != 4) { if (strlen != 4) {
PyErr_SetString(PyExc_ValueError, "4 byte string expected"); PyErr_SetString(PyExc_ValueError, "4 byte string expected");
return NULL; return NULL;
} }
/* Python guarantees that the string is 4 byte aligned */ /* Python guarantees that the string is 4 byte aligned */
xdr.x_ops = NULL; xdr.x_ops = NULL;
xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE); xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE);
if( xdr.x_ops == NULL ) if (xdr.x_ops == NULL)
PyErr_SetString(xdr_error, "XDR stream initialization failed."); PyErr_SetString(xdr_error,
else if (xdr_float(&xdr, &value)) "XDR stream initialization failed.");
rtn = Py_BuildValue("f", value); else if (xdr_float(&xdr, &value))
else rtn = Py_BuildValue("f", value);
PyErr_SetString(xdr_error, "conversion to float failed"); else
PyErr_SetString(xdr_error, "conversion to float failed");
xdr_destroy(&xdr);
return rtn; xdr_destroy(&xdr);
return rtn;
} }
static PyObject* static PyObject*
unpack_double(self, args) unpack_double(self, args)
PyObject* self; PyObject* self;
PyObject* args; PyObject* args;
{ {
XDR xdr; XDR xdr;
double value; double value;
char* string; char* string;
int strlen; int strlen;
PyObject* rtn = NULL; PyObject* rtn = NULL;
if (!PyArg_ParseTuple(args, "s#", &string, &strlen)) if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
return NULL; return NULL;
if (strlen != 8) { if (strlen != 8) {
PyErr_SetString(PyExc_ValueError, "8 byte string expected"); PyErr_SetString(PyExc_ValueError, "8 byte string expected");
return NULL; return NULL;
} }
/* Python guarantees that the string is 4 byte aligned */ /* Python guarantees that the string is 4 byte aligned */
xdr.x_ops = NULL; xdr.x_ops = NULL;
xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE); xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE);
if( xdr.x_ops == NULL ) if (xdr.x_ops == NULL)
PyErr_SetString(xdr_error, "XDR stream initialization failed."); PyErr_SetString(xdr_error,
else if (xdr_double(&xdr, &value)) "XDR stream initialization failed.");
rtn = Py_BuildValue("d", value); else if (xdr_double(&xdr, &value))
else rtn = Py_BuildValue("d", value);
PyErr_SetString(xdr_error, "conversion to double failed"); else
PyErr_SetString(xdr_error, "conversion to double failed");
xdr_destroy(&xdr);
return rtn; xdr_destroy(&xdr);
return rtn;
} }
static struct PyMethodDef static struct PyMethodDef
xdr_methods[] = { xdr_methods[] = {
{"pack_float", pack_float, 1}, {"pack_float", pack_float, 1},
{"pack_double", pack_double, 1}, {"pack_double", pack_double, 1},
{"unpack_float", unpack_float, 1}, {"unpack_float", unpack_float, 1},
{"unpack_double", unpack_double, 1}, {"unpack_double", unpack_double, 1},
{NULL, NULL, 0} /* sentinel */ {NULL, NULL, 0} /* sentinel */
}; };
...@@ -170,12 +174,14 @@ xdr_methods[] = { ...@@ -170,12 +174,14 @@ xdr_methods[] = {
void void
init_xdr() init_xdr()
{ {
PyObject* module; PyObject* module;
PyObject* dict; PyObject* dict;
module = Py_InitModule("_xdr", xdr_methods); module = Py_InitModule("_xdr", xdr_methods);
dict = PyModule_GetDict(module); dict = PyModule_GetDict(module);
xdr_error = PyString_FromString("_xdr.error"); xdr_error = PyString_FromString("_xdr.error");
PyDict_SetItemString(dict, "error", xdr_error); PyDict_SetItemString(dict, "error", xdr_error);
if (PyErr_Occurred())
Py_FatalError("can't initialize module _xdr");
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment