summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-05-14 02:32:54 (GMT)
committerGuido van Rossum <guido@python.org>1998-05-14 02:32:54 (GMT)
commit09cae1f8cd57b24b711c3024056562e5124c3b65 (patch)
treef6a286e1e7c72547d4a4a7c26b16cd8c72310c41 /Python/import.c
parentba7cc0cfbacc099d9c6ccc35dda3c0f525e258f5 (diff)
downloadcpython-09cae1f8cd57b24b711c3024056562e5124c3b65.zip
cpython-09cae1f8cd57b24b711c3024056562e5124c3b65.tar.gz
cpython-09cae1f8cd57b24b711c3024056562e5124c3b65.tar.bz2
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)());
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/Python/import.c b/Python/import.c
index afbd4d8..6c1050f 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2266,3 +2266,61 @@ initimp()
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);
+}