Skip to content

Commit 8235ea1

Browse files
committed
Land Patch [ 566100 ] Rationalize DL_IMPORT and DL_EXPORT.
1 parent b881698 commit 8235ea1

File tree

12 files changed

+209
-256
lines changed

12 files changed

+209
-256
lines changed

Doc/ext/extending.tex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ \section{Intermezzo: Errors and Exceptions
212212
the error checking for now):
213213

214214
\begin{verbatim}
215-
void
215+
PyMODINIT_FUNC
216216
initspam(void)
217217
{
218218
PyObject *m;
@@ -240,6 +240,7 @@ \section{Intermezzo: Errors and Exceptions
240240
Should it become a dangling pointer, C code which raises the exception
241241
could cause a core dump or other unintended side effects.
242242

243+
We discuss the use of PyMODINIT_FUNC later in this sample.
243244

244245
\section{Back to the Example
245246
\label{backToExample}}
@@ -339,14 +340,16 @@ \section{The Module's Method Table and Initialization Function
339340
the module file:
340341

341342
\begin{verbatim}
342-
void
343+
PyMODINIT_FUNC
343344
initspam(void)
344345
{
345346
(void) Py_InitModule("spam", SpamMethods);
346347
}
347348
\end{verbatim}
348349

349-
Note that for \Cpp, this method must be declared \code{extern "C"}.
350+
Note that PyMODINIT_FUNC declares the function as \code{void} return type,
351+
declares any special linkage declarations required by the platform, and for
352+
\Cpp declares the function as \code{extern "C"}.
350353

351354
When the Python program imports module \module{spam} for the first
352355
time, \cfunction{initspam()} is called. (See below for comments about
@@ -1263,7 +1266,7 @@ \section{Providing a C API for an Extension Module
12631266
the C API pointer array:
12641267

12651268
\begin{verbatim}
1266-
void
1269+
PyMODINIT_FUNC
12671270
initspam(void)
12681271
{
12691272
PyObject *m;
@@ -1352,7 +1355,7 @@ \section{Providing a C API for an Extension Module
13521355
function:
13531356

13541357
\begin{verbatim}
1355-
void
1358+
PyMODINIT_FUNC
13561359
initclient(void)
13571360
{
13581361
PyObject *m;

Include/import.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@
77
extern "C" {
88
#endif
99

10-
DL_IMPORT(long) PyImport_GetMagicNumber(void);
11-
DL_IMPORT(PyObject *) PyImport_ExecCodeModule(char *name, PyObject *co);
12-
DL_IMPORT(PyObject *) PyImport_ExecCodeModuleEx(
10+
PyAPI_FUNC(long) PyImport_GetMagicNumber(void);
11+
PyAPI_FUNC(PyObject *) PyImport_ExecCodeModule(char *name, PyObject *co);
12+
PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleEx(
1313
char *name, PyObject *co, char *pathname);
14-
DL_IMPORT(PyObject *) PyImport_GetModuleDict(void);
15-
DL_IMPORT(PyObject *) PyImport_AddModule(char *name);
16-
DL_IMPORT(PyObject *) PyImport_ImportModule(char *name);
17-
DL_IMPORT(PyObject *) PyImport_ImportModuleEx(
14+
PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void);
15+
PyAPI_FUNC(PyObject *) PyImport_AddModule(char *name);
16+
PyAPI_FUNC(PyObject *) PyImport_ImportModule(char *name);
17+
PyAPI_FUNC(PyObject *) PyImport_ImportModuleEx(
1818
char *name, PyObject *globals, PyObject *locals, PyObject *fromlist);
19-
DL_IMPORT(PyObject *) PyImport_Import(PyObject *name);
20-
DL_IMPORT(PyObject *) PyImport_ReloadModule(PyObject *m);
21-
DL_IMPORT(void) PyImport_Cleanup(void);
22-
DL_IMPORT(int) PyImport_ImportFrozenModule(char *);
19+
PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name);
20+
PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m);
21+
PyAPI_FUNC(void) PyImport_Cleanup(void);
22+
PyAPI_FUNC(int) PyImport_ImportFrozenModule(char *);
2323

24-
extern DL_IMPORT(PyObject *)_PyImport_FindExtension(char *, char *);
25-
extern DL_IMPORT(PyObject *)_PyImport_FixupExtension(char *, char *);
24+
extern PyAPI_FUNC(PyObject *)_PyImport_FindExtension(char *, char *);
25+
extern PyAPI_FUNC(PyObject *)_PyImport_FixupExtension(char *, char *);
2626

2727
struct _inittab {
2828
char *name;
2929
void (*initfunc)(void);
3030
};
3131

32-
extern DL_IMPORT(struct _inittab *) PyImport_Inittab;
32+
extern PyAPI_DATA(struct _inittab *) PyImport_Inittab;
3333

34-
extern DL_IMPORT(int) PyImport_AppendInittab(char *name, void (*initfunc)(void));
35-
extern DL_IMPORT(int) PyImport_ExtendInittab(struct _inittab *newtab);
34+
extern PyAPI_FUNC(int) PyImport_AppendInittab(char *name, void (*initfunc)(void));
35+
extern PyAPI_FUNC(int) PyImport_ExtendInittab(struct _inittab *newtab);
3636

3737
struct _frozen {
3838
char *name;
@@ -43,7 +43,7 @@ struct _frozen {
4343
/* Embedding apps may change this pointer to point to their favorite
4444
collection of frozen modules: */
4545

46-
extern DL_IMPORT(struct _frozen *) PyImport_FrozenModules;
46+
extern PyAPI_DATA(struct _frozen *) PyImport_FrozenModules;
4747

4848
#ifdef __cplusplus
4949
}

Include/pyport.h

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,82 @@ extern int fsync(int fd);
379379
extern double hypot(double, double);
380380
#endif
381381

382-
#ifndef __CYGWIN__
383-
#ifndef DL_IMPORT /* declarations for DLL import */
384-
#define DL_IMPORT(RTYPE) RTYPE
385-
#endif
386-
#else /* __CYGWIN__ */
387-
#ifdef USE_DL_IMPORT
388-
#define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
389-
#define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
390-
#else /* !USE_DL_IMPORT */
391-
#define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
392-
#define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
393-
#endif /* USE_DL_IMPORT */
394-
#endif /* __CYGWIN__ */
382+
/* Declarations for symbol visibility.
383+
384+
PyAPI_FUNC(type): Declares a public Python API function and return type
385+
PyAPI_DATA(type): Declares public Python data and its type
386+
PyMODINIT_FUNC: A Python module init function. If these functions are
387+
inside the Python core, they are private to the core.
388+
If in an extension module, it may be declared with
389+
external linkage depending on the platform.
390+
391+
As a number of platforms support/require "__declspec(dllimport/dllexport)",
392+
we support a HAVE_DECLSPEC_DLL macro to save duplication.
393+
*/
394+
395+
/*
396+
All windows ports, except cygwin, are handled in PC/pyconfig.h
397+
BeOS is only other autoconf platform requiring special linkage handling
398+
and both these use __declspec()
399+
*/
400+
#if defined(__CYGWIN__) || defined(__BEOS__)
401+
# define HAVE_DECLSPEC_DLL
402+
#endif
403+
404+
#if defined(Py_ENABLE_SHARED) /* only get special linkage if built as shared */
405+
# if defined(HAVE_DECLSPEC_DLL)
406+
# ifdef Py_BUILD_CORE
407+
# define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
408+
# define PyAPI_DATA(RTYPE) __declspec(dllexport) RTYPE
409+
/* module init functions inside the core need no external linkage */
410+
# define PyMODINIT_FUNC void
411+
# else /* Py_BUILD_CORE */
412+
/* Building an extension module, or an embedded situation */
413+
/* public Python functions and data are imported */
414+
# define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
415+
# define PyAPI_DATA(RTYPE) __declspec(dllimport) RTYPE
416+
/* module init functions outside the core must be exported */
417+
# if defined(__cplusplus)
418+
# define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
419+
# else /* __cplusplus */
420+
# define PyMODINIT_FUNC __declspec(dllexport) void
421+
# endif /* __cplusplus */
422+
# endif /* Py_BUILD_CORE */
423+
# endif /* HAVE_DECLSPEC */
424+
#endif /* Py_ENABLE_SHARED */
425+
426+
/* If no external linkage macros defined by now, create defaults */
427+
#ifndef PyAPI_FUNC
428+
# define PyAPI_FUNC(RTYPE) RTYPE
429+
#endif
430+
#ifndef PyAPI_DATA
431+
# define PyAPI_DATA(RTYPE) RTYPE
432+
#endif
433+
#ifndef PyMODINIT_FUNC
434+
# if defined(__cplusplus)
435+
# define PyMODINIT_FUNC extern "C" void
436+
# else /* __cplusplus */
437+
# define PyMODINIT_FUNC void
438+
# endif /* __cplusplus */
439+
#endif
440+
441+
/* Deprecated DL_IMPORT and DL_EXPORT macros */
442+
#if defined(Py_ENABLE_SHARED) && defined (HAVE_DECLSPEC_DLL)
443+
# if defined(Py_BUILD_CORE)
444+
# define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
445+
# define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
446+
# else
447+
# define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
448+
# define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
449+
# endif
450+
#endif
451+
#ifndef DL_EXPORT
452+
# define DL_EXPORT(RTYPE) RTYPE
453+
#endif
454+
#ifndef DL_IMPORT
455+
# define DL_IMPORT(RTYPE) RTYPE
456+
#endif
457+
/* End of deprecated DL_* macros */
395458

396459
/* If the fd manipulation macros aren't defined,
397460
here is a set that should do the job */
@@ -458,15 +521,6 @@ typedef struct fd_set {
458521
#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
459522
#endif
460523

461-
/*
462-
* Rename some functions for the Borland compiler
463-
*/
464-
#ifdef __BORLANDC__
465-
# include <io.h>
466-
# define _chsize chsize
467-
# define _setmode setmode
468-
#endif
469-
470524
#ifdef __cplusplus
471525
}
472526
#endif

Makefile.pre.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ LINKFORSHARED= @LINKFORSHARED@
6565
# Extra C flags added for building the interpreter object files.
6666
CFLAGSFORSHARED=@CFLAGSFORSHARED@
6767
# C flags used for building the interpreter object files
68-
PY_CFLAGS= $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED)
68+
PY_CFLAGS= $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE
6969

7070

7171
# Machine-dependent subdirectories
@@ -420,10 +420,10 @@ Parser/metagrammar.o: $(srcdir)/Parser/metagrammar.c
420420
Python/compile.o Python/symtable.o: $(GRAMMAR_H)
421421

422422
Python/getplatform.o: $(srcdir)/Python/getplatform.c
423-
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
423+
$(CC) -c $(PY_CFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
424424

425425
Python/importdl.o: $(srcdir)/Python/importdl.c
426-
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
426+
$(CC) -c $(PY_CFLAGS) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
427427

428428
Objects/unicodectype.o: $(srcdir)/Objects/unicodectype.c \
429429
$(srcdir)/Objects/unicodetype_db.h

Modules/_sre.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,8 +2979,7 @@ static PyMethodDef _functions[] = {
29792979
{NULL, NULL}
29802980
};
29812981

2982-
DL_EXPORT(void)
2983-
init_sre(void)
2982+
PyMODINIT_FUNC init_sre(void)
29842983
{
29852984
PyObject* m;
29862985
PyObject* d;

Modules/pyexpat.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,10 +1583,9 @@ get_version_string(void)
15831583
#define MODULE_INITFUNC initpyexpat
15841584
#endif
15851585

1586-
void MODULE_INITFUNC(void); /* avoid compiler warnings */
1586+
PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
15871587

1588-
DL_EXPORT(void)
1589-
MODULE_INITFUNC(void)
1588+
PyMODINIT_FUNC MODULE_INITFUNC(void)
15901589
{
15911590
PyObject *m, *d;
15921591
PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");

PC/_winreg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ inskey(PyObject * d, char * name, HKEY key)
14231423

14241424
#define ADD_KEY(val) inskey(d, #val, val)
14251425

1426-
__declspec(dllexport) void init_winreg(void)
1426+
PyMODINIT_FUNC init_winreg(void)
14271427
{
14281428
PyObject *m, *d;
14291429
m = Py_InitModule3("_winreg", winreg_methods, module_doc);

0 commit comments

Comments
 (0)