This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add Py_AtInit() startup hook for extenders
Type: enhancement Stage: test needed
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, loewis, patmiller, terry.reedy
Priority: normal Keywords: patch

Created on 2003-04-30 22:26 by patmiller, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Py_AtInit.diff patmiller, 2003-04-30 22:26 Patch to add Py_AtInit()
Messages (7)
msg43551 - (view) Author: Patrick Miller (patmiller) Date: 2003-04-30 22:26
I work on several projects that have initialization
requirements that
need to grab control after Py_Initialize(), but before
any user code
runs (via input, script, -c, etc...).

Note that these are Python clones that take advantage
of an installed
python (using its $prefix/lib/pythonx.x/*.py and
site-packages/*)

We could use 

PyImport_AppendInittab("sitecustomize",initsitecustomize);

But if there already IS customization in
sitecustomize.py, I've
blown it away (and have to look it up and force an
import).
And if someone uses the -S flag, I'm screwed.

I propose a hook styled after Py_AtExit(func) called
Py_AtInit(func)
which maintains a list of functions that are called in
Py_Initialize
right after main and site initializations.

If the hook isn't used, then the cost is a single extra
function
call at initialization.  Here's a spurious example:  A
customer wants
a version of python that has all the math functions and
his
extensions to act like builtins...

I would write (without refcnt or error checks ;-):

#include "Python.h"
static void after_init(void) {
    PyObject
*builtin,*builtin_dict,*math,*math_dict,*user,*user_dict;

    builtin = PyImport_ImportModule("__builtin__");
    builtin_dict = PyModule_GetDict(builtin);
    math = PyImport_ImportModule("math");
    math_dict = PyModule_GetDict(math);
    user = PyImport_ImportModule("user");
    user_dict = PyModule_GetDict(math);

    PyDict_Update(builtin_dictionary, math_dict);
    PyDict_Update(builtin_dictionary, user_dict);
}


int main(int argc, char** argv) {
    PyImport_AppendInittab("user",inituser);
    Py_AtInit(after_init);

    return Py_Main(argc, argv);
}

voila!  An extended Python with new builtins.
This is vastly better than hacking in through
site.py or sitecustomize

I actually want this to do some MPI initialization to
setup a
single user prompt with broadcast which has to run
after
Py_Initialize() but before the import of readline.



msg43552 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-04-29 13:10
The patch looks good to me in principle. However, I wonder why you run the init functions after importing site; if you want it before any user-defined code, shouldn't you run it before site?

Also, can you please provide documentation patches?
msg43553 - (view) Author: Patrick Miller (patmiller) Date: 2007-04-29 14:54
Martin,

You are absolutely right, the call to initinitialize() should occur before
initsite().

I'll prepare a new patch with that change and also include doc updates.

Pat
msg59201 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-04 01:18
Where is the new patch? :)
msg87658 - (view) Author: Patrick Miller (patmiller) Date: 2009-05-12 20:06
Thanks... I'll submit patches for 2.6, 2.7, and 3.2

On Tue, May 12, 2009 at 2:07 PM, Daniel Diniz <report@bugs.python.org> wrote:
>
> Changes by Daniel Diniz <ajaksu@gmail.com>:
>
>
> ----------
> stage:  -> test needed
> versions: +Python 2.7, Python 3.2 -Python 2.6
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue730473>
> _______________________________________
>
msg162995 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-06-16 21:33
Patrick, its been 5 years since you promised a 2nd patch (which would now have to be against the current 3.3 tip). If you have lost interested, perhaps we should close this.
msg163001 - (view) Author: Patrick Miller (patmiller) Date: 2012-06-17 00:11
Just languishing for lo on these 5 years....
History
Date User Action Args
2022-04-10 16:08:29adminsetgithub: 38410
2012-06-17 00:11:42patmillersetstatus: open -> closed
resolution: out of date
messages: + msg163001
2012-06-16 21:33:38terry.reedysetnosy: + terry.reedy

messages: + msg162995
versions: + Python 3.4, - Python 3.2
2010-08-09 18:36:37terry.reedysetversions: - Python 2.7
2009-05-12 20:06:01patmillersetmessages: + msg87658
2009-05-12 18:07:12ajaksu2setstage: test needed
versions: + Python 2.7, Python 3.2, - Python 2.6
2008-01-04 01:18:41christian.heimessetnosy: + christian.heimes
type: enhancement
messages: + msg59201
versions: + Python 2.6, - Python 2.3
2003-04-30 22:26:34patmillercreate