[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Unexpected behaviour in pygame.image.load (patch)



Hello,

The following program:

"""
import pygame

pygame.init()
img = pygame.image.load(file('test.png'))

while 1:
    pass
""" 

hangs and must be killed with kill -9 while this program:

"""
import pygame

pygame.init()
img = pygame.image.load('test.png')

while 1:
    pass
"""

works fine.

I have traced the problem to imageext.c. After the python file object
has been converted to an SDL_RWops, it is passed to IMG_LoadTyped_RW.
The second arguments of this function tells it whether to close the file
after the image has been read. imageext.c passes 1 which closes the
file; however, the original python file object remains open. When the
garbage collector tries to destroy the python file object, it tries to
close the file, although it has already been closed. This results in a
pygame paracute in my game and the behaviour demonstrated above.

The solution is to prevent IMG_LoadTyped_RW from closing the file by
passing a 0 instead of a 1. A patch that fixes this is attached.

Thanks,

-- 
Timothy Stranex <timothy@stranex.com>
http://www.stranex.com/~perspex/

--- imageext.c	2003-09-25 15:14:42.000000000 +0200
+++ imageext2.c	2003-09-25 15:14:27.000000000 +0200
@@ -70,12 +70,12 @@
 			return NULL;
 		if(RWopsCheckPython(rw))
                 {
-			surf = IMG_LoadTyped_RW(rw, 1, find_extension(name));
+			surf = IMG_LoadTyped_RW(rw, 0, find_extension(name));
                 }
 		else
 		{
 			Py_BEGIN_ALLOW_THREADS
-			surf = IMG_LoadTyped_RW(rw, 1, find_extension(name));
+			surf = IMG_LoadTyped_RW(rw, 0, find_extension(name));
 			Py_END_ALLOW_THREADS
 		}
 	}

Attachment: signature.asc
Description: This is a digitally signed message part