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

Skip to content

Commit f57a4a2

Browse files
committed
Glue code to connect obj_New and obj_Convert routines (the PyArg_Parse and Py_BuildTuple helpers) from one dynamically imported module to another.
1 parent 0e04eec commit f57a4a2

2 files changed

Lines changed: 131 additions & 6 deletions

File tree

Mac/Include/pymactoolbox.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@
1515
#include <Movies.h>
1616
#include <Errors.h>
1717

18+
#ifdef USE_TOOLBOX_OBJECT_GLUE
19+
/*
20+
** These macros are used in the module init code. If we use toolbox object glue
21+
** it sets the function pointer to point to the real function.
22+
*/
23+
#define PyMac_INIT_TOOLBOX_OBJECT_NEW(rtn) { \
24+
extern PyObject *(*PyMacGluePtr_##rtn)(object); \
25+
PyMacGluePtr_##rtn = _##rtn; \
26+
}
27+
#define PyMac_INIT_TOOLBOX_OBJECT_CONVERT(rtn) { \
28+
extern int (*PyMacGluePtr_##rtn)(object); \
29+
PyMacGluePtr_##rtn = _##rtn; \
30+
}
31+
#else
32+
/*
33+
** If we don't use toolbox object glue the init macros are empty. Moreover, we define
34+
** _xxx_New to be the same as xxx_New, and the code in mactoolboxglue isn't included.
35+
*/
36+
#define PyMac_INIT_TOOLBOX_OBJECT_NEW(rtn)
37+
#define PyMac_INIT_TOOLBOX_OBJECT_CONVERT(rtn)
38+
#endif /* USE_TOOLBOX_OBJECT_GLUE */
39+
1840
/* AE exports */
1941
extern PyObject *AEDesc_New(AppleEvent *); /* XXXX Why passed by address?? */
2042
extern int AEDesc_Convert(PyObject *, AppleEvent *);
@@ -32,10 +54,7 @@ extern int CtlObj_Convert(PyObject *, ControlHandle *);
3254
/* Dlg exports */
3355
extern PyObject *DlgObj_New(DialogPtr);
3456
extern int DlgObj_Convert(PyObject *, DialogPtr *);
35-
extern WindowPtr DlgObj_ConvertToWindow(PyObject *);
3657
extern PyObject *DlgObj_WhichDialog(DialogPtr);
37-
extern PyTypeObject Dialog_Type;
38-
#define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type)
3958

4059
/* Drag exports */
4160
extern PyObject *DragObj_New(DragReference);
@@ -67,7 +86,7 @@ extern int TrackObj_Convert(PyObject *, Track *);
6786
extern PyObject *MovieObj_New(Movie);
6887
extern int MovieObj_Convert(PyObject *, Movie *);
6988
extern PyObject *MovieCtlObj_New(MovieController);
70-
extern int MovieCtlObj_Convert(PyObject *, TimeBase *);
89+
extern int MovieCtlObj_Convert(PyObject *, MovieController *);
7190
extern PyObject *TimeBaseObj_New(TimeBase);
7291
extern int TimeBaseObj_Convert(PyObject *, TimeBase *);
7392
extern PyObject *UserDataObj_New(UserData);
@@ -89,8 +108,6 @@ extern int TEObj_Convert(PyObject *, TEHandle *);
89108
extern PyObject *WinObj_New(WindowPtr);
90109
extern int WinObj_Convert(PyObject *, WindowPtr *);
91110
extern PyObject *WinObj_WhichWindow(WindowPtr);
92-
extern PyTypeObject Window_Type;
93-
#define WinObj_Check(x) ((x)->ob_type == &Window_Type)
94111

95112

96113
#ifdef __cplusplus

