Fatal Python error using ctypes & python exceptions

Discussion in 'Python' started by mmacrobert, Aug 31, 2007.

  1. mmacrobert

    mmacrobert Guest

    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
    mmacrobert, Aug 31, 2007
    #1
    1. Advertising

  2. 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
    Thomas Heller, Aug 31, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    394
    Henk Punt
    Jul 23, 2004
  2. Uri Nix

    ctypes and DLL exceptions

    Uri Nix, Feb 21, 2007, in forum: Python
    Replies:
    0
    Views:
    225
    Uri Nix
    Feb 21, 2007
  3. Replies:
    1
    Views:
    1,445
    Ian Collins
    Jun 20, 2007
  4. Replies:
    0
    Views:
    501
  5. Replies:
    1
    Views:
    413
Loading...

Share This Page