Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8645 > unrolled thread

Nested/Sub Extensions in Python

Started byH Linux <hartwig.linux@gmail.com>
First post2011-07-01 13:02 -0700
Last post2011-07-02 06:36 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Nested/Sub Extensions in Python H Linux <hartwig.linux@gmail.com> - 2011-07-01 13:02 -0700
    Re: Nested/Sub Extensions in Python Corey Richardson <kb1pkl@aim.com> - 2011-07-01 21:46 -0400
      Re: Nested/Sub Extensions in Python H Linux <hartwig.linux@gmail.com> - 2011-07-02 06:36 -0700

#8645 — Nested/Sub Extensions in Python

FromH Linux <hartwig.linux@gmail.com>
Date2011-07-01 13:02 -0700
SubjectNested/Sub Extensions in Python
Message-ID<23a9eb92-65b9-4825-b23b-b01e7c04599c@q15g2000yqk.googlegroups.com>
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

[toc] | [next] | [standalone]


#8652

FromCorey Richardson <kb1pkl@aim.com>
Date2011-07-01 21:46 -0400
Message-ID<mailman.551.1309571543.1164.python-list@python.org>
In reply to#8645
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/
-- 
Corey Richardson
  "Those who deny freedom to others, deserve it not for themselves"
     -- Abraham Lincoln

[toc] | [prev] | [next] | [standalone]


#8665

FromH Linux <hartwig.linux@gmail.com>
Date2011-07-02 06:36 -0700
Message-ID<68ab655f-7ce6-41f9-aa36-fc192e8d9a3c@gv8g2000vbb.googlegroups.com>
In reply to#8652
On Jul 2, 3:46 am, Corey Richardson <kb1...@aim.com> 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...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web