Mutual module imports

Discussion in 'Python' started by Tony Nelson, Oct 18, 2005.

  1. Tony Nelson

    Tony Nelson Guest

    How does one normally make a Python extension module that has some parts
    in Python and some functions in C share globals between the Python and C
    functions? Will that approach work with Pyrex?

    I have written a Python module that uses some C functions. I wrote the
    module in two parts, one Python, one Pyrex (C). They need to share some
    globals. (I use pyrex to handle ref counting. I think I'm glad I did.)

    At first they just sort of mutually imported each other, and it worked
    until I put tests in the Python one and set it up to run them when it is
    named "__main__". What happened reminded me that there are also other
    ways modules can be imported under different names, so I tried a
    different approach.

    Now the Python module imports the Pyrex module and just shoves
    references to its globals into the Pyrex module (the Pyrex module
    defines them as None). The Pyrex module doesn't import the Python
    module anymore. This also works, even when the Python module has a
    different name (e.g. "__main__"). I just feel dirty about it.
    ________________________________________________________________________
    TonyN.:' *firstname*nlsnews@georgea*lastname*.com
    ' <http://www.georgeanelson.com/>
     
    Tony Nelson, Oct 18, 2005
    #1
    1. Advertising

  2. Tony Nelson

    Tuvas Guest

    The trick is via something called Extending. Pyrex just makes extending
    a bit easier, but, depending on the complexity of the function, it
    might be easier just to extend it yourself. Basically you make a
    function that translates python into C. There are some instructions on
    the Python website, http://www.python.org . Look around, and you'll
    find lots of cool stuff on it.
     
    Tuvas, Oct 18, 2005
    #2
    1. Advertising

  3. "Tony Nelson" <*firstname*nlsnews@georgea*lastname*.com> wrote:

    > [snipped]
    >
    > I have written a Python module that uses some C functions. I wrote the
    > module in two parts, one Python, one Pyrex (C). They need to share some
    > globals.
    >


    > [snipped]
    >
    > Now the Python module imports the Pyrex module and just shoves
    > references to its globals into the Pyrex module (the Pyrex module
    > defines them as None). The Pyrex module doesn't import the Python
    > module anymore. This also works, even when the Python module has a
    > different name (e.g. "__main__"). I just feel dirty about it.


    Well, I feel dirty every time I have to share globals <wink>.

    George
     
    George Sakkis, Oct 18, 2005
    #3
    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. Vjeran Marcinko

    Mutual EJB possible?

    Vjeran Marcinko, Oct 17, 2003, in forum: Java
    Replies:
    3
    Views:
    465
    Michael Borgwardt
    Oct 17, 2003
  2. Giambattista Bloisi

    non-blocking mutual exclusion

    Giambattista Bloisi, Dec 16, 2003, in forum: Java
    Replies:
    1
    Views:
    436
    Matt Humphrey
    Dec 16, 2003
  3. Albert
    Replies:
    4
    Views:
    11,030
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    690
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    129
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page