[Distutils] Re: limiting symbols in C module

Fred L. Drake, Jr. fdrake at acm.org
Fri Sep 10 15:42:53 CEST 2004

On Friday 10 September 2004 07:22 am, Brandon Craig Rhodes wrote:
 > "M.-A. Lemburg" <mal at egenix.com> writes:
 > > There are probably a few cases where [a Python extension module
 > > defining hundreds of extra symbols] may cause problems, but in
 > > general, I don't see why distutils should try to strip symbols from
 > > C extensions.  Is there a specific problem you are trying to solve?
 > My "problem" is simply that my Python extension defines several
 > hundred symbols (some of which are not at all specific to its task,
 > like "range" and "start"), which violates section 1.12 of the
 > _Extending and Embedding_ document:

I think part of the issue is that the E&E manual was really describing the set 
of symbols introduced by the extension code, and had not really intended to 
reflect on the set of symbols provided by the external library.

That said, I don't think it *hurts* to avoid exporting more symbols than 
needed, but going to great lengths to change how distutils works is likely 
more pain than it's worth.

Also, how Python loads C extensions has changed a bit over time.  I think 
we're no longer using RTLD_GLOBAL on various Unix flavors, and I don't think 
there's a problem with this on Windows either (though I'm less sure of this).  
On Unix, you can use sys.getdlopenflags() to determine what flags are being 
use to load extensions; see the documentation for more information on this 

 > This made me concerned that, unless I took measures to remove the
 > extraneous symbols, I would be inviting lots of user complaints later
 > when "range" and "start" and so forth wound up conflicting with
 > symbols from other parts of Python on certain platforms.
 > I will happily ignore the issue if the Extending and Embedding
 > document is being overzealous; but if extraneous symbols are really
 > not much of a problem, I'm surprised that so many extension modules
 > are written as single huge .c files.

I think you'll live longer and more happily if you simply restrain the number 
of symbols you introduce in writing the binding to your C library.  If you 
only expose the symbols from the library and the init<module> function, 
you'll be doing as well as anyone else.  Think about it like this: if you 
were using the external library directly from C, you'd expect to have all the 
symbols from it available, right?


Fred L. Drake, Jr.  <fdrake at acm.org>

More information about the Distutils-SIG mailing list