Nested/Sub Extensions in Python

2011-07-01 Thread H Linux
Dear all,

I am currently fighting with a problem writing a set of Python
extensions in C. I want to structure the whole package (here called
smt for sub-module test) into different sub-modules, e.g. according to
this layout:

smt.foo.func()

I can only build a module
>import foo
>print foo.func(1,2)

Once I try to nest this, I cannot get the module to load anymore:
>import smt.bar
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named bar

I have found the following hints on the web:
http://stackoverflow.com/questions/1681281/nested-python-c-extensions-modules
I have also found that XEN has a python module which does this, but
http://www.google.de/codesearch#4Wqoij9clTg/tools/python/setup.py

Still I am unable to get this to work. What am I missing? In case it
matters, this is on Ubuntu10.4/Python2.6.5 Below are listings from my
source files.

Thanks in advance for any help,
Hartwig


1. setup.py:
from distutils.core import setup, Extension
PACKAGE_NAME = 'smt'
setup(
name = PACKAGE_NAME,
version  = '0.1',
author   = 'Myself',
packages = [PACKAGE_NAME],
ext_modules  = [ Extension('foo', ['src/foo.c']),
 Extension('smt.bar', ['src/bar.c'])  ]
)

2. src/bar.c
#include 

static PyObject*
bar_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;

if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}

return PyNumber_Add(a, b);
}

static PyMethodDef bar_methods[] = {
  {"func", bar_func, METH_VARARGS, NULL},
{NULL, NULL}
};

PyMODINIT_FUNC
initbar(void)
{
Py_InitModule("smt.bar", bar_methods);
}

3. src/foo.c
#include 

static PyObject*
foo_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;
if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}
return PyNumber_Add(a, b);
}

static PyMethodDef foo_methods[] = {
{"func", foo_func, METH_VARARGS, NULL},
{NULL, NULL}
};

PyMODINIT_FUNC
initfoo(void)
{
Py_InitModule("foo", foo_methods);
}

4. Code Layout:
├── setup.py
├── smt
│   └── __init__.py
└── src
├── bar.c
└── foo.c
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested/Sub Extensions in Python

2011-07-02 Thread H Linux
On Jul 2, 3:46 am, Corey Richardson  wrote:
> Excerpts from H Linux's message of Fri Jul 01 16:02:15 -0400 2011:
>
> > Dear all,
>
> > I am currently fighting with a problem writing a set of Python
> > extensions in C.
>
> If you haven't seen it yet, Cython is a *very* nice tool for writing
> C extensions.http://cython.org/
Thanks for the tip, I'll have a look. Still fighting with plain python
though...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested/Sub Extensions in Python

2011-07-02 Thread H Linux
On Jul 2, 2:28 am, Carl Banks  wrote:
> On Friday, July 1, 2011 1:02:15 PM UTC-7, H Linux wrote:
> > Once I try to nest this, I cannot get the module to load anymore:
> > >import smt.bar
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > ImportError: No module named bar
>
> [snip]
>
> > PyMODINIT_FUNC
> > initbar(void)
> > {
> >    Py_InitModule("smt.bar", bar_methods);
> > }
>
> This should be: Py_InitModule("bar", bar_methods);
> That's probably it; other than that, it looks like you did everything right.
Thanks for your help, but I actually tried both ways. This does not
seem to be the problem, as it fails both ways with identical error
message.

> What does the installed file layout look like after running distutils setup?
Tree output is:
/usr/local/lib/python2.6/dist-packages/
├── foo.so
├── smt
│   ├── bar.so
│   ├── __init__.py
│   └── __init__.pyc
└── smt-0.1.egg-info

Just in case anyone is willing to have a look, here is a link to the
complete module as built with:
python setup.py sdist:
https://docs.google.com/leaf?id=0Byt62fSE5VC5NTgxOTFkYzQtNzI3NC00OTUzLWI1NzMtNmJjN2E0ZTViZTJi&hl=en_US

If anyone has any other ideas how to get it to work, thanks in
advance...
Hartwig


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested/Sub Extensions in Python

2011-07-03 Thread H Linux
On Jul 2, 10:43 pm, Carl Banks  wrote:
> I got and built the package, and it imported smt.bar just fine for me.
>
> So my advice would be to rename all the modules.  My guess is that there is a 
> conflict for smt and Python is importing some other module or package.  Is 
> there a file called smt.py in your working directory? ...(snip)...
> And if that is the problem, in the future be more careful to keep your module 
> namespace clean.

Thanks a lot for your effort, those suggestions pointed me to the
solution, even if my mistake was actually a bit different. The name of
the module (abbreviated "Simple Test Module" => smt), which was just
chosen for a minimal reproducible version, was not the culprit: I
unfortunately tested in the directory where I previously built/
installed the module. Result: python picked up my local "smt"
directory (see tree output in first post) instead of that from the
installed module :-(. Hence, even a more unique module name would not
have saved me.

Anyway, problem finally solved and learned a bit more about the
pitfalls of python, thanks again to your help, your efforts are
greatly appreciated,
Hartwig

-- 
http://mail.python.org/mailman/listinfo/python-list