Commits

pygame  committed 8959358

filelike read fix

  • Participants
  • Parent commits fb11cd4

Comments (0)

Files changed (6)

 # BREAK = change breaks existing code
 # BUG	= fixed a bug that was (or could have been) crashing
 
+September 27, 2003
+	Fix reading from file file-like objects (thx Timothy Stranex)
+
 September 21, 2003
 	Fix sysfont registry bug on winNT arch
 
     Niki Spahiev, Gordon Tyler, Nathaniel Pryce,
     Dave Wallace, John Popplewell, Michael Urman,
     Andrew Straw, Michael Hudson, Ole Martin Bjoerndalen,
-    Hervé Cauwelier, James Mazer, Lalo Martins</li>
+    Hervé Cauwelier, James Mazer, Lalo Martins,
+    Timothy Stranex</li>
 <li>And our bug hunters above and beyond:
     Angus, Guillaume Proux, Frank Raiser,
     Austin Henry, Kaweh Kazemi, Arturo Aldama,
      Thanks to those sending in patches and fixes: Niki Spahiev, Gordon
    Tyler, Nathaniel Pryce, Dave Wallace, John Popplewell, Michael Urman,
    Andrew Straw, Michael Hudson, Ole Martin Bjoerndalen, Hervé Cauwelier,
-   James Mazer, Lalo Martins
+   James Mazer, Lalo Martins, Timothy Stranex
 
      And our bug hunters above and beyond: Angus, Guillaume Proux, Frank
    Raiser, Austin Henry, Kaweh Kazemi, Arturo Aldama, Mike Mulcheck, Rene
         else if(size == -8) size = AUDIO_S8;
         else if(size == 16) size = AUDIO_U16SYS;
         else if(size == -16) size = AUDIO_S16SYS;
-        
+
 	/*make chunk a power of 2*/
 	for(i=0; 1<<i < chunk; ++i); //yes, semicolon on for loop
 	chunk = max(1<<i, 256);
 	if(!SDL_WasInit(SDL_INIT_AUDIO))
 	{
 		PyGame_RegisterQuit(autoquit);
-                
+
                 if(!channeldata) /*should always be null*/
                 {
                     numchanneldata = MIX_CHANNELS;
 #if MIX_MAJOR_VERSION>=1 && MIX_MINOR_VERSION>=2 && MIX_PATCHLEVEL>=3
                 Mix_ChannelFinished(endsound_callback);
 #endif
-                
+
               	Mix_VolumeMusic(127);
 	}
 	return PyInt_FromLong(1);
 		RETURN_NONE
 
 	//create a signed or unsigned number of bits per sample
-	realform = format&~0xff ? -(format&0xff) : format&0xff; 
+	realform = format&~0xff ? -(format&0xff) : format&0xff;
 	return Py_BuildValue("(iii)", freq, realform, channels>1);
 }
 
 	Mix_Chunk* chunk = PySound_AsChunk(self);
 	int channelnum = -1;
 	int loops = 0, playtime = -1;
-	
+
 	if(!PyArg_ParseTuple(args, "|ii", &loops, &playtime))
 		return NULL;
-	
+
 	channelnum = Mix_PlayChannelTimed(-1, chunk, loops, playtime);
 	if(channelnum == -1)
 		RETURN_NONE
 	channeldata[channelnum].queue = NULL;
         channeldata[channelnum].sound = self;
         Py_INCREF(self);
-                    
+
 	//make sure volume on this arbitrary channel is set to full
 	Mix_Volume(channelnum, 128);
 
     /*DOC*/    "pygame.mixer module's initialization.\n"
     /*DOC*/ ;
 
