473,626 Members | 3,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_SetStri ng(::PyExc_Runt imeError,
theErrorString) ;

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

Fatal Python error: PyThreadState_G et: no current thread

when the calling code in python is:

try:
cdll.MyDll.Thro wingFunction()
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 3559
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_SetStri ng(::PyExc_Runt imeError,
theErrorString) ;

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

Fatal Python error: PyThreadState_G et: no current thread

when the calling code in python is:

try:
cdll.MyDll.Thro wingFunction()
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
3844
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 modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
16
5198
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 | win32con.MB_TOPMOST ) 2. I create 1_setup.py file for py2exe:
0
263
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 ( +4) / 240 closed ( +1) / 484 total ( +5) New / Reopened Patches ______________________
1
4267
by: p.lavarre | last post by:
Is this interesting Python output? $ gc-object-already-tracked.py ********************************************************************** File "/Users/admin/Desktop/lxio/gc-object-already-tracked.py", line 770, in __main__.REDACTED Failed example: nqzhexes(self.send()) Expected:
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.