Commits

Anonymous committed 5691b27

Fixed pygame2.sdl.event.Event constructor
Fixed pygame2.sdlttf.Font.render arguments
Changed Add/RemoveRefLock() methods for Surface and Overlay to use weakrefs.

Comments (0)

Files changed (6)

 	@echo "Cleaning up in $(top_srcdir)/ ..."
 	@rm -f *.cache *.core *~ MANIFEST *.pyc
 	@rm -rf src/doc
+	@rm -rf doc/ref
 	@rm -rf build dist
 
 	@for dir in $(SUBDIRS); do \

doc/create_htmlref.py

 from xml.dom.minidom import parse
 import os, glob, sys
 
-HTML_HEAD = """
+HTML_HEADER = """
 <html>
-<title>%s - Pygame Documentation</title>
-<body bgcolor=#aaeebb text=#000000 link=#331111 vlink=#331111>
+<head>
+<title>%s</title>
+</head>
+<body>
+"""
 
-<table cellpadding=0 cellspacing=0 border=0 style='border: 3px solid black;' width='100%%'>
-<tr>
-<td bgcolor='#c2fc20' style='padding: 6px;' align=center valign=center><a href='http://www.pygame.org/'><img src='../pygame_tiny.gif' border=0 width=200 height=60></a><br><b>pygame documentation</b></td>
-<td bgcolor='#6aee28' style='border-left: 3px solid black; padding: 6px;' align=center valign=center>
-	||&nbsp;
-	<a href=http://www.pygame.org>Pygame Home</a> &nbsp;||&nbsp;
-	<a href=../index.html>Help Contents</a> &nbsp;||
-	<a href=index.html>Reference Index</a> &nbsp;||
-	<br>&nbsp;<br>
-	
+HTML_FOOTER = """
+</body>
+</html>
 """
 
 class DocClass (object):
     def __init__ (self, filename):
         self.filename = filename
         self.modulename = None
-        self.description = None
+        self.description = ""
         self.classes = []
         self.functions = []
     
     
     def get_module_docs (self, dom):
         module = dom.getElementsByTagName ("module")[0]
-        self.module = module.getAttribute ("name")
-        self.description = module.getElementsByTagName \
-            ("desc")[0].firstChild.nodeValue
+        self.modulename = module.getAttribute ("name")
+        node = module.getElementsByTagName ("desc")[0]
+        if node.firstChild:
+            self.description = node.firstChild.nodeValue
         return module
     
     def get_module_funcs (self, module):
         functions = module.getElementsByTagName ("func")
         for func in functions:
             name = func.getAttribute ("name")
-            desc = func.getElementsByTagName ("desc")[0].firstChild.nodeValue
+            node = func.getElementsByTagName ("desc")[0]
+            if node.firstChild:
+                desc = node.firstChild.nodeValue
+            else:
+                desc = ""
             self.functions.append (DocMethod (None, name, desc))
 
     def get_class_refs (self, module):
         classes = module.getElementsByTagName ("class")
         for cls in classes:
             name = cls.getAttribute ("name")
-            desc = cls.getElementsByTagName ("desc")[0].firstChild.nodeValue
+            node = cls.getElementsByTagName ("desc")[0]
+            if node.firstChild:
+                desc = node.firstChild.nodeValue
+            else:
+                desc = ""
             clsdoc = DocClass (name, desc)
 
             attrs = cls.getElementsByTagName ("attr")
 
     def create_attr_ref (self, doccls, attr):
         name = attr.getAttribute ("name")
-        desc = attr.firstChild.nodeValue
+        if attr.firstChild:
+            desc = attr.firstChild.nodeValue
+        else:
+            desc = ""
         doccls.attributes.append (DocAttribute (name, desc))
 
     def create_method_ref (self, doccls, method):
         name = method.getAttribute ("name")
-        desc = method.getElementsByTagName ("desc")[0].firstChild.nodeValue
+        node = method.getElementsByTagName ("desc")[0]
+        if node.firstChild:
+            desc = node.firstChild.nodeValue
+        else:
+            desc = ""
         doccls.methods.append (DocMethod (doccls, name, desc))
+
+    def create_desc_html (self, desc, refcache):
+        data = '<p>'
+        written = 0
+        blocks = 0
+        for line in desc.split ('\n'):
+            line = line.strip ()
+            if written > 0 and line == '':
+                data += '</p><p>'
+                blocks += 1
+            else:
+                data += line
+            written += 1
+        if blocks > 0:
+            data += '</p>'
+        return data
+    
+    def create_func_html (self, func, refcache):
+        data = '<dt class="functions"><a name="%s">%s</a></dt>\n' % \
+               (func.name, func.name)
+        data += '<dd class="functions">%s</dd>\n' % \
+                self.create_desc_html (func.description, refcache)
+        return data
+    
+    def create_html (self, refcache):
+        fname = os.path.join ("ref", "%s.html")
+        fp = open (fname % self.modulename.replace (".", "_"), "w")
+        fp.write (HTML_HEADER % self.modulename)
+        if len (self.functions) > 0:
+            fp.write ('<dl class="functions">\n')
+            for func in self.functions:
+                fp.write (self.create_func_html (func, refcache))
+            fp.write ("</dl>")
+
+        if len (self.classes) > 0:
+            for cls in self.classes:
+                fp.write (cls.name)
+                fp.write (cls.description)
+                for attr in cls.attributes:
+                    fp.write (attr.name)
+                    fp.write (attr.description)
+                for method in cls.methods:
+                    fp.write (method.name)
+                    fp.write (method.description)
+        fp.write (HTML_FOOTER)
+        fp.close ()
         
-    def create_html (self, doclist):
-        # TODO
-        pass
+def create_html (docs):
+    refcache = {}
+    if not os.path.exists ("ref"):
+        os.mkdir ("ref")
+
+    for doc in docs:
+        for cls in doc.classes:
+            refcache[cls.name] = doc.filename
+        for func in doc.functions:
+            refcache[func.name] = doc.filename
+    for doc in docs:
+        print ("Now writing HTML for %s...." % doc.modulename)
+        doc.create_html (refcache)
 
 def get_doc_files ():
     docs = []
 if __name__ == "__main__":
     docs = get_doc_files ()
     for doc in docs:
+        print ("Parsing file %s..." % doc.filename)
         doc.parse_content ()
-        doc.create_html (docs)
+    create_html (docs)
     PyEvent *event = (PyEvent *) type->tp_alloc (type, 0);
     if (!event)
         return NULL;
-    event->dict = NULL;
+    event->dict = PyDict_New ();
+    if (!event->dict)
+    {
+        Py_DECREF (event);
+        return NULL;
+    }
     event->type = 0;
     return (PyObject*) event;
 }
     }
 
     event->type = eventid;
+    
     if (PyDict_Update (event->dict, dict) == -1)
         return -1;
 

src/sdl/overlay.c

 PyOverlay_AddRefLock (PyObject *overlay, PyObject *lock)
 {
     PyOverlay *ov = (PyOverlay*)overlay;
+    PyObject *wkref;
 
     if (!PyOverlay_Check (overlay))
     {
     if (SDL_LockYUVOverlay (ov->overlay) == -1)
         return 0;
 
-    if (PyList_Append (ov->locklist, lock) == -1)
+    wkref = PyWeakref_NewRef (lock, NULL);
+    if (!wkref)
+        return 0;
+
+    if (PyList_Append (ov->locklist, wkref) == -1)
     {
         SDL_UnlockYUVOverlay (ov->overlay);
+        Py_DECREF (wkref);
         return 0;
     }
-    /* Append increments the refcount, but we do not want that. The
-     * locklist simulates a sort of weak reference list.
-     */
-    Py_DECREF (lock);
-    Py_INCREF (overlay);
 
     return 1;
 }
 PyOverlay_RemoveRefLock (PyObject *overlay, PyObject *lock)
 {
     PyOverlay *ov = (PyOverlay*)overlay;
-    Py_ssize_t pos;
+    PyObject *ref, *item;
+    Py_ssize_t size;
+    int found = 0, noerror = 1;
 
     if (!PyOverlay_Check (overlay))
     {
         return 0;
     }
 
-    pos = PySequence_Index (ov->locklist, lock);
-    if (pos == -1)
+    size = PyList_Size (ov->locklist);
+    if (size == 0)
     {
         PyErr_SetString (PyExc_ValueError, "no locks are hold by the object");
         return 0;
     }
+    
+    while (--size >= 0)
+    {
+        ref = PyList_GET_ITEM (ov->locklist, size);
+        item = PyWeakref_GET_OBJECT (ref);
+        if (item == lock)
+        {
+            if (PySequence_DelItem (ov->locklist, size) == -1)
+                return 0;
+            found++;
+        }
+        else if (item == Py_None)
+        {
+            /* Clear dead references */
+            if (PySequence_DelItem (ov->locklist, size) != -1)
+                found++;
+            else
+                noerror = 0;
+        }
+    }
+    if (!found)
+        return noerror;
 
-    /* We are about to remove the lock object from the locklist.
-     * This requires us to increment its refcount, so that the removal
-     * won't destroy it.
-     */
-    Py_INCREF (lock);
-    if (PySequence_DelItem (ov->locklist, pos) == -1)
+    /* Release all locks on the overlay */
+    while (found > 0)
     {
-        /* Failed */
-        Py_DECREF (lock);
-        return 0;
+        SDL_UnlockYUVOverlay (ov->overlay);
+        found--;
     }
-    SDL_UnlockYUVOverlay (ov->overlay);
-    Py_DECREF (overlay);
-
-    return 1;
+    return noerror;
 }
 
 void

src/sdl/surface.c

 PySurface_AddRefLock (PyObject *surface, PyObject *lock)
 {
     PySurface *sf = (PySurface*)surface;
+    PyObject *wkref;
 
     if (!PySurface_Check (surface))
     {
     if (SDL_LockSurface (sf->surface) == -1)
         return 0;
 
-    if (PyList_Append (sf->locklist, lock) == -1)
+    wkref = PyWeakref_NewRef (lock, NULL);
+    if (!wkref)
+        return 0;
+
+    if (PyList_Append (sf->locklist, wkref) == -1)
     {
         SDL_UnlockSurface (sf->surface);
+        Py_DECREF (wkref);
         return 0;
     }
-    /* Append increments the refcount, but we do not want that. The
-     * locklist simulates a sort of weak reference list.
-     */
-    Py_DECREF (lock);
-    Py_INCREF (surface);
 
     return 1;
 }
 PySurface_RemoveRefLock (PyObject *surface, PyObject *lock)
 {
     PySurface *sf = (PySurface*)surface;
-    Py_ssize_t pos;
+    PyObject *ref, *item;
+    Py_ssize_t size;
+    int found = 0, noerror = 1;
 
     if (!PySurface_Check (surface))
     {
         return 0;
     }
 
-    pos = PySequence_Index (sf->locklist, lock);
-    if (pos == -1)
+    size = PyList_Size (sf->locklist);
+    if (size == 0)
     {
         PyErr_SetString (PyExc_ValueError, "no locks are hold by the object");
         return 0;
     }
+    
+    while (--size >= 0)
+    {
+        ref = PyList_GET_ITEM (sf->locklist, size);
+        item = PyWeakref_GET_OBJECT (ref);
+        if (item == lock)
+        {
+            if (PySequence_DelItem (sf->locklist, size) == -1)
+                return 0;
+            found++;
+        }
+        else if (item == Py_None)
+        {
+            /* Clear dead references */
+            if (PySequence_DelItem (sf->locklist, size) != -1)
+                found++;
+            else
+                noerror = 0;
+        }
+    }
+    if (!found)
+        return noerror;
 
-    /* We are about to remove the lock object from the locklist.
-     * This requires us to increment its refcount, so that the removal
-     * won't destroy it.
-     */
-    Py_INCREF (lock);
-    if (PySequence_DelItem (sf->locklist, pos) == -1)
+    /* Release all locks on the surface */
+    while (found > 0)
     {
-        /* Failed */
-        Py_DECREF (lock);
-        return 0;
+        SDL_UnlockSurface (sf->surface);
+        found--;
     }
-    SDL_UnlockSurface (sf->surface);
-    Py_DECREF (surface);
-
-    return 1;
+    return noerror;
 }
 
 PyObject*

src/sdlttf/font.c

 
     ASSERT_TTF_INIT (NULL);
 
-    if (!PyArg_ParseTuple (args, "OO|iO:render", &text, &colorfg, &colorbg,
+    if (!PyArg_ParseTuple (args, "OO|Oi:render", &text, &colorfg, &colorbg,
             &render))
         return NULL;