Commits

Lenard Lindstrom committed f0bb4a4 Merge

Add Python 3.2 fix to head

  • Participants
  • Parent commits dd6752f, b34a422

Comments (0)

Files changed (6)

File lib/ftfont.py

 """pygame module for loading and rendering fonts (freetype alternative)"""
 
-from pygame._freetype import (init, Face as _Face, get_default_resolution,
-                              quit, get_default_font, was_init as _was_init)
+__all__ = ['Font', 'init', 'quit', 'get_default_font', 'get_init', 'SysFont']
+
+from pygame._freetype import init, Face as _Face, get_default_resolution
+from pygame._freetype import quit, get_default_font, was_init as _was_init
+from pygame._freetype import __PYGAMEinit__
 from pygame.sysfont import match_font, get_fonts, SysFont as _SysFont
 from pygame import encode_file_path
 from pygame.compat import bytes_, unicode_, as_unicode, as_bytes
                        "-Wnested-externs -Wshadow -Wredundant-decls"
     sys.argv.remove ("-warnings")
 
-import os.path, glob, stat
+import os.path, glob, stat, shutil
 import distutils.sysconfig
 from distutils.core import setup, Extension, Command
 from distutils.extension import read_setup_file
             extensions.append(e)
     del tmp_extensions
 
+# if not building font, try replacing with ftfont
+alternate_font = os.path.join('lib', 'font.py')
+if os.path.exists(alternate_font):
+    os.remove(alternate_font)
+have_font = False
+have_freetype = False
+for e in extensions:
+    if e.name == 'font':
+        have_font = True
+    if e.name == '_freetype':
+        have_freetype = True
+if not have_font and have_freetype:
+    shutil.copyfile(os.path.join('lib', 'ftfont.py'), alternate_font)
+
 #extra files to install
 data_path = os.path.join(distutils.sysconfig.get_python_lib(), 'pygame')
 pygame_data_files = []

File src/_freetype.c

 static PyObject *_ft_get_error(PyObject *);
 static PyObject *_ft_was_init(PyObject *);
 static PyObject *_ft_autoinit(PyObject *);
+static void _ft_autoquit(void);
 static PyObject *_ft_get_default_resolution(PyObject *);
 static PyObject *_ft_set_default_resolution(PyObject *, PyObject *);
 static PyObject *_ft_get_default_font(PyObject* self);
     FT_Error result = 1;
 
     if (!FREETYPE_MOD_STATE(self)->freetype) {
+        PyGame_RegisterQuit(_ft_autoquit);
+
         if (_PGFT_Init(&(FREETYPE_MOD_STATE(self)->freetype),
                        FREETYPE_MOD_STATE(self)->cache_size)) {
             return 0;
     return PyInt_FromLong(result);
 }
 
+static void
+_ft_autoquit(void)
+{
+    _FreeTypeState *state = FREETYPE_STATE;
+
+    if (state->freetype) {
+        _PGFT_Quit(state->freetype);
+        state->freetype = 0;
+    }
+}
+
 static PyObject *
 _ft_quit(PyObject *self)
 {
-    if (FREETYPE_MOD_STATE(self)->freetype) {
-        _PGFT_Quit(FREETYPE_MOD_STATE(self)->freetype);
-        FREETYPE_MOD_STATE(self)->freetype = 0;
-    }
+    _ft_autoquit();
     Py_RETURN_NONE;
 }
 
 
 MODINIT_DEFINE (_freetype)
 {
-    PyObject *module, *apiobj, *pygame, *pygame_register_quit, *quit, *rval;
+    PyObject *module, *apiobj;
     static void* c_api[PYGAMEAPI_FREETYPE_NUMSLOTS];
 
     import_pygame_base();
         MODINIT_ERROR;
     }
 
-    /* import needed modules. Do this first so if there is an error
-       the module is not loaded.
-    */
-    pygame = PyImport_ImportModule ("pygame");
-    if (!pygame) {
-        MODINIT_ERROR;
-    }
-    pygame_register_quit = PyObject_GetAttrString (pygame, "register_quit");
-    Py_DECREF (pygame);
-    if (!pygame_register_quit) {
-        MODINIT_ERROR;
-    }
-
     /* type preparation */
     if (PyType_Ready(&PgFace_Type) < 0)  {
-        Py_DECREF(pygame_register_quit);
         MODINIT_ERROR;
     }
 
 #endif
 
     if (!module)  {
-        Py_DECREF(pygame_register_quit);
         MODINIT_ERROR;
     }
 
     Py_INCREF((PyObject *)&PgFace_Type);
     if (PyModule_AddObject(module, FACE_TYPE_NAME,
                            (PyObject *)&PgFace_Type) == -1)  {
-        Py_DECREF(pygame_register_quit);
         Py_DECREF((PyObject *) &PgFace_Type);
         DECREF_MOD(module);
         MODINIT_ERROR;
 
     apiobj = encapsulate_api(c_api, "freetype");
     if (!apiobj)  {
-        Py_DECREF (pygame_register_quit);
         DECREF_MOD(module);
         MODINIT_ERROR;
     }
 
     if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) == -1)  {
-        Py_DECREF(pygame_register_quit);
         Py_DECREF(apiobj);
         DECREF_MOD(module);
         MODINIT_ERROR;
     }
 
