From 09cae1f8cd57b24b711c3024056562e5124c3b65 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 14 May 1998 02:32:54 +0000 Subject: [PATCH] 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)()); --- Python/import.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Python/import.c b/Python/import.c index afbd4d8ba3b..6c1050f0c69 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); +}