setup() and C extensions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 7stud

    #1

    setup() and C extensions

    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])
    ----


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

    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'])],
    )
    ----


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



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

  • 7stud

    #2
    Re: setup() and C extensions

    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_No ne);
    return Py_None;

    instead of:

    return Py_BuildValue(" ");

    Comment

    • Gabriel Genellina

      #3
      Re: setup() and C extensions

      En Tue, 10 Apr 2007 03:02:22 -0300, 7stud <bbxx789_05ss@y ahoo.com>
      escribió:
      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:

      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.mod ule1.
      "name" should be the full dotted name - but I've never tried it actually.

      --
      Gabriel Genellina

      Comment

      • Gabriel Genellina

        #4
        Re: setup() and C extensions

        En Tue, 10 Apr 2007 03:35:35 -0300, 7stud <bbxx789_05ss@y ahoo.com>
        escribió:
        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 PyCFunctionWith Keywords, by example). But the C struct declaration
        (PyMethodDef) has a fixed type. See

        2) When returning None, why use the idiom:
        >
        Py_INCREF(Py_No ne);
        return Py_None;
        >
        instead of:
        >
        return Py_BuildValue(" ");
        Because it's a lot faster and clear?

        --
        Gabriel Genellina

        Comment

        • Carsten Haese

          #5
          Re: setup() and C extensions

          On Mon, 2007-04-09 at 23:35 -0700, 7stud wrote:
          2) When returning None, why use the idiom:
          >
          Py_INCREF(Py_No ne);
          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


          Comment

          Working...