Nested/Sub Extensions in Python

H

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 "<stdin>", line 1, in <module>
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 <Python.h>

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 <Python.h>

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
 
C

Corey Richardson

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/
 
H

H Linux

Excerpts from H Linux's message of Fri Jul 01 16:02:15 -0400 2011:



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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,795
Latest member
YolandaCar

Latest Threads

Top