Lenard Lindstrom avatar Lenard Lindstrom committed 6eb7090

First batch of fixes for issue #146

Fix original problem with compat.py, a Unicode string format error in
_freetype.c, and general Python 3.x compatibility issues with movieplayer.py
and png.py.

Python 3.3 still does not pass ftfont_test.py; there is a problem with
ftfont.Font initialization.

Comments (0)

Files changed (4)

examples/movieplayer.py

     msize = (w, h)
 
 
-    print "new screen..."
+    print ("new screen...")
     screen = pygame.display.set_mode(msize)
 
 
     pygame.display.set_caption(os.path.split(info.filename)[-1])
 
 
-    print "before movie = pygame._movie.Movie(filepath, screen)"
+    print ("before movie = pygame._movie.Movie(filepath, screen)")
     #surf = screen.copy().convert()
     #movie = pygame._movie.Movie(filepath, screen)
     movie = pygame._movie.Movie(filepath)
     #movie.surface = surf
-    print "after movie = pygame._movie.Movie(filepath, screen)"
+    print ("after movie = pygame._movie.Movie(filepath, screen)")
     #movie.set_display(screen, Rect((5, 5), msize))
 
-    print dir(movie)
-    print movie.surface
+    print (dir(movie))
+    print (movie.surface)
     #movie.xleft = 300
-    print "before movie.play()"
+    print ("before movie.play()")
     movie.play(0)
-    print "after movie.play()"
+    print ("after movie.play()")
 
     while movie.playing:
 
         events = pygame.event.get()
         for e in events:
-            print e
+            print (e)
             if e.type == QUIT or e.type == KEYDOWN and e.key == K_ESCAPE:
                 movie.stop()
 
 #   e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
 #         as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes"  # Python 3.x
 try:
-    eval("u'a'")
+    unicode
     def as_bytes(string):
         """ '<binary literal>' => '<binary literal>' """
         return string
     def as_unicode(rstring):
         """ r'<Unicode literal>' => u'<Unicode literal>' """
         return rstring.decode('unicode_escape', 'strict')
-except SyntaxError:
+except NameError:
     def as_bytes(string):
         """ '<binary literal>' => b'<binary literal>' """
         return string.encode('latin-1', 'strict')
         """ r'<Unicode literal>' => '<Unicode literal>' """
         return rstring.encode('ascii', 'strict').decode('unicode_escape',
                                                         'stict')
+# Include a next compatible function for Python versions < 2.6
+try:
+    next_ = next
+except NameError:
+    def next_(i, *args):
+        try:
+            return i.next()
+        except StopIteration:
+            if args:
+                return args[0]
+            raise
 
-
+# itertools.imap is missing in 3.x
+try:
+    import itertools.imap as imap_
+except ImportError:
+    imap_ = map
 {
     if (PgFont_IS_ALIVE(self)) {
 #if PY3
-        return PyUnicode_FromFormat("Font('%.1024u')", self->path);
+        return PyUnicode_FromFormat("Font('%.1024U')", self->path);
 #else
         PyObject *str = PyUnicode_AsEncodedString(self->path,
                                                   "raw_unicode_escape",

test/test_utils/png.py

 
 # png.py - PNG encoder/decoder in pure Python
 #
+# Modified for Pygame in Oct., 2012 to work with Python 3.x. 
+#
 # Copyright (C) 2006 Johann C. Rocholl <johann@browsershots.org>
 # Portions Copyright (C) 2009 David Jones <drj@pobox.com>
 # And probably portions Copyright (C) 2006 Nicko van Someren <nicko@nicko.org>
 
 __version__ = "$URL: http://pypng.googlecode.com/svn/trunk/code/png.py $ $Rev: 228 $"
 
+from pygame.compat import geterror, next_, imap_
 from array import array
 try: # See :pyver:old
     import itertools
         # :todo: Certain exceptions in the call to ``.next()`` or the
         # following try would indicate no row data supplied.
         # Should catch.
-        i,row = enumrows.next()
+        i,row = next_(enumrows)
         try:
             # If this fails...
             extend(row)
     # first row, which requires that we take a copy of its iterator.
     # We may also need the first row to derive width and bitdepth.
     a,t = itertools.tee(a)
-    row = t.next()
+    row = next_(t)
     del t
     try:
         row[0][0]
                 out.extend(map(lambda i: mask&(o>>i), shifts))
             return out[:width]
 
-        return itertools.imap(asvalues, rows)
+        return imap_(asvalues, rows)
 
     def serialtoflat(self, bytes, width=None):
         """Convert serial format (byte stream) pixel data to flat row
             while True:
                 try:
                     type, data = self.chunk()
-                except ValueError, e:
+                except ValueError:
+                    e = geterror()
                     raise ChunkError(e.args[0])
                 if type == 'IEND':
                     # http://www.w3.org/TR/PNG/#11IEND
             arraycode = 'BH'[self.bitdepth>8]
             # Like :meth:`group` but producing an array.array object for
             # each row.
-            pixels = itertools.imap(lambda *row: array(arraycode, row),
+            pixels = imap_(lambda *row: array(arraycode, row),
                        *[iter(self.deinterlace(raw))]*self.width*self.planes)
         else:
             pixels = self.iterboxed(self.iterstraight(raw))
 
     import os
 
-    print name
+    print (name)
     f = BytesIO()
     w = Writer(x, y, **k)
     w.write(f, rows)
             candi = candidate.replace('n', 'i')
             if candi not in _pngsuite:
                 continue
-            print 'adam7 read', candidate
+            print ('adam7 read %s' % (candidate,))
             straight = Reader(bytes=_pngsuite[candidate])
             adam7 = Reader(bytes=_pngsuite[candi])
             # Just compare the pixels.  Ignore x,y (because they're
         import itertools
 
         i = itertools.islice(itertools.count(10), 20)
-        i = itertools.imap(lambda x: [x, x, x], i)
+        i = imap_(lambda x: [x, x, x], i)
         img = from_array(i, 'RGB;5', dict(height=20))
         f = open('testiter.png', 'wb')
         img.save(f)
         try:
             import numpy
         except ImportError:
-            print >>sys.stderr, "skipping numpy test"
+            sys.stderr.write("skipping numpy test\n")
             return
 
         rows = [map(numpy.uint16, range(0,0x10000,0x5555))]
         try:
             import numpy
         except ImportError:
-            print >>sys.stderr, "skipping numpy test"
+            sys.stderr.write("skipping numpy test\n")
             return
 
         rows = [map(numpy.uint8, range(0,0x100,0x55))]
         try:
             import numpy
         except ImportError:
-            print >>sys.stderr, "skipping numpy test"
+            sys.stderr.write("skipping numpy test\n")
             return
 
         rows = [map(numpy.bool, [0,1])]
         try:
             import numpy
         except ImportError:
-            print >>sys.stderr, "skipping numpy test"
+            sys.stderr.write("skipping numpy test\n")
             return
 
         pixels = numpy.array([[0,0x5555],[0x5555,0xaaaa]], numpy.uint16)
         names = list(_pngsuite)
         names.sort()
         for name in names:
-            print name
+            print (name)
         return
 
     # Run regression tests
 if __name__ == '__main__':
     try:
         _main(sys.argv)
-    except Error, e:
-        print >>sys.stderr, e
+    except Error:
+        e = geterror()
+        sys.stderr.write("%s\n" % (e,))
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.