-    quit = PyObject_GetAttrString (module, "quit");
-    if (!quit) {  /* assertion */
-        Py_DECREF (pygame_register_quit);
-        DECREF_MOD (module);
-        MODINIT_ERROR;
-    }
-    rval = PyObject_CallFunctionObjArgs (pygame_register_quit, quit, 0);
-    Py_DECREF (pygame_register_quit);
-    Py_DECREF (quit);
-    if (!rval) {
-        DECREF_MOD (module);
-        MODINIT_ERROR;
-    }
-    Py_DECREF (rval);
-
     MODINIT_RETURN(module);
 }

File test/font_test.py

         else:
             self.assert_(not equal_images(su, sb))
 
-        if UCS_4:
-            # SDL_ttf only handles UCS-2
+        # If the font module is SDL_ttf based, then it can only supports  UCS-2;
+        # it will raise an exception for an out-of-range UCS-4 code point.
+        if UCS_4 and not hasattr(f, 'ucs4'):
             ucs_2 = as_unicode(r"\uFFEE")
             s = f.render(ucs_2, False, [0, 0, 0], [255, 255, 255])
             ucs_4 = as_unicode(r"\U00010000")

File test/freetype_test.py

             if was_init:
                 ft.quit()
 
+    def test_autoinit_and_autoquit(self):
+        pygame.init()
+        self.assertTrue(ft.was_init())
+        pygame.quit()
+        self.assertFalse(ft.was_init())
+
+        # Ensure autoquit is replaced at init time
+        pygame.init()
+        self.assertTrue(ft.was_init())
+        pygame.quit()
+        self.assertFalse(ft.was_init())
 
 if __name__ == '__main__':
     unittest.main()

File test/test_utils/async_sub.py

             if conn is None:
                 return None
             
-            flags = fcntl.fcntl(conn, fcntl.F_GETFL)
-            if not conn.closed:
-                fcntl.fcntl(conn, fcntl.F_SETFL, flags| os.O_NONBLOCK)
+            if not select.select([conn], [], [], 0)[0]:
+                return ''
             
-            try:
-                if not select.select([conn], [], [], 0)[0]:
-                    return ''
-                
-                r = conn.read(maxsize)
-                if not r:
-                    return self._close(which)
-    
-                if self.universal_newlines:
-                    r = r.replace("\r\n", "\n").replace("\r", "\n")
-                return r
-            finally:
-                if not conn.closed:
-                    fcntl.fcntl(conn, fcntl.F_SETFL, flags)
+            r = conn.read(maxsize)
+            if not r:
+                return self._close(which)
+
+            if self.universal_newlines:
+                r = r.replace("\r\n", "\n").replace("\r", "\n")
+            return r
+
 
 ################################################################################