-static PyTypeObject PySound_Type = 
+static PyTypeObject PySound_Type =
 {
 	PyObject_HEAD_INIT(NULL)
 	0,
 	"Sound",
 	sizeof(PySoundObject),
 	0,
-	sound_dealloc,	
+	sound_dealloc,
 	0,
 	sound_getattr,
 	NULL,					/*setattr*/
 	PyObject* sound;
 	Mix_Chunk* chunk;
 	int loops = 0, playtime = -1;
-	
+
 	if(!PyArg_ParseTuple(args, "O!|ii", &PySound_Type, &sound, &loops, &playtime))
 		return NULL;
 	chunk = PySound_AsChunk(sound);
-	
+
 	channelnum = Mix_PlayChannelTimed(channelnum, chunk, loops, playtime);
 	if(channelnum != -1)
 		Mix_GroupChannel(channelnum, (int)chunk);
 	channeldata[channelnum].queue = NULL;
         Py_INCREF(sound);
 
-        	
+
 	RETURN_NONE
 }
 
 	int channelnum = PyChannel_AsInt(self);
 	PyObject* sound;
 	Mix_Chunk* chunk;
-	
+
 	if(!PyArg_ParseTuple(args, "O!", &PySound_Type, &sound))
 		return NULL;
 	chunk = PySound_AsChunk(sound);
 	    channeldata[channelnum].queue = sound;
 	    Py_INCREF(sound);
 	}
-        	
+
 	RETURN_NONE
 }
 
 	sound = channeldata[channelnum].sound;
 	if(!sound)
 	    RETURN_NONE
-		    
+
 	Py_INCREF(sound);
 	return sound;
 }
 	sound = channeldata[channelnum].queue;
 	if(!sound)
 	    RETURN_NONE
-		    
+
 	Py_INCREF(sound);
 	return sound;
 }
 
 	{ "get_sound", chan_get_sound, 1, doc_chan_get_sound },
 	{ "get_queue", chan_get_queue, 1, doc_chan_get_queue },
-		
+
 	{ "set_endevent", chan_set_endevent, 1, doc_chan_set_endevent },
 	{ "get_endevent", chan_get_endevent, 1, doc_chan_get_endevent },
 
     /*DOC*/ ;
 
 
-static PyTypeObject PyChannel_Type = 
+static PyTypeObject PyChannel_Type =
 {
 	PyObject_HEAD_INIT(NULL)
 	0,
 	"Channel",
 	sizeof(PyChannelObject),
 	0,
-	channel_dealloc,	
+	channel_dealloc,
 	0,
 	channel_getattr,
 	NULL,					/*setattr*/
 	    }
             numchanneldata = numchans;
         }
-        
+
 	Mix_AllocateChannels(numchans);
 	RETURN_NONE
 }
 static PyObject* PySound_New(Mix_Chunk* chunk)
 {
 	PySoundObject* soundobj;
-	
+
 	if(!chunk)
 		return RAISE(PyExc_RuntimeError, "unable to create sound.");
 
 static PyObject* PyChannel_New(int channelnum)
 {
 	PyChannelObject* chanobj;
-	
+
 	if(channelnum < 0 || channelnum >= Mix_GroupCount(-1))
 		return RAISE(PyExc_IndexError, "invalid channel index");
 
 	import_pygame_rwobject();
 
 	music = PyImport_ImportModule("pygame.mixer_music");
-        if(music) 
+        if(music)
 	{
 		PyObject* ptr, *dict;
 		PyModule_AddObject(module, "music", music);
 		current_music = (Mix_Music**)PyCObject_AsVoidPtr(ptr);
 		ptr = PyDict_GetItemString(dict, "_QUEUE_POINTER");
 		queue_music = (Mix_Music**)PyCObject_AsVoidPtr(ptr);
-	}	
+	}
 	else /*music module not compiled? cleanly ignore*/
 	{
             current_music = NULL;
 	Py_BEGIN_ALLOW_THREADS
 	current_music = Mix_LoadMUS(filename);
 	Py_END_ALLOW_THREADS
-#endif	
+#endif
 	if(!current_music)
 		return RAISE(PyExc_SDLError, SDL_GetError());
 	if(queue_music)
 		queue_music = NULL;
 	}
 
-	
+
 	RETURN_NONE;
 }
 
 	Py_BEGIN_ALLOW_THREADS
 	queue_music = Mix_LoadMUS(filename);
 	Py_END_ALLOW_THREADS
