Mercurial > cpython-lru_cache-weakref
annotate Modules/_xdrmodule.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | a3926484a658 |
| children | ddea69f9071b |
| rev | line source |
|---|---|
| 4048 | 1 /* This module exports part of the C API to the XDR routines into Python. |
| 2 * XDR is Sun's eXternal Data Representation, as described in RFC 1014. This | |
| 3 * module is used by xdrlib.py to support the float and double data types | |
| 4 * which are too much of a pain to support in Python directly. It is | |
| 5 * not required by xdrlib.py -- when not available, these types aren't | |
| 6 * supported at the Python layer. Note that representations that can be | |
| 7 * implemented solely in Python, are *not* reproduced here. | |
| 8 * | |
| 9 * Module version number: 1.0 | |
| 10 * | |
| 11 * See xdrlib.py for usage notes. | |
| 12 * | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
13 * Note: this has so far, only been tested on Solaris 2.5 and IRIX 5.3. On |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
14 * these systems, you will need to link with -lnsl for these symbols to be |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
15 * defined. |
| 4048 | 16 */ |
| 17 | |
| 18 #include "Python.h" | |
| 19 | |
| 20 #include <rpc/rpc.h> | |
| 21 #include <rpc/xdr.h> | |
| 22 | |
| 23 static PyObject* xdr_error; | |
| 24 | |
| 25 | |
| 26 | |
| 27 static PyObject* | |
| 28 pack_float(self, args) | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
29 PyObject* self; |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
30 PyObject* args; |
| 4048 | 31 { |
| 32 XDR xdr; | |
| 33 float value; | |
| 34 union { /* guarantees proper alignment */ | |
| 35 long dummy; | |
| 36 char buffer[4]; | |
| 37 } addr; | |
| 38 PyObject* rtn = NULL; | |
| 39 | |
| 40 if (!PyArg_ParseTuple(args, "f", &value)) | |
| 41 return NULL; | |
| 42 | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
43 xdr.x_ops = NULL; |
| 4048 | 44 xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE); |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
45 if( xdr.x_ops == NULL ) |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
46 PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
47 else if (xdr_float(&xdr, &value)) |
| 4048 | 48 rtn = PyString_FromStringAndSize(addr.buffer, 4); |
| 49 else | |
| 50 PyErr_SetString(xdr_error, "conversion from float failed"); | |
| 51 | |
| 52 xdr_destroy(&xdr); | |
| 53 return rtn; | |
| 54 } | |
| 55 | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
56 |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
57 |
| 4048 | 58 static PyObject* |
| 59 pack_double(self, args) | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
60 PyObject* self; |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
61 PyObject* args; |
| 4048 | 62 { |
| 63 XDR xdr; | |
| 64 double value; | |
| 65 union { /* guarantees proper alignment */ | |
| 66 long dummy; | |
| 67 char buffer[8]; | |
| 68 } addr; | |
| 69 PyObject* rtn = NULL; | |
| 70 | |
| 71 if (!PyArg_ParseTuple(args, "d", &value)) | |
| 72 return NULL; | |
| 73 | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
74 xdr.x_ops = NULL; |
| 4048 | 75 xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE); |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
76 if( xdr.x_ops == NULL ) |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
77 PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
78 else if (xdr_double(&xdr, &value)) |
| 4048 | 79 rtn = PyString_FromStringAndSize(addr.buffer, 8); |
| 80 else | |
| 81 PyErr_SetString(xdr_error, "conversion from double failed"); | |
| 82 | |
| 83 xdr_destroy(&xdr); | |
| 84 return rtn; | |
| 85 } | |
| 86 | |
| 87 | |
| 88 | |
| 89 static PyObject* | |
| 90 unpack_float(self, args) | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
91 PyObject* self; |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
92 PyObject* args; |
| 4048 | 93 { |
| 94 XDR xdr; | |
| 95 float value; | |
| 96 char* string; | |
| 97 int strlen; | |
| 98 PyObject* rtn = NULL; | |
| 99 | |
| 100 if (!PyArg_ParseTuple(args, "s#", &string, &strlen)) | |
| 101 return NULL; | |
| 102 | |
| 103 if (strlen != 4) { | |
| 104 PyErr_SetString(PyExc_ValueError, "4 byte string expected"); | |
| 105 return NULL; | |
| 106 } | |
| 107 | |
| 108 /* Python guarantees that the string is 4 byte aligned */ | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
109 xdr.x_ops = NULL; |
| 4048 | 110 xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE); |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
111 if( xdr.x_ops == NULL ) |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
112 PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
113 else if (xdr_float(&xdr, &value)) |
| 4048 | 114 rtn = Py_BuildValue("f", value); |
| 115 else | |
| 116 PyErr_SetString(xdr_error, "conversion to float failed"); | |
| 117 | |
| 118 xdr_destroy(&xdr); | |
| 119 return rtn; | |
| 120 } | |
| 121 | |
| 122 | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
123 |
| 4048 | 124 static PyObject* |
| 125 unpack_double(self, args) | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
126 PyObject* self; |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
127 PyObject* args; |
| 4048 | 128 { |
| 129 XDR xdr; | |
| 130 double value; | |
| 131 char* string; | |
| 132 int strlen; | |
|
4182
a3926484a658
#include <netconfig.h> seems not needed and is harmful on NeXT, so drop it.
Guido van Rossum <guido@python.org>
parents:
4062
diff
changeset
|
133 PyObject* rtn = NULL; |
| 4048 | 134 |
| 135 if (!PyArg_ParseTuple(args, "s#", &string, &strlen)) | |
| 136 return NULL; | |
| 137 | |
| 138 if (strlen != 8) { | |
| 139 PyErr_SetString(PyExc_ValueError, "8 byte string expected"); | |
| 140 return NULL; | |
| 141 } | |
| 142 | |
| 143 /* Python guarantees that the string is 4 byte aligned */ | |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
144 xdr.x_ops = NULL; |
| 4048 | 145 xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE); |
|
4062
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
146 if( xdr.x_ops == NULL ) |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
147 PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
|
d6de2c148819
Test for xdrmem_create() failure.
Guido van Rossum <guido@python.org>
parents:
4048
diff
changeset
|
148 else if (xdr_double(&xdr, &value)) |
| 4048 | 149 rtn = Py_BuildValue("d", value); |
| 150 else | |
| 151 PyErr_SetString(xdr_error, "conversion to double failed"); | |
| 152 | |
| 153 xdr_destroy(&xdr); | |
| 154 return rtn; | |
| 155 } | |
| 156 | |
| 157 | |
| 158 | |
| 159 static struct PyMethodDef | |
| 160 xdr_methods[] = { | |
| 161 {"pack_float", pack_float, 1}, | |
| 162 {"pack_double", pack_double, 1}, | |
| 163 {"unpack_float", unpack_float, 1}, | |
| 164 {"unpack_double", unpack_double, 1}, | |
| 165 {NULL, NULL, 0} /* sentinel */ | |
| 166 }; | |
| 167 | |
| 168 | |
| 169 | |
| 170 void | |
| 171 init_xdr() | |
| 172 { | |
| 173 PyObject* module; | |
| 174 PyObject* dict; | |
| 175 | |
| 176 module = Py_InitModule("_xdr", xdr_methods); | |
| 177 dict = PyModule_GetDict(module); | |
| 178 | |
| 179 xdr_error = PyString_FromString("_xdr.error"); | |
| 180 PyDict_SetItemString(dict, "error", xdr_error); | |
| 181 } |
