setup() and C extensions

7

7stud

Hi,

I can't find any documentation on the setup() function in the
distutils.core module; specifically I want to know what the 'name'
argument does. In some examples in the python docs, they use the name
argument like this:
----
from distutils.core import setup, Extension

module1 = Extension('demo',
sources = ['demo.c'])

setup (name = 'PackageName',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1])
----
http://python.org/doc/current/ext/building.html#building

So it looks like the 'name' argument should be a package name.
However, when I compile an extension module using that format, I can
import the module using the syntax:

import module1

I don't have to use PackageName.module1.

On the other hand, in another example in the python docs, they do
this:
---
from distutils.core import setup, Extension

setup(name='foo',
version='1.0',
ext_modules=[Extension('foo', ['foo.c'])],
)
----
http://python.org/doc/current/dist/describing-extensions.html

In that example, the name argument matches the module name in the
Extension constructor. A similar example by Alex Martelli can be
found at:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66509

So what is the name argument in setup() used for?
 
7

7stud

Also:

1) When you create a C array to map python names to the C functions
that you defined:

static PyMethodDef MyFunctions[] =
{
{"my_calc", (PyCFunction)my_func, METH_VARARGS, "my very speedy c
function"},
{NULL, NULL, 0, NULL}
};

Why do you need to cast my_func to PyCFunction?


2) When returning None, why use the idiom:

Py_INCREF(Py_None);
return Py_None;

instead of:

return Py_BuildValue("");
 
G

Gabriel Genellina

I can't find any documentation on the setup() function in the
distutils.core module; specifically I want to know what the 'name'
argument does. In some examples in the python docs, they use the name
argument like this:
http://docs.python.org/dist/module-distutils.core.html

So it looks like the 'name' argument should be a package name.
Exactly.

However, when I compile an extension module using that format, I can
import the module using the syntax:

import module1

I don't have to use PackageName.module1.

"name" should be the full dotted name - but I've never tried it actually.
 
G

Gabriel Genellina

1) When you create a C array to map python names to the C functions
that you defined:

static PyMethodDef MyFunctions[] =
{
{"my_calc", (PyCFunction)my_func, METH_VARARGS, "my very speedy c
function"},
{NULL, NULL, 0, NULL}
};

Why do you need to cast my_func to PyCFunction?

Because it does not *have* to be a PyCFunction; the ml_flags field is used
to indicate exactly what kind of function it is (METH_KEYWORDS indicating
a PyCFunctionWithKeywords, by example). But the C struct declaration
(PyMethodDef) has a fixed type. See
http://docs.python.org/api/common-structs.html
2) When returning None, why use the idiom:

Py_INCREF(Py_None);
return Py_None;

instead of:

return Py_BuildValue("");

Because it's a lot faster and clear?
 
C

Carsten Haese

2) When returning None, why use the idiom:

Py_INCREF(Py_None);
return Py_None;

instead of:

return Py_BuildValue("");

As Gabriel said, the preferred idiom is faster and clearer. Sufficiently
recent Pythons define the macro Py_RETURN_NONE for the preferred idiom.

-Carsten
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top