-		
+
     	RETURN_NONE
 }
 

File src/rwobject.c

 		return SDL_RWFromFile(name, "rb");
 	}
 	else if(PyFile_Check(obj))
-		return SDL_RWFromFP(PyFile_AsFile(obj), 1);
-
+		return SDL_RWFromFP(PyFile_AsFile(obj), 0);
         return NULL;
 }
 
 	result = PyObject_CallFunction(helper->read, "i", size * maxnum);
 	if(!result)
 		return -1;
-		
+
 	if(!PyString_Check(result))
 	{
 		Py_DECREF(result);
 		return -1;
 	}
-		
+
 	retval = PyString_GET_SIZE(result);
 	memcpy(ptr, PyString_AsString(result), retval);
 	retval /= size;
 	RWHelper* helper;
         PyInterpreterState* interp;
         PyThreadState* thread;
-    
+
 	if(!obj)
 		return (SDL_RWops*)RAISE(PyExc_TypeError, "Invalid filetype object");
 
         rw = get_standard_rwop(obj);
         if(rw)
                 return rw;
-        
+
 #ifndef WITH_THREAD
         return (SDL_RWops*)RAISE(PyExc_NotImplementedError, "Python built without thread support");
 #else
         helper = PyMem_New(RWHelper, 1);
         fetch_object_methods(helper, obj);
-        
+
         rw = SDL_AllocRW();
         rw->hidden.unknown.data1 = (void*)helper;
         rw->seek = rw_seek_th;
 
         PyEval_AcquireLock();
         oldstate = PyThreadState_Swap(helper->thread);
-        
+
 	if(!(offset == 0 && whence == SEEK_CUR)) /*being called only for 'tell'*/
 	{
 		result = PyObject_CallFunction(helper->seek, "ii", offset, whence);
 
         PyThreadState_Swap(oldstate);
         PyEval_ReleaseLock();
-        
+
 	return retval;
 }
 
 
         PyEval_AcquireLock();
         oldstate = PyThreadState_Swap(helper->thread);
-        
+
 	result = PyObject_CallFunction(helper->read, "i", size * maxnum);
 	if(!result)
 		return -1;
-		
+
 	if(!PyString_Check(result))
 	{
 		Py_DECREF(result);
 		return -1;
 	}
-		
+
 	retval = PyString_GET_SIZE(result);
 	memcpy(ptr, PyString_AsString(result), retval);
 	retval /= size;
 
         PyThreadState_Swap(oldstate);
         PyEval_ReleaseLock();
-        
+
 	return retval;
 }
 
 	RWHelper* helper = (RWHelper*)context->hidden.unknown.data1;
 	PyObject* result;
         PyThreadState* oldstate;
-   
+
 	if(!helper->write)
 		return -1;
 
         PyEval_AcquireLock();
         oldstate = PyThreadState_Swap(helper->thread);
-        
+
 	result = PyObject_CallFunction(helper->write, "s#", ptr, size * num);
 	if(!result)
 		return -1;
 
         PyThreadState_Swap(oldstate);
         PyEval_ReleaseLock();
-        
+
 	return num;
 }
 
 	PyObject* result;
 	int retval = 0;
         PyThreadState* oldstate;
-    
+
         PyEval_AcquireLock();
         oldstate = PyThreadState_Swap(helper->thread);
-        
+
 	if(helper->close)
 	{
 		result = PyObject_CallFunction(helper->close, NULL);
         PyThreadState_Clear(helper->thread);
         PyThreadState_Delete(helper->thread);
         PyEval_ReleaseLock();
-        
+
 	SDL_FreeRW(context);
 	return retval;
 }