Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 970a0a2

Browse files
committed
api version checking
1 parent 6da5bfa commit 970a0a2

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

Include/modsupport.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,22 @@ extern object *mkvalue();
5151
extern int vgetargs PROTO((object *, char *, va_list));
5252
extern object *vmkvalue PROTO((char *, va_list));
5353

54-
extern object *initmodule PROTO((char *, struct methodlist *));
55-
extern object *initmodule3 PROTO((char *, struct methodlist *,
56-
char *, object *));
54+
#define PYTHON_API_VERSION 1001
55+
/* The API version is maintained (independently from the Python version)
56+
so we can detect mismatches between the interpreter and dynamically
57+
loaded modules.
58+
59+
Please add a line or two to the top of this log for each API
60+
version change:
61+
62+
9-Jan-1995 GvR Initial version (incompatible with older API)
63+
*/
64+
65+
extern object *initmodule4 PROTO((char *, struct methodlist *,
66+
char *, object *, int));
67+
#define initmodule(name, methods) \
68+
initmodule4(name, methods, (char *)NULL, (object *)NULL, \
69+
PYTHON_API_VERSION)
5770

5871
/* The following are obsolete -- use getargs directly! */
5972
#define getnoarg(v) getargs(v, "")

Python/modsupport.c

+16-14
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,32 @@ typedef extended va_double;
3333
typedef double va_double;
3434
#endif
3535

36-
/* initmodule3() has two additional parameters:
37-
- doc is the documentation string;
38-
- passthrough is passed as self to functions defined in the module.
36+
/* initmodule4() parameters:
37+
- name is the module name
38+
- methods is the list of top-level functions
39+
- doc is the documentation string
40+
- passthrough is passed as self to functions defined in the module
41+
- api_version is the value of PYTHON_API_VERSION at the time the
42+
module was compiled
3943
*/
4044

45+
static char api_version_warning[] =
46+
"WARNING: Python C API version mismatch for module %s:\n\
47+
This Python has API version %d, module %s has version %s.\n";
48+
4149
object *
42-
initmodule3(name, methods, doc, passthrough)
50+
initmodule4(name, methods, doc, passthrough, module_api_version)
4351
char *name;
4452
struct methodlist *methods;
4553
char *doc;
4654
object *passthrough;
55+
int module_api_version;
4756
{
4857
object *m, *d, *v;
4958
struct methodlist *ml;
59+
if (module_api_version != PYTHON_API_VERSION)
60+
fprintf(stderr, api_version_warning,
61+
name, PYTHON_API_VERSION, name, module_api_version);
5062
if ((m = add_module(name)) == NULL) {
5163
fprintf(stderr, "initializing module: %s\n", name);
5264
fatal("can't create a module");
@@ -69,16 +81,6 @@ initmodule3(name, methods, doc, passthrough)
6981
return m;
7082
}
7183

72-
/* The standard initmodule() passes NULL for 'self' */
73-
74-
object *
75-
initmodule(name, methods)
76-
char *name;
77-
struct methodlist *methods;
78-
{
79-
return initmodule3(name, methods, (char *)NULL, (object *)NULL);
80-
}
81-
8284

8385
/* Helper for mkvalue() to scan the length of a format */
8486

0 commit comments

Comments
 (0)