Lenard Lindstrom avatar Lenard Lindstrom committed a9fd70c

adapt fastevent module for Python 3.1

Comments (0)

Files changed (4)

examples/fastevents.py

         #for x in range(200):
         #    ee = event.Event(USEREVENT)
         #    r = event_module.post(ee)
-        #    print r
+        #    print (r)
 
         #for e in event_module.get():
         event_list = []
 
         for e in event_list:
             if e.type == QUIT:
-                print c.get_fps()
+                print (c.get_fps())
                 poster.stop.append(1)
                 going = False
             if e.type == KEYDOWN:
                 if e.key == K_ESCAPE:
-                    print c.get_fps()
+                    print (c.get_fps())
                     poster.stop.append(1)
                     going = False
         if poster.done:
-            print c.get_fps()
-            print c
+            print (c.get_fps())
+            print (c)
             t2 = pytime.time()
-            print "total time:%s" % (t2 - t1)
-            print "events/second:%s" % (NUM_EVENTS_TO_POST / (t2 - t1))
+            print ("total time:%s" % (t2 - t1))
+            print ("events/second:%s" % (NUM_EVENTS_TO_POST / (t2 - t1)))
             going = False
         if with_display:
             display.flip()
 constants src/constants.c $(SDL) $(DEBUG)
 display src/display.c $(SDL) $(DEBUG)
 event src/event.c $(SDL) $(DEBUG)
-#fastevent src/fastevent.c src/fastevents.c $(SDL) $(DEBUG)
+fastevent src/fastevent.c src/fastevents.c $(SDL) $(DEBUG)
 key src/key.c $(SDL) $(DEBUG)
 mouse src/mouse.c $(SDL) $(DEBUG)
 rect src/rect.c $(SDL) $(DEBUG)
  */
 #define PYGAMEAPI_FASTEVENT_INTERNAL
 #include "pygame.h"
+#include "pgcompat.h"
 #include "fastevents.h"
 
 static int FE_WasInit = 0;
 /*DOC*/ "main thread of an SDL program.\n"
 /*DOC*/ ;
 static PyObject *
-fastevent_post (PyObject * self, PyObject * args)
+fastevent_post (PyObject * self, PyObject * arg)
 {
-    PyEventObject *e;
     SDL_Event     event;
     int           status;
 
-    if (!PyArg_ParseTuple (args, "O!", &PyEvent_Type, &e))
+    if (!PyObject_IsInstance (arg, (PyObject *) &PyEvent_Type)) {
+        PyErr_Format (PyExc_TypeError, "argument 1 must be %s, not %s",
+                      PyEvent_Type.tp_name, Py_TYPE(arg)->tp_name);
         return NULL;
+    }
 
     FE_INIT_CHECK ();
 
-    if (PyEvent_FillUserEvent (e, &event))
+    if (PyEvent_FillUserEvent ((PyEventObject *) arg, &event))
         return NULL;
 
     Py_BEGIN_ALLOW_THREADS;
     Py_RETURN_NONE;
 }
 
-static PyMethodDef fastevent_builtins[] =
+static PyMethodDef _fastevent_methods[] =
 {
     {"init", (PyCFunction) fastevent_init, METH_NOARGS, doc_init},
     {"get", (PyCFunction) fastevent_get, METH_NOARGS, doc_get},
     {"pump", (PyCFunction) fastevent_pump, METH_NOARGS, doc_pump},
     {"wait", (PyCFunction) fastevent_wait, METH_NOARGS, doc_wait},
     {"poll", (PyCFunction) fastevent_poll, METH_NOARGS, doc_poll},
-    {"post", fastevent_post, METH_VARARGS, doc_post},
+    {"post", fastevent_post, METH_O, doc_post},
 
     {NULL, NULL, 0, NULL}
 };
 /*DOC*/ "any of the pump, wait, poll, post, get, peek, etc. functions\n"
 /*DOC*/ "from pygame.event, but you should use the Event objects.\n"
 /*DOC*/ ;
-PYGAME_EXPORT
-void 
-initfastevent (void)
+MODINIT_DEFINE (fastevent)
 {
     PyObject *module, *eventmodule, *dict;
+    int ecode;
+
+#if PY3
+    static struct PyModuleDef _module = {
+        PyModuleDef_HEAD_INIT,
+        "fastevent",
+        doc_fastevent_MODULE,
+        -1,
+        _fastevent_methods,
+        NULL, NULL, NULL, NULL
+    };
+#endif
 
     /* imported needed apis; Do this first so if there is an error
        the module is not loaded.
     */
     import_pygame_base ();
     if (PyErr_Occurred ()) {
-	return;
+	MODINIT_ERROR;
     }
     import_pygame_event ();
     if (PyErr_Occurred ()) {
-	return;
+	MODINIT_ERROR;
     }
 
     /* create the module */
-    module = Py_InitModule3 ("fastevent", fastevent_builtins,
+#if PY3
+    module = PyModule_Create (&_module);
+#else
+    module = Py_InitModule3 ("fastevent", _fastevent_methods,
                              doc_fastevent_MODULE);
+#endif
+    if (module == NULL) {
+        MODINIT_ERROR;
+    }
     dict = PyModule_GetDict (module);
 
     /* add the event module functions if available */
             PyObject *ref = PyObject_GetAttrString (eventmodule, NAMES[i]);
             if (ref)
             {
-                PyDict_SetItemString (dict, NAMES[i], ref);
+                ecode = PyDict_SetItemString (dict, NAMES[i], ref);
                 Py_DECREF (ref);
+                if (ecode == -1) {
+                    MODINIT_ERROR;
+                }
             }
             else
                 PyErr_Clear ();
         }
     }
+    else
+    {
+        PyErr_Clear ();
+    }
+    MODINIT_RETURN (module);
 }

test/fastevent_test.py

     from test.event_test import race_condition_notification
 import pygame
 from pygame import event, fastevent
+from pygame.compat import geterror
 
 ################################################################################
 
             [e.type for e in event.get()], [pygame.USEREVENT] * 10,
             race_condition_notification
         )
+
+        try:
+            # Special case for post: METH_O.
+            fastevent.post(1)
+        except TypeError:
+            e = geterror()
+            msg = ("argument 1 must be %s, not %s" %
+                   (fastevent.Event.__name__, type(1).__name__))
+            self.failUnlessEqual(str(e), msg)
+        else:
+            self.fail()
     
     def todo_test_pump(self):
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.