Mac/Python/mactoolboxglue.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
** mactoolboxglue.c - Glue together the toolbox objects.
3+
**
4+
** Because toolbox modules interdepend on each other, they use each others
5+
** object types, on MacOSX/MachO this leads to the situation that they
6+
** cannot be dynamically loaded (or they would all have to be lumped into
7+
** a single .so, but this would be bad for extensibility).
8+
**
9+
** This file defines wrappers for all the _New and _Convert functions,
10+
** which are the Py_BuildValue and PyArg_ParseTuple helpers. The wrappers
11+
** check an indirection function pointer, and if it isn't filled in yet
12+
** they import the appropriate module, whose init routine should fill in
13+
** the pointer.
14+
*/
15+
16+
#ifdef USE_TOOLBOX_OBJECT_GLUE
17+
18+
#include "python.h"
19+
#include "pymactoolbox.h"
20+
21+
#define GLUE_NEW(object, routinename, module) \
22+
PyObject *(*PyMacGluePtr_##routinename)(object); \
23+
\
24+
PyObject *routinename(object cobj) { \
25+
if (!PyMacGluePtr_##routinename) { \
26+
if (!PyImport_ImportModule(module)) return NULL; \
27+
if (!PyMacGluePtr_##routinename) { \
28+
PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \
29+
return NULL; \
30+
} \
31+
} \
32+
return (*PyMacGluePtr_##routinename)(cobj); \
33+
}
34+
35+
#define GLUE_CONVERT(object, routinename, module) \
36+
int (*PyMacGluePtr_##routinename)(PyObject *, object *); \
37+
\
38+
int routinename(PyObject *pyobj, object *cobj) { \
39+
if (!PyMacGluePtr_##routinename) { \
40+
if (!PyImport_ImportModule(module)) return NULL; \
41+
if (!PyMacGluePtr_##routinename) { \
42+
PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \
43+
return NULL; \
44+
} \
45+
} \
46+
return (*PyMacGluePtr_##routinename)(pyobj, cobj); \
47+
}
48+
49+
GLUE_NEW(AppleEvent *, AEDesc_New, "AE") /* XXXX Why by address? */
50+
GLUE_CONVERT(AppleEvent, AEDesc_Convert, "AE")
51+
52+
GLUE_NEW(Component, CmpObj_New, "Cm")
53+
GLUE_CONVERT(Component, CmpObj_Convert, "Cm")
54+
GLUE_NEW(ComponentInstance, CmpInstObj_New, "Cm")
55+
GLUE_CONVERT(ComponentInstance, CmpInstObj_Convert, "Cm")
56+
57+
GLUE_NEW(ControlHandle, CtlObj_New, "Ctl")
58+
GLUE_CONVERT(ControlHandle, CtlObj_Convert, "Ctl")
59+
60+
GLUE_NEW(DialogPtr, DlgObj_New, "Dlg")
61+
GLUE_CONVERT(DialogPtr, DlgObj_Convert, "Dlg")
62+
GLUE_NEW(DialogPtr, DlgObj_WhichDialog, "Dlg")
63+
64+
GLUE_NEW(DragReference, DragObj_New, "Drag")
65+
GLUE_CONVERT(DragReference, DragObj_Convert, "Drag")
66+
67+
GLUE_NEW(ListHandle, ListObj_New, "List")
68+
GLUE_CONVERT(ListHandle, ListObj_Convert, "List")
69+
70+
GLUE_NEW(MenuHandle, MenuObj_New, "Menu")
71+
GLUE_CONVERT(MenuHandle, MenuObj_Convert, "Menu")
72+
73+
GLUE_NEW(GrafPtr, GrafObj_New, "Qd")
74+
GLUE_CONVERT(GrafPtr, GrafObj_Convert, "Qd")
75+
GLUE_NEW(BitMapPtr, BMObj_New, "Qd")
76+
GLUE_CONVERT(BitMapPtr, BMObj_Convert, "Qd")
77+
GLUE_NEW(RGBColor *, QdRGB_New, "Qd") /* XXXX Why? */
78+
GLUE_CONVERT(RGBColor, QdRGB_Convert, "Qd")
79+
80+
GLUE_NEW(GWorldPtr, GWorldObj_New, "Qdoffs")
81+
GLUE_CONVERT(GWorldPtr, GWorldObj_Convert, "Qdoffs")
82+
83+
GLUE_NEW(Track, TrackObj_New, "Qt")
84+
GLUE_CONVERT(Track, TrackObj_Convert, "Qt")
85+
GLUE_NEW(Movie, MovieObj_New, "Qt")
86+
GLUE_CONVERT(Movie, MovieObj_Convert, "Qt")
87+
GLUE_NEW(MovieController, MovieCtlObj_New, "Qt")
88+
GLUE_CONVERT(MovieController, MovieCtlObj_Convert, "Qt")
89+
GLUE_NEW(TimeBase, TimeBaseObj_New, "Qt")
90+
GLUE_CONVERT(TimeBase, TimeBaseObj_Convert, "Qt")
91+
GLUE_NEW(UserData, UserDataObj_New, "Qt")
92+
GLUE_CONVERT(UserData, UserDataObj_Convert, "Qt")
93+
GLUE_NEW(Media, MediaObj_New, "Qt")
94+
GLUE_CONVERT(Media, MediaObj_Convert, "Qt")
95+
96+
GLUE_NEW(Handle, ResObj_New, "Res")
97+
GLUE_CONVERT(Handle, ResObj_Convert, "Res")
98+
GLUE_NEW(Handle, OptResObj_New, "Res")
99+
GLUE_CONVERT(Handle, OptResObj_Convert, "Res")
100+
101+
GLUE_NEW(TEHandle, TEObj_New, "TE")
102+
GLUE_CONVERT(TEHandle, TEObj_Convert, "TE")
103+
104+
GLUE_NEW(WindowPtr, WinObj_New, "Win")
105+
GLUE_CONVERT(WindowPtr, WinObj_Convert, "Win")
106+
GLUE_NEW(WindowPtr, WinObj_WhichWindow, "Win")
107+
108+
#endif /* USE_TOOLBOX_OBJECT_GLUE */

0 commit comments

Comments
 (0)