Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2afffd4

Browse files
committed
Wrote the "Describing extension modules" section.
1 parent 3a58420 commit 2afffd4

1 file changed

Lines changed: 193 additions & 4 deletions

File tree

Doc/dist/dist.tex

Lines changed: 193 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ \section{Writing the Setup Script}
274274
path, the opposite of the Mac OS convention with colons).
275275

276276

277-
\subsection{Package directories}
278-
\label{package-dirs}
277+
\subsection{Listing whole packages}
278+
\label{listing-packages}
279279

280280
The \option{packages} option tells the Distutils to process (build,
281281
distribute, install, etc.) all pure Python modules found in each package
@@ -340,13 +340,202 @@ \subsection{Listing individual modules}
340340
other in the \module{pkg} package. Again, the default package/directory
341341
layout implies that these two modules can be found in \file{mod1.py} and
342342
\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
343-
And again, you can override the package/directory layout using the
344-
\option{package\_dir} option.
343+
And again, you can override the package/directory correspondence using
344+
the \option{package\_dir} option.
345345

346346

347347
\subsection{Describing extension modules}
348348
\label{sec:describing-extensions}
349349

350+
Just as writing Python extension modules is a bit more complicated than
351+
writing pure Python modules, describing them to the Distutils is a bit
352+
more complicated. Unlike pure modules, it's not enough just to list
353+
modules or packages and expect the Distutils to go out and find the
354+
right files; you have to specify the extension name, source file(s), and
355+
any compile/link requirements (include directories, libraries to link
356+
with, etc.).
357+
358+
All of this is done through another keyword argument to
359+
\function{setup()}, the \option{extensions} option. \option{extensions}
360+
is just a list of \class{Extension} instances, each of which describes a
361+
single extension module. Suppose your distribution includes a single
362+
extension, called \module{foo} and implemented by \file{foo.c}. If no
363+
additional instructions to the compiler/linker are needed, describing
364+
this extension is quite simple:
365+
\begin{verbatim}
366+
Extension("foo", ["foo.c"])
367+
\end{verbatim}
368+
The \class{Extension} class can be imported from
369+
\module{distutils.core}, along with \function{setup()}. Thus, the setup
370+
script for a module distribution that contains only this one extension
371+
and nothing else might be:
372+
\begin{verbatim}
373+
from distutils.core import setup, Extension
374+
setup(name = "foo", version = "1.0",
375+
extensions = [Extension("foo", ["foo.c"])])
376+
\end{verbatim}
377+
378+
The \class{Extension} class (actually, the underlying extension-building
379+
machinery implemented by the \command{built\_ext} command) supports a
380+
great deal of flexibility in describing Python extensions, which is
381+
explained in the following sections.
382+
383+
384+
\subsubsection{Extension names and packages}
385+
386+
The first argument to the \class{Extension} constructor is always the
387+
name of the extension, including any package names. For example,
388+
\begin{verbatim}
389+
Extension("foo", ["src/foo1.c", "src/foo2.c"])
390+
\end{verbatim}
391+
describes an extension that lives in the root package, while
392+
\begin{verbatim}
393+
Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
394+
\end{verbatim}
395+
describes the same extension in the \module{pkg} package. The source
396+
files and resulting object code are identical in both cases; the only
397+
difference is where in the filesystem (and therefore where in Python's
398+
namespace hierarchy) the resulting extension lives.
399+
400+
If you have a number of extensions all in the same package (or all under
401+
the same base package), use the \option{ext\_package} keyword argument
402+
to \function{setup()}. For example,
403+
\begin{verbatim}
404+
setup(...
405+
ext_package = "pkg",
406+
extensions = [Extension("foo", ["foo.c"]),
407+
Extension("subpkg.bar", ["bar.c"])]
408+
)
409+
\end{verbatim}
410+
will compile \file{foo.c} to the extension \module{pkg.foo}, and
411+
\file{bar.c} to \module{pkg.subpkg.bar}.
412+
413+
414+
\subsubsection{Extension source files}
415+
416+
The second argument to the \class{Extension} constructor is a list of
417+
source files. Since the Distutils currently only support C/C++
418+
extensions, these are normally C/C++ source files. (Be sure to use
419+
appropriate extensions to distinguish C++ source files: \file{.cc} and
420+
\file{.cpp} seem to be recognized by both Unix and Windows compilers.)
421+
422+
However, you can also include SWIG interface (\file{.i}) files in the
423+
list; the \command{build\_ext} command knows how to deal with SWIG
424+
extensions: it will run SWIG on the interface file and compile the
425+
resulting C/C++ file into your extension.
426+
427+
\XXX{SWIG support is rough around the edges and largely untested;
428+
especially SWIG support of C++ extensions! Explain in more detail
429+
here when the interface firms up.}
430+
431+
On some platforms, you can include non-source files that are processed
432+
by the compiler and included in your extension. Currently, this just
433+
means Windows resource files for Visual C++. \XXX{get more detail on
434+
this feature from Thomas Heller!}
435+
436+
437+
\subsubsection{Preprocessor options}
438+
439+
Three optional arguments to \class{Extension} will help if you need to
440+
specify include directories to search or preprocessor macros to
441+
define/undefine: \code{include\_dirs}, \code{define\_macros}, and
442+
\code{undef\_macros}.
443+
444+
For example, if your extension requires header files in the
445+
\file{include} directory under your distribution root, use the
446+
\code{include\_dirs} option:
447+
\begin{verbatim}
448+
Extension("foo", ["foo.c"], include_dirs=["include"])
449+
\end{verbatim}
450+
451+
You can specify absolute directories there; if you know that your
452+
extension will only be built on Unix systems with X11R6 installed to
453+
\file{/usr}, you can get away with
454+
\begin{verbatim}
455+
Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
456+
\end{verbatim}
457+
You should avoid this sort of non-portable usage if you plan to
458+
distribute your code: it's probably better to write your code to include
459+
(e.g.) \code{<X11/Xlib.h>}.
460+
461+
If you need to include header files from some other Python extension,
462+
you can take advantage of the fact that the Distutils install extension
463+
header files in a consistent way. For example, the Numerical Python
464+
header files are installed (on a standard Unix installation) to
465+
\file{/usr/local/include/python1.5/Numerical}. (The exact location will
466+
differ according to your platform and Python installation.) Since the
467+
Python include directory---\file{/usr/local/include/python1.5} in this
468+
case---is always included in the search path when building Python
469+
extensions, the best approach is to include (e.g.)
470+
\code{<Numerical/arrayobject.h>}. If you insist on putting the
471+
\file{Numerical} include directory right into your header search path,
472+
though, you can find that directory using the Distutils
473+
\module{sysconfig} module:
474+
\begin{verbatim}
475+
from distutils.sysconfig import get_python_inc
476+
incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
477+
setup(...,
478+
Extension(..., include_dirs=[incdir]))
479+
\end{verbatim}
480+
Even though this is quite portable---it will work on any Python
481+
installation, regardless of platform---it's probably easier to just
482+
write your C code in the sensible way.
483+
484+
You can define and undefine pre-processor macros with the
485+
\code{define\_macros} and \code{undef\_macros} options.
486+
\code{define\_macros} takes a list of \code{(name, value)} tuples, where
487+
\code{name} is the name of the macro to define (a string) and
488+
\code{value} is its value: either a string or \code{None}. (Defining a
489+
macro \code{FOO} to \code{None} is the equivalent of a bare
490+
\code{\#define FOO} in your C source: with most compilers, this sets
491+
\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
492+
a list of macros to undefine.
493+
494+
For example:
495+
\begin{verbatim}
496+
Extension(...,
497+
define_macros=[('NDEBUG', '1')],
498+
('HAVE_STRFTIME', None),
499+
undef_macros=['HAVE_FOO', 'HAVE_BAR'])
500+
\end{verbatim}
501+
is the equivalent of having this at the top of every C source file:
502+
\begin{verbatim}
503+
#define NDEBUG 1
504+
#define HAVE_STRFTIME
505+
#undef HAVE_FOO
506+
#undef HAVE_BAR
507+
\end{verbatim}
508+
509+
510+
\subsubsection{Library options}
511+
512+
You can also specify the libraries to link against when building your
513+
extension, and the directories to search for those libraries. The
514+
\code{libraries} option is a list of libraries to link against,
515+
\code{library\_dirs} is a list of directories to search for libraries at
516+
link-time, and \code{runtime\_library\_dirs} is a list of directories to
517+
search for shared (dynamically loaded) libraries at run-time.
518+
519+
For example, if you need to link against libraries known to be in the
520+
standard library search path on target systems
521+
\begin{verbatim}
522+
Extension(...,
523+
libraries=["gdbm", "readline"])
524+
\end{verbatim}
525+
526+
If you need to link with libraries in a non-standard location, you'll
527+
have to include the location in \code{library\_dirs}:
528+
\begin{verbatim}
529+
Extension(...,
530+
library_dirs=["/usr/X11R6/lib"],
531+
libraries=["X11", "Xt"])
532+
\end{verbatim}
533+
(Again, this sort of non-portable construct should be avoided if you
534+
intend to distribute your code.)
535+
536+
\XXX{still undocumented: extra\_objects, extra\_compile\_args,
537+
extra\_link\_args, export\_symbols---none of which are frequently
538+
needed, some of which might be completely unnecessary!}
350539

351540

352541
\section{Writing the Setup Configuration File}

0 commit comments

Comments
 (0)