Issue 730473: Add Py_AtInit() startup hook for extenders (original) (raw)
Created on 2003-04-30 22:26 by patmiller, last changed 2022-04-10 16:08 by admin. This issue is now closed.
Messages (7)
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.
Author: Martin v. Löwis (loewis) *
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?
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
Author: Christian Heimes (christian.heimes) *
Date: 2008-01-04 01:18
Where is the new patch? :)
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>
Author: Terry J. Reedy (terry.reedy) *
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.
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:29
admin
set
github: 38410
2012-06-17 00:11:42
patmiller
set
status: open -> closed
resolution: out of date
messages: +
2012-06-16 21:33:38
terry.reedy
set
nosy: + terry.reedy
messages: +
versions: + Python 3.4, - Python 3.2
2010-08-09 18:36:37
terry.reedy
set
versions: - Python 2.7
2009-05-12 20:06:01
patmiller
set
messages: +
2009-05-12 18:07:12
ajaksu2
set
stage: test needed
versions: + Python 2.7, Python 3.2, - Python 2.6
2008-01-04 01🔞41
christian.heimes
set
nosy: + christian.heimes
type: enhancement
messages: +
versions: + Python 2.6, - Python 2.3
2003-04-30 22:26:34
patmiller
create