[issue1576] [Patch] Working post import hook and lazy modules

2008-01-24 Thread Christian Heimes

Christian Heimes added the comment:

The post import hook patch is in my pep 369 branch.

--
resolution:  -> out of date
status: open -> closed

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1576] [Patch] Working post import hook and lazy modules

2008-01-08 Thread Christian Heimes

Changes by Christian Heimes:


Removed file: http://bugs.python.org/file8928/py3k_post_import_hook_lazy.patch

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1576] [Patch] Working post import hook and lazy modules

2008-01-08 Thread Christian Heimes

Changes by Christian Heimes:


Removed file: http://bugs.python.org/file8914/py3k_post_import_hook4.patch

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1576] [Patch] Working post import hook and lazy modules

2008-01-08 Thread Christian Heimes

Changes by Christian Heimes:


Removed file: http://bugs.python.org/file8909/py3k_post_import_hook3.patch

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1576] [Patch] Working post import hook and lazy modules

2008-01-08 Thread Christian Heimes

Christian Heimes added the comment:

The new patch applies against the latest svn revision. Several functions
were renamed.

Added file: http://bugs.python.org/file9112/py3k_post_import_lazy.patch

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1576] [Patch] Working post import hook and lazy modules

2007-12-11 Thread Christian Heimes

Christian Heimes added the comment:

Updates:
* Some minor cleanups
* First draft of lazy modules implemented in C. It works but it needs
some cleanup and more error checking.

--
keywords: +patch
title: First draft of a post import hook -> [Patch] Working post import hook 
and lazy modules
Added file: http://bugs.python.org/file8928/py3k_post_import_hook_lazy.patch

__
Tracker <[EMAIL PROTECTED]>

__Index: Python/import.c
===
--- Python/import.c	(Revision 59470)
+++ Python/import.c	(Arbeitskopie)
@@ -161,7 +161,7 @@
 void
 _PyImportHooks_Init(void)
 {
-	PyObject *v, *path_hooks = NULL, *zimpimport;
+	PyObject *v, *path_hooks = NULL, *zimpimport, *pihr;
 	int err = 0;
 
 	/* adding sys.path_hooks and sys.path_importer_cache, setting up
@@ -198,6 +198,14 @@
 			  );
 	}
 
+	pihr = PyDict_New();
+	if (pihr == NULL ||
+PySys_SetObject("post_import_hooks", pihr) != 0) {
+		PyErr_Print();
+		Py_FatalError("initialization of post import hook registry "
+			  "failed");
+	}
+
 	zimpimport = PyImport_ImportModule("zipimport");
 	if (zimpimport == NULL) {
 		PyErr_Clear(); /* No zip import module -- okay */
@@ -369,6 +377,7 @@
 	"path", "argv", "ps1", "ps2",
 	"last_type", "last_value", "last_traceback",
 	"path_hooks", "path_importer_cache", "meta_path",
+	"post_import_hooks",
 	NULL
 };
 
@@ -623,6 +632,228 @@
 			  "sys.modules failed");
 }
 
+/* post import hook API */
+PyObject *
+PyImport_GetPostImportHooks(void)
+{
+	PyObject *pihr;
+
+	pihr = PySys_GetObject("post_import_hooks");
+	/* This should only happen during initialization */
+	if (pihr == NULL)
+		return NULL;
+
+	if (!PyDict_Check(pihr)) {
+		PyErr_SetString(PyExc_TypeError,
+"post import registry is not a dict");
+	}
+	return pihr;
+}
+
+PyObject *
+PyImport_NotifyPostImport(PyObject *module)
+{
+	static PyObject *name = NULL;
+	PyObject *mod_name = NULL, *registry = NULL, *o;
+	PyObject *hooks = NULL, *hook, *it = NULL;
+	int status = -1;
+
+	if (name == NULL) {
+		name = PyUnicode_InternFromString("__name__");
+		if (name == NULL) {
+			return NULL;
+		}
+	}
+
+	if (module == NULL) {
+		return NULL;
+	}
+
+	if (!PyModule_Check(module)) {
+		PyErr_Format(PyExc_TypeError,
+			 "A module object was expected, got '%.200s'",
+			 Py_Type(module)->tp_name);
+		Py_DECREF(module);
+		return NULL;
+	}
+
+	if (PyModule_IsLazy(module)) {
+		/* nothing to do here */
+		return module;
+	}
+	/* XXX check if module is in sys.modules ? */
+
+	registry = PyImport_GetPostImportHooks();
+	if (registry == NULL) {
+		/* This should only happen during initialization */
+		return module;
+	}
+
+	mod_name = PyObject_GetAttr(module, name);
+	if (mod_name == NULL) {
+		goto error;
+	}
+	if (!PyUnicode_Check(mod_name)) {
+		PyObject *repr;
+		char *name;
+
+		repr = PyObject_Repr(module);
+		name = repr ? PyUnicode_AsString(repr) : "";
+		PyErr_Format(PyExc_TypeError,
+			 "Module __name__ attribute of '%.200s' is not "
+			 "string", name);
+		Py_XDECREF(repr);
+		goto error;
+	}
+
+	hooks = PyDict_GetItem(registry, mod_name);
+	if (hooks == NULL || hooks == Py_None) {
+		/* Either no hooks are defined or they are already fired */
+		if (hooks == NULL) {
+			PyErr_Clear();
+		}
+		goto end;
+	}
+	if (!PyList_Check(hooks)) {
+		PyErr_Format(PyExc_TypeError,
+			 "expected None or list of hooks, got '%.200s'",
+			 Py_Type(hooks)->tp_name);
+		goto error;
+	}
+
+	/* fire hooks */
+	it = PyObject_GetIter(hooks);
+	if (it == NULL) {
+		goto error;
+	}
+	while ((hook = PyIter_Next(it)) != NULL) {
+		o = PyObject_CallFunctionObjArgs(hook, module, NULL);
+		Py_DECREF(hook);
+		if (o == NULL) {
+			goto error;
+		}
+		Py_DECREF(o);
+	}
+
+	/* Mark hooks as fired */
+	if (PyDict_SetItem(registry, mod_name, Py_None) < 0) {
+		goto error;
+	}
+
+end:
+	status = 0;
+error:
+	Py_XDECREF(mod_name);
+	Py_XDECREF(it);
+	if (status < 0) {
+		return NULL;
+	}
+	else {
+		return module;
+	}
+}
+
+PyObject *
+PyImport_RegisterPostImportHook(PyObject *callable, PyObject *mod_name)
+{
+	PyObject *registry = NULL, *hooks = NULL;
+	int status = -1;
+
+	if (!PyCallable_Check(callable)) {
+		PyErr_SetString(PyExc_TypeError, "expected callable");
+		goto error;
+	}
+	if (!PyUnicode_Check(mod_name)) {
+		PyErr_SetString(PyExc_TypeError, "expected string");
+		goto error;
+	}
+
+	registry = PyImport_GetPostImportHooks();
+	if (registry == NULL) {
+		goto error;
+	}
+
+	lock_import();
+
+	hooks = PyDict_GetItem(registry, mod_name);
+	/* module may be already loaded, get the module object from sys */
+	if (hooks == NULL || hooks == Py_None) {
+		PyObject *o, *modules;
+		PyObject *module = NULL;
+
+		modules = PyImport_GetModuleDict();
+		if (modules == NULL) {
+			goto error;
+		}
+		module = PyObject_GetItem(modules, mod_name);
+		if (module == NULL) {
+			PyErr_Clear();
+