[pywin32-checkins] pywin32/win32/src timermodule.cpp,1.4,1.5
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Roger U. <ru...@us...> - 2007-08-13 02:11:41
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12262/win32/src Modified Files: timermodule.cpp Log Message: Fix 64-bit issues Add autoduck and docstrings Whitespace consistency Index: timermodule.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/timermodule.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** timermodule.cpp 12 Jul 2006 12:29:17 -0000 1.4 --- timermodule.cpp 13 Aug 2007 02:11:41 -0000 1.5 *************** *** 7,10 **** --- 7,12 ---- // + // @doc - Contains autoduck comments for documentation + #include "windows.h" #include "Python.h" *************** *** 12,127 **** //#include "abstract.h" - static PyObject *timer_module_error; static PyObject *timer_id_callback_map = NULL; VOID CALLBACK ! py_win32_timer_callback (HWND hwnd, UINT msg, UINT event, DWORD time) { - // do we have a valid callback dictionary? - - if (timer_id_callback_map) { CEnterLeavePython _celp; ! PyObject * py_event = Py_BuildValue ("i", (int) event); ! ! // is this timer id recognized? ! PyObject * callback_function = \ ! PyDict_GetItem (timer_id_callback_map, py_event); ! // call the user's function ! if (callback_function) { ! // create a 'death grip' on the callback function, just incase ! // the callback itself removes the function from the map. ! Py_INCREF(callback_function); ! PyObject * callback_args = Py_BuildValue ("(il)", (int) event, (long) time); ! PyObject * result = \ ! PyEval_CallObject (callback_function, callback_args); ! if (!result) { // Is this necessary, or will python already have flagged // an exception? Can we even catch exceptions here? PyErr_Print(); ! } ! // everything's ok, return ! Py_DECREF(callback_function); ! Py_XDECREF(callback_args); ! Py_XDECREF(result); ! Py_DECREF (py_event); ! return; ! } ! // invalid key or callback: kill the timer (note there is no ! // key to remove - we have already determined it is not there!) ! Py_DECREF (py_event); ! ::KillTimer (NULL, event); return; - } else { - // the id/callback map is NULL - ::KillTimer (NULL, event); - } } static PyObject * py_timer_set_timer (PyObject * self, PyObject * args) { ! PyObject *callback; ! PyObject * py_timer_id; ! int elapse; ! UINT timer_id; ! ! if (!PyArg_ParseTuple (args, "iO", &elapse, &callback)) { ! return NULL; ! } ! ! // make sure the callback is a valid callable object ! if (!PyCallable_Check (callback)) { ! PyErr_SetString (timer_module_error, "argument must be a callable object"); ! return NULL; ! } ! // create the win32 timer ! Py_BEGIN_ALLOW_THREADS; ! timer_id = ::SetTimer (NULL, 0, (UINT) elapse, (TIMERPROC) py_win32_timer_callback); ! Py_END_ALLOW_THREADS; ! if (!timer_id) { ! PyErr_SetString (timer_module_error, "win32 SetTimer() failed"); ! return NULL; ! } ! py_timer_id = PyInt_FromLong((long) timer_id); ! if (!py_timer_id) ! return NULL; ! // associate the timer id with the given callback function ! if (PyObject_SetItem (timer_id_callback_map, py_timer_id, callback) == -1) { ! ::KillTimer (NULL, timer_id); ! Py_DECREF(py_timer_id); ! PyErr_SetString (timer_module_error, ! "internal error, couldn't set timer id callback item"); ! return NULL; ! } ! // everything went ok. ! return py_timer_id; } static PyObject * py_timer_kill_timer (PyObject * self, PyObject * args) { ! PyObject * py_timer_id; ! if (!PyArg_ParseTuple (args, "O", &py_timer_id)) { ! return NULL; ! } else if (timer_id_callback_map) { ! if (0 != PyDict_DelItem (timer_id_callback_map, py_timer_id)) { ! return NULL; ! } ! } ! int rc; ! Py_BEGIN_ALLOW_THREADS; ! rc = ::KillTimer (NULL, (int) PyInt_AsLong (py_timer_id)); ! Py_END_ALLOW_THREADS; ! return Py_BuildValue ("i", rc); } --- 14,124 ---- //#include "abstract.h" static PyObject *timer_id_callback_map = NULL; VOID CALLBACK ! py_win32_timer_callback (HWND hwnd, UINT msg, UINT_PTR timer_id, DWORD time) { CEnterLeavePython _celp; ! PyObject * py_timer_id = PyWinLong_FromVoidPtr((void *)timer_id); ! if (!py_timer_id){ ! PyErr_Print(); ! return; ! } ! // is this timer id recognized? ! PyObject * callback_function = PyDict_GetItem (timer_id_callback_map, py_timer_id); ! if (!callback_function){ ! ::KillTimer (NULL, timer_id); ! PyErr_Warn(PyExc_RuntimeWarning, "Unrecognized timer id"); ! Py_DECREF(py_timer_id); ! return; ! } // call the user's function ! // create a 'death grip' on the callback function, just incase ! // the callback itself removes the function from the map. ! Py_INCREF(callback_function); ! PyObject * callback_args = Py_BuildValue ("(Ok)", py_timer_id, time); ! PyObject * result = PyEval_CallObject (callback_function, callback_args); ! if (!result) { // Is this necessary, or will python already have flagged // an exception? Can we even catch exceptions here? PyErr_Print(); ! } ! // everything's ok, return ! Py_DECREF(callback_function); ! Py_XDECREF(callback_args); ! Py_XDECREF(result); ! Py_DECREF (py_timer_id); return; } + // @pymethod int|timer|set_timer|Creates a timer that executes a callback function + // @rdesc Returns the id of the timer, which can be passed to kill_timer to stop it. + // @comm Uses the SetTimer function. static PyObject * py_timer_set_timer (PyObject * self, PyObject * args) { ! PyObject *callback; ! PyObject * py_timer_id; ! UINT elapse; ! UINT_PTR timer_id; ! if (!PyArg_ParseTuple (args, "kO:set_timer", ! &elapse, // @pyparm int|Elapse||Timer period, in milliseconds ! &callback)) // @pyparm function|TimerFunc||Callback function. Will be called with with 2 int args: (timer_id, time) ! return NULL; ! // make sure the callback is a valid callable object ! if (!PyCallable_Check (callback)) { ! PyErr_SetString (PyExc_TypeError, "argument must be a callable object"); ! return NULL; ! } ! // create the win32 timer ! Py_BEGIN_ALLOW_THREADS; ! timer_id = ::SetTimer (NULL, 0, elapse, py_win32_timer_callback); ! Py_END_ALLOW_THREADS; ! if (!timer_id) ! return PyWin_SetAPIError("SetTimer"); ! py_timer_id = PyWinLong_FromVoidPtr((void *)timer_id); ! if (!py_timer_id){ ! ::KillTimer (NULL, timer_id); ! return NULL; ! } ! // associate the timer id with the given callback function ! if (PyDict_SetItem (timer_id_callback_map, py_timer_id, callback) == -1) { ! ::KillTimer (NULL, timer_id); ! Py_DECREF(py_timer_id); ! return NULL; ! } ! // everything went ok. ! return py_timer_id; } + // @pymethod boolean|timer|kill_timer|Creates a timer that executes a callback function + // @comm Uses the KillTimer API function. static PyObject * py_timer_kill_timer (PyObject * self, PyObject * args) { ! PyObject * py_timer_id; ! UINT_PTR timer_id; ! if (!PyArg_ParseTuple (args, "O:kill_timer", ! &py_timer_id)) // @pyparm int|IDEvent||Timer id as returned by <om timer.set_timer> ! return NULL; ! if (!PyWinLong_AsVoidPtr(py_timer_id, (void **)&timer_id)) ! return NULL; ! if (timer_id_callback_map) ! if (0 != PyDict_DelItem (timer_id_callback_map, py_timer_id)) ! return NULL; ! BOOL rc; ! Py_BEGIN_ALLOW_THREADS; ! rc = ::KillTimer (NULL, timer_id); ! Py_END_ALLOW_THREADS; ! return PyBool_FromLong(rc); } *************** *** 130,149 **** py_timer_timer_map (PyObject * self, PyObject * args) { ! if (!PyArg_ParseTuple (args, "")) { ! return NULL; ! } ! Py_INCREF (timer_id_callback_map); ! return (timer_id_callback_map); } #endif // List of functions exported by this module static struct PyMethodDef timer_functions[] = { ! {"set_timer", py_timer_set_timer, 1}, ! {"kill_timer", py_timer_kill_timer, 1}, #ifdef _DEBUG ! {"_id_timer_map", py_timer_timer_map, 1}, #endif ! {NULL, NULL} }; --- 127,149 ---- py_timer_timer_map (PyObject * self, PyObject * args) { ! if (!PyArg_ParseTuple (args, "")) ! return NULL; ! ! Py_INCREF (timer_id_callback_map); ! return (timer_id_callback_map); } #endif // List of functions exported by this module + // @module timer|Extension that wraps Win32 Timer functions static struct PyMethodDef timer_functions[] = { ! // @pymeth set_timer|Creates a timer that executes a callback function ! {"set_timer", py_timer_set_timer, METH_VARARGS, "int = set_timer(milliseconds, callback}\nCreates a timer that executes a callback function"}, ! // @pymeth kill_timer|Stops a timer ! {"kill_timer", py_timer_kill_timer, METH_VARARGS, "boolean = kill_timer(timer_id)\nStops a timer"}, #ifdef _DEBUG ! {"_id_timer_map", py_timer_timer_map, 1}, #endif ! {NULL, NULL} }; *************** *** 151,163 **** inittimer(void) { ! PyObject *dict, *module; ! module = Py_InitModule("timer", timer_functions); ! if (!module) /* Eeek - some serious error! */ ! return; ! dict = PyModule_GetDict(module); ! if (!dict) return; /* Another serious error!*/ ! timer_module_error = PyString_FromString("timer error"); ! PyDict_SetItemString(dict, "error", timer_module_error); ! PyDict_SetItemString(dict, "__version__", PyString_FromString("0.2")); ! timer_id_callback_map = PyDict_New(); } --- 151,167 ---- inittimer(void) { ! PyWinGlobals_Ensure(); ! timer_id_callback_map = PyDict_New(); ! if (!timer_id_callback_map) ! return; ! PyObject *dict, *module; ! module = Py_InitModule("timer", timer_functions); ! if (!module) /* Eeek - some serious error! */ ! return; ! dict = PyModule_GetDict(module); ! if (!dict) ! return; /* Another serious error!*/ ! Py_INCREF(PyWinExc_ApiError); ! PyDict_SetItemString(dict, "error", PyWinExc_ApiError); ! PyDict_SetItemString(dict, "__version__", PyString_FromString("0.2")); } |