Sharing functions between C extension modules in stdlib

I have learned a long time ago that it is not enough to simply declare a function in some header file if you want to define it in one module and use in another. You have to use what now is known as PyCapsule - an array of pointers to C functions wrapped in a Python object. However, while navigating through the time/datetime maze recently I have come across timefuncs.h which seems to share _PyTime_DoubleToTimet between time and datetime modules. I did not expect this to work, but apparently the build machinery somehow knows how to place _PyTime_DoubleToTimet code in both time.so and datetime.so: $ nm build/lib.macosx-10.4-x86_64-3.2-pydebug/datetime.so | grep _PyTime_DoubleToTimet 000000000000f4e2 T __PyTime_DoubleToTimet $ nm build/lib.macosx-10.4-x86_64-3.2-pydebug/time.so | grep _PyTime_DoubleToTimet 0000000000000996 T __PyTime_DoubleToTimet I have two questions: 1) how does this happen; and 2) is this intentional? Thanks.

On Mon, Jun 14, 2010 at 6:45 PM, Alexander Belopolsky <alexander.belopolsky@gmail.com> wrote: ..
I did not expect this to work, but apparently the build machinery somehow knows how to place _PyTime_DoubleToTimet code in both time.so and datetime.so: .. I have two questions: 1) how does this happen; and 2) is this intentional?
OK, the answer to the first question is simple: in setup.py, we have exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'], libraries=math_libs) ) but if timemodule.c is compiled-in with datetime module, why is does it also need to be imported to share some other code?

$ nm build/lib.macosx-10.4-x86_64-3.2-pydebug/datetime.so | grep _PyTime_DoubleToTimet 000000000000f4e2 T __PyTime_DoubleToTimet $ nm build/lib.macosx-10.4-x86_64-3.2-pydebug/time.so | grep _PyTime_DoubleToTimet 0000000000000996 T __PyTime_DoubleToTimet
I have two questions: 1) how does this happen;
'T' means "defined in text segment", so it looks like the code is included twice. And indeed, it is: exts.append( Extension('time', ['timemodule.c'], libraries=math_libs) ) exts.append( Extension('datetime', ['datetimemodule.c', 'timemodule.c'], libraries=math_libs) )
and 2) is this intentional?
This was added with ------------------------------------------------------------------------ r36221 | bcannon | 2004-06-24 03:38:47 +0200 (Do, 24. Jun 2004) | 3 Zeilen Add compilation of timemodule.c with datetimemodule.c to get __PyTime_DoubleToTimet(). ------------------------------------------------------------------------ So it's clearly intentional. I doubt its desirable, though. If only __PyTime_DoubleToTimet needs to be duplicated, I'd rather put that function into a separate C file that gets included twice, instead of including the full timemodule.c into datetimemodule.c. Regards, Martin

On Mon, Jun 14, 2010 at 7:09 PM, "Martin v. Löwis" <martin@v.loewis.de> wrote: ..
So it's clearly intentional. I doubt its desirable, though. If only __PyTime_DoubleToTimet needs to be duplicated, I'd rather put that function into a separate C file that gets included twice, instead of including the full timemodule.c into datetimemodule.c.
Thanks for your research, Martin. I've opened an issue for this at http://bugs.python.org/issue9012 .
participants (2)
-
"Martin v. Löwis"
-
Alexander Belopolsky