diff Python/import.c @ 8330:7a1e7da922f8 legacy-trunk

New APIs for embedding applications that want to add their own entries to the table of built-in modules. This should normally be called *before* Py_Initialize(). When the malloc() or realloc() call fails, -1 is returned and the existing table is unchanged. After a similar function by Just van Rossum. int PyImport_ExtendInittab(struct _inittab *newtab); int PyImport_AppendInittab(char *name, void (*initfunc)());
author Guido van Rossum <guido@python.org>
date Thu, 14 May 1998 02:32:54 +0000
parents 2b09a5d26368
children a1bfe5bc6d8e
line wrap: on
line diff
--- a/Python/import.c	Thu May 14 02:31:26 1998 +0000
+++ b/Python/import.c	Thu May 14 02:32:54 1998 +0000
@@ -2266,3 +2266,61 @@
   failure:
 	;
 }
+
+
+/* API for embedding applications that want to add their own entries to the
+   table of built-in modules.  This should normally be called *before*
+   Py_Initialize().  When the malloc() or realloc() call fails, -1 is returned
+   and the existing table is unchanged.
+
+   After a similar function by Just van Rossum. */
+
+int
+PyImport_ExtendInittab(newtab)
+	struct _inittab *newtab;
+{
+	static struct _inittab *our_copy = NULL;
+	struct _inittab *p;
+	int i, n;
+
+	/* Count the number of entries in both tables */
+	for (n = 0; newtab[n].name != NULL; n++)
+		;
+	if (n == 0)
+		return 0; /* Nothing to do */
+	for (i = 0; PyImport_Inittab[i].name != NULL; i++)
+		;
+
+	/* Allocate new memory for the combined table */
+	if (our_copy == NULL)
+		p = malloc((i+n+1) * sizeof(struct _inittab));
+	else
+		p = realloc(our_copy, (i+n+1) * sizeof(struct _inittab));
+	if (p == NULL)
+		return -1;
+
+	/* Copy the tables into the new memory */
+	if (our_copy != PyImport_Inittab)
+		memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
+	PyImport_Inittab = our_copy = p;
+	memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
+
+	return 0;
+}
+
+/* Shorthand to add a single entry given a name and a function */
+
+int
+PyImport_AppendInittab(name, initfunc)
+	char *name;
+	void (*initfunc)();
+{
+	struct _inittab newtab[2];
+
+	memset(newtab, '\0', sizeof newtab);
+
+	newtab[0].name = name;
+	newtab[0].initfunc = initfunc;
+
+	return PyImport_ExtendInittab(newtab);
+}