From pablo.yabo at gmail.com Sat Nov 3 22:58:06 2007 From: pablo.yabo at gmail.com (Pablo Yabo) Date: Sat, 3 Nov 2007 18:58:06 -0300 Subject: [capi-sig] Thread structures BUG? Message-ID: Hello, I create lots of threads that run a script and I have a problem when a thread is created with the same threadId that existed before. When a new thread is started I use this code to enter the interpreter: // create a thread state object for this thread PyEval_AcquireLock(); threadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(threadState); and when the thread finishes: PyThreadState_Swap(NULL); PyThreadState_Clear(info); // delete my thread state object PyThreadState_Delete(info); PyEval_ReleaseLock(); Everything works smoothly until a thread id is re-used. I've been testing what is happening and the problem is that PyGILState_GetThisThreadState() returns a PyThreadState for the reused thread id. I changed my code to call PyGILState_GetThisThreadState() before creating the PyThreadState. But if I try to use the returned PyThreadState (that shouldn't exist because I've already deleted) I got an error when trying to access the interpreter dictionary. The workaround that I found is to keep all PyThreadState structures without calling PyThreadState_Clear / PyThreadState_Delete. When a new thread is created I call PyGILState_GetThisThreadState(). If it returns something I reuse the structure. In this way, my code works. Thanks, Pablo Yabo -- http://www.nektra.com From sndive at gmail.com Fri Nov 9 20:04:01 2007 From: sndive at gmail.com (Anton Tropashko) Date: Fri, 9 Nov 2007 13:04:01 -0600 Subject: [capi-sig] No pickling? Message-ID: <2822e000711091104h16258b0bl962fb068d387eb8d@mail.gmail.com> I see some api published for marshal module: http://www.python.org/dev/doc/maint24/api/marshalling-utils.html but per latest discussion on comp.lang.python people were suggesting to use pickle. If i pass the marshalled string to a machine running python on a machine with a different byte order, would it unmarshall correctly? From fdrake at gmail.com Fri Nov 9 20:20:34 2007 From: fdrake at gmail.com (Fred Drake) Date: Fri, 9 Nov 2007 14:20:34 -0500 Subject: [capi-sig] No pickling? In-Reply-To: <2822e000711091104h16258b0bl962fb068d387eb8d@mail.gmail.com> References: <2822e000711091104h16258b0bl962fb068d387eb8d@mail.gmail.com> Message-ID: <9cee7ab80711091120p221c96c4j83a93e419f39a563@mail.gmail.com> On Nov 9, 2007 2:04 PM, Anton Tropashko wrote: > If i pass the marshalled string to a machine running python on > a machine with a different byte order, would it unmarshall correctly? This isn't the right place for this question; it makes more sense on comp.lang.python. Both the marshal and pickle formats are byte-order independent. -Fred -- Fred L. Drake, Jr. "Chaos is the score upon which reality is written." --Henry Miller From sndive at gmail.com Fri Nov 9 22:55:32 2007 From: sndive at gmail.com (Anton Tropashko) Date: Fri, 9 Nov 2007 15:55:32 -0600 Subject: [capi-sig] scopes and PyEval_EvalCode() Message-ID: <2822e000711091355x1a1f094dl223d910c22b98bda@mail.gmail.com> On Nov 6, 8:25 pm, snd... at gmail.com wrote: > i naively created execution context: > PyObject *execcontext = PyDict_New(); > stuffed a handle in it: > PyObject *ih = PyCObject_FromVoidPtr(handle, NULL); > int st= PyDict_SetItemString(res, "interp", ih); > > and later on in a function for a module that i defined > expected to extract that handle: > PyObject *dict = PyEval_GetGlobals(); // or GetLocals > depending on where > // execcontext ended up in PyEval_EvalCode > PyObject *co = PyDict_GetItemString(dict, "interp"); > assert(PyCObject_Check(co)); > but i get null co either way and the dict does not match execcontext > that is passed into PyEval_EvalCode. > > any ideas how to fix this? in other words: is there a scope more global than a module scope? something that won't change for a given PyEval_EvalCode or a similar call. i stuffed my CObject into __main__ but in case of multiple threads or nested PyEval_EvalCode i'd have a problem with this approach. i somehow just need to get to the pointer to the same dictionary that i passed to PyEval_EvalCode for global scope to retrieve my handle in a method of a new python type defined in C space. thank you