Differences between revisions 1 and 2
Revision 1 as of 2006-12-06 07:31:33
Size: 2837
Editor: c-24-6-89-208
Comment:
Revision 2 as of 2007-10-02 01:07:52
Size: 2895
Editor: caramel
Comment: 'exam' -> 'examp', since "exam" is a confusing abbreviation
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
We'll be creating a project called 'exam' (short for 'example'.) We'll be creating a project called 'examp' (short for 'example'.)
Line 9: Line 9:
{{{> mkdir exam
> cd exam
{{{> mkdir examp
> cd examp
Line 16: Line 16:
Line 21: Line 22:
    name="exam",     name="examp",
Line 27: Line 28:
    test_suite = "test.exam_unittest",     test_suite = "test.examp_unittest",
Line 32: Line 33:
          'exam', # Python name of the module
          ['src/exam.c'] # Source files to build
          'examp', # Python name of the module
          ['src/examp.c'] # Source files to build
Line 35: Line 36:
    )}}}     )
}}}
Line 41: Line 43:
This will be located in the location specified in the {{{setup.py}}} script given above; in this example, the location is {{{exam/src/exam.c}}}. This file will contain the following: This will be located in the location specified in the {{{setup.py}}} script given above; in this example, the location is {{{examp/src/examp.c}}}. This file will contain the following:
Line 46: Line 48:
PyMODINIT_FUNC initexam(void) PyMODINIT_FUNC initexamp(void)
Line 50: Line 52:
    m = Py_InitModule( "exam", NULL );     m = Py_InitModule( "examp", NULL );
Line 58: Line 60:
Create an empty __init.py__ file in exam/test. Create an empty __init.py__ file in examp/test.
Line 60: Line 62:
{{{>mkdir test {{{
> mkdir test
Line 66: Line 69:
This will be located in {{{exam/test/exam_unittest.py}}}. This file will contain the following. This will be located in {{{examp/test/examp_unittest.py}}}. This file will contain the following.
Line 76: Line 79:
            import exam             import examp
Line 85: Line 88:
exam/ examp/
Line 88: Line 91:
    exam.c     examp.c
Line 91: Line 94:
    exam_unittest.py     examp_unittest.py
Line 101: Line 104:
----
CategoryDistutilsCookbook

Creating a Python Extension Module from Scratch

This guide will take you through the process of creating a Python extension module. It is assumed that you have a recent Python version (2.4 or later) and setuptools installed.

We'll be creating a project called 'examp' (short for 'example'.)

Step 1: Create the project directory

{{{> mkdir examp > cd examp }}}

Step 2: Create the setup.py script

In the directory you just created, make a new file, setup.py containing the following:

   1 from setuptools import setup, Extension
   2 
   3 setup(
   4     # Name of this package
   5     name="examp",
   6     
   7     # Package version
   8     version=0.1,
   9     
  10     # This tells setup how to find our unit tests.
  11     test_suite = "test.examp_unittest",
  12     
  13     # Describes how to build the actual extension module from C source files.
  14     ext_modules = [
  15         Extension(
  16           'examp',         # Python name of the module
  17           ['src/examp.c']  # Source files to build
  18         )]
  19     )

The setup.py file tells Python how to compile, test, and install your extension module.

Step 3: Create the module source file

This will be located in the location specified in the setup.py script given above; in this example, the location is examp/src/examp.c. This file will contain the following:

   1 #include <Python.h>
   2 
   3 PyMODINIT_FUNC initexamp(void)
   4 {
   5     PyObject *m;
   6     
   7     m = Py_InitModule( "examp", NULL );
   8 }

As you can see, this is a pretty minimal extension module - it does nothing but establish that there is, in fact, a module. We'll add more to this later, but for now, let's just see if we can get it to compile and run.

Step 4: Create the unit test module

Create an empty init.py file in examp/test.

> mkdir test
> touch test/__init__.py

Step 5: Create the unit test source file

This will be located in examp/test/examp_unittest.py. This file will contain the following.

   1 import unittest
   2 import doctest
   3 
   4 class DeviceTest( unittest.TestCase ):
   5     # This is a simple test that just tries to load the module
   6     def runTest( self ):
   7         try:
   8             import examp
   9         except ImportError, e:
  10             self.Fail( str( e ) )

The setup.py will automatically scan this file for unit test cases (subclasses of unittest.TestCase).

At this point, your directory structure should look like this:

examp/
  setup.py
  src/
    examp.c
  test/
    __init__.py
    examp_unittest.py

Step 6: Build and test the module

> python setup.py test

If everything is correct, your extension module should have built, and you should see a message on the console telling you that your unit test has passed.


CategoryDistutilsCookbook

ExtensionTutorial (last edited 2009-01-25 18:34:25 by 61)

Unable to edit the page? See the FrontPage for instructions.