473,231 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Fatal Python error using ctypes & python exceptions

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: ::PyErr_SetString(::PyExc_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

Aug 31 '07 #1
1 3530
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: ::PyErr_SetString(::PyExc_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

Aug 31 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
16
by: PyDenis | last post by:
Today, I found strange error while using py2exe: 1. I wrote simple program and save as 1.py: import win32ui import win32con win32ui.MessageBox('Test messageBox.' , 'Test', win32con.MB_OK |...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 431 open ( +3) / 3425 closed ( +8) / 3856 total (+11) Bugs : 916 open (-23) / 6273 closed (+44) / 7189 total (+21) RFE : 244 open...
1
by: p.lavarre | last post by:
Is this interesting Python output? $ gc-object-already-tracked.py ********************************************************************** File...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.