diff options
Diffstat (limited to 'overlapped.c')
-rw-r--r-- | overlapped.c | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/overlapped.c b/overlapped.c index 6842efb..2c85676 100644 --- a/overlapped.c +++ b/overlapped.c @@ -31,6 +31,18 @@ #define T_HANDLE T_POINTER +#if PY_MAJOR_VERSION >= 3 +# define PYTHON3 +#endif + +#ifndef Py_MIN +# define Py_MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) +#endif + +#ifndef Py_MAX +# define Py_MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) +#endif + enum {TYPE_NONE, TYPE_NOT_STARTED, TYPE_READ, TYPE_WRITE, TYPE_ACCEPT, TYPE_CONNECT, TYPE_DISCONNECT, TYPE_CONNECT_NAMED_PIPE, TYPE_WAIT_NAMED_PIPE_AND_CONNECT}; @@ -69,6 +81,7 @@ SetFromWindowsErr(DWORD err) if (err == 0) err = GetLastError(); +#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3) || PY_MAJOR_VERSION > 3 switch (err) { case ERROR_CONNECTION_REFUSED: exception_type = PyExc_ConnectionRefusedError; @@ -79,6 +92,9 @@ SetFromWindowsErr(DWORD err) default: exception_type = PyExc_OSError; } +#else + exception_type = PyExc_WindowsError; +#endif return PyErr_SetExcFromWindowsErr(exception_type, err); } @@ -328,7 +344,11 @@ overlapped_CreateEvent(PyObject *self, PyObject *args) Py_UNICODE *Name; HANDLE Event; +#ifdef PYTHON3 if (!PyArg_ParseTuple(args, "O" F_BOOL F_BOOL "Z", +#else + if (!PyArg_ParseTuple(args, "O" F_BOOL F_BOOL "z", +#endif &EventAttributes, &ManualReset, &InitialState, &Name)) return NULL; @@ -805,7 +825,11 @@ Overlapped_WriteFile(OverlappedObject *self, PyObject *args) return NULL; } +#ifdef PYTHON3 if (!PyArg_Parse(bufobj, "y*", &self->write_buffer)) +#else + if (!PyArg_Parse(bufobj, "s*", &self->write_buffer)) +#endif return NULL; #if SIZEOF_SIZE_T > SIZEOF_LONG @@ -861,7 +885,11 @@ Overlapped_WSASend(OverlappedObject *self, PyObject *args) return NULL; } +#ifdef PYTHON3 if (!PyArg_Parse(bufobj, "y*", &self->write_buffer)) +#else + if (!PyArg_Parse(bufobj, "s*", &self->write_buffer)) +#endif return NULL; #if SIZEOF_SIZE_T > SIZEOF_LONG @@ -1328,6 +1356,7 @@ static PyMethodDef overlapped_functions[] = { {NULL} }; +#ifdef PYTHON3 static struct PyModuleDef overlapped_module = { PyModuleDef_HEAD_INIT, "_overlapped", @@ -1339,12 +1368,13 @@ static struct PyModuleDef overlapped_module = { NULL, NULL }; +#endif #define WINAPI_CONSTANT(fmt, con) \ PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con)) -PyMODINIT_FUNC -PyInit__overlapped(void) +PyObject* +_init_overlapped(void) { PyObject *m, *d; @@ -1360,7 +1390,11 @@ PyInit__overlapped(void) if (PyType_Ready(&OverlappedType) < 0) return NULL; +#ifdef PYTHON3 m = PyModule_Create(&overlapped_module); +#else + m = Py_InitModule("_overlapped", overlapped_functions); +#endif if (PyModule_AddObject(m, "Overlapped", (PyObject *)&OverlappedType) < 0) return NULL; @@ -1375,6 +1409,22 @@ PyInit__overlapped(void) WINAPI_CONSTANT(F_DWORD, SO_UPDATE_ACCEPT_CONTEXT); WINAPI_CONSTANT(F_DWORD, SO_UPDATE_CONNECT_CONTEXT); WINAPI_CONSTANT(F_DWORD, TF_REUSE_SOCKET); + WINAPI_CONSTANT(F_DWORD, ERROR_CONNECTION_REFUSED); + WINAPI_CONSTANT(F_DWORD, ERROR_CONNECTION_ABORTED); return m; } + +#ifdef PYTHON3 +PyMODINIT_FUNC +PyInit__overlapped(void) +{ + return _init_overlapped(); +} +#else +PyMODINIT_FUNC +init_overlapped(void) +{ + _init_overlapped(); +} +#endif |