Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Mutual module imports

Reply
Thread Tools

Mutual module imports

 
 
Tony Nelson
Guest
Posts: n/a
 
      10-18-2005
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/>
 
Reply With Quote
 
 
 
 
Tuvas
Guest
Posts: n/a
 
      10-18-2005
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.

 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      10-18-2005
"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


 
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
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
The imp module and cyclic imports Matthias Kramm Python 2 11-27-2005 01:03 PM
module imports and memory usage Brad Tilley Python 4 12-01-2004 06:01 PM
Enabling/disabling module imports based on a setting Adrian Holovaty Python 0 11-06-2004 06:15 PM



Advertisments