Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Fatal Python error using ctypes & python exceptions

Reply
Thread Tools

Fatal Python error using ctypes & python exceptions

 
 
mmacrobert
Guest
Posts: n/a
 
      08-31-2007
Hi Everyone,
I've created a 'C' dll that is accessed via ctypes library containing
a bunch of functions. I've successfully been able to use the
functions. However, I would like to throw python exceptions from some
of them.

I throw them using: :yErr_SetString(:yExc_RuntimeError,
theErrorString);

I crash the console when this function is invoked in the 'C' domain. I
get an error stating:

Fatal Python error: PyThreadState_Get: no current thread

when the calling code in python is:

try:
cdll.MyDll.ThrowingFunction()
except:
print "An error has occurred"

The dll is just a plain win32 'C' dll, built with an MS compiler. How
do I throw python exceptions correctly? Is there some kind of "init"
function that needs to be called?

Any help much appreciated.

Thanks,
Martin

 
Reply With Quote
 
 
 
 
Thomas Heller
Guest
Posts: n/a
 
      08-31-2007
mmacrobert schrieb:
> Hi Everyone,
> I've created a 'C' dll that is accessed via ctypes library containing
> a bunch of functions. I've successfully been able to use the
> functions. However, I would like to throw python exceptions from some
> of them.
>
> I throw them using: :yErr_SetString(:yExc_RuntimeError,
> theErrorString);
>
> I crash the console when this function is invoked in the 'C' domain. I
> get an error stating:
>
> Fatal Python error: PyThreadState_Get: no current thread
>
> when the calling code in python is:
>
> try:
> cdll.MyDll.ThrowingFunction()
> except:
> print "An error has occurred"
>
> The dll is just a plain win32 'C' dll, built with an MS compiler. How
> do I throw python exceptions correctly? Is there some kind of "init"
> function that needs to be called?


For libraries loaded with cdll.MyDll or CDLL("MyDll") ctypes releases
the GIL before calling the function, and reacquires the GIL afterwards.

This has the consequence that you cannot use any Python api functions
inside the dll functions (because there is no ThreadState, just like
the error message says).

If you want to throw Python exceptions in the dlls functions, or use
other Python apis, you must use the 'Python calling convention'.
For this calling convention the GIL is NOT released and reacquired,
but after the function call returns PyErr_Occurred() is called and an
exception raised in the calling code - exactly what you want.

The 'Python calling convention' is used when you load the library
with pydll.MyDll or PyDLL("MyDll").

Additional remark: You can have functions with different calling
conventions in the same dll, just load it with different library loaders
and you're fine.

Thomas

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2.x,3.x iOS static build: Fatal Python error: exceptionsbootstrapping error. angeljanai@gmail.com Python 1 05-22-2012 05:20 AM
WindowsXP/ CTypes - How to convert ctypes array to a string? dudeja.rajat@gmail.com Python 0 08-19-2008 10:20 AM
fatal error U1023: syntax error in expression while using NMAKE parveen.beniwal@rediffmail.com C++ 1 06-20-2007 05:28 AM
ctypes and DLL exceptions Uri Nix Python 0 02-21-2007 07:46 AM
RE: [ctypes-users] [Ann] ctypes 0.9.0 released Henk Punt Python 0 07-23-2004 10:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57