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

Skip to content

Commit eefac35

Browse files
committed
Added WeakLink...Generator classes (should have done that ages ago). These check the c-function pointer for being NULL before calling it and raise UnimplementedError if it is.
This allows system libs to be weak-linked, thereby allowing us to generate functions that are only available on some OS versions without getting a NULL dereference if the function isn't available.
1 parent 340d98f commit eefac35

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Tools/bgen/bgen/bgenGenerator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def docstring(self):
164164

165165
def functionbody(self):
166166
self.declarations()
167+
self.precheck()
167168
self.getargs()
168169
self.callit()
169170
self.checkit()
@@ -194,6 +195,9 @@ def getargs(self):
194195
continue
195196
if arg.mode in (InMode, InOutMode):
196197
arg.getargsCheck()
198+
199+
def precheck(self):
200+
pass
197201

198202
def callit(self):
199203
args = ""

Tools/bgen/bgen/macsupport.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ def errorCheck(self, name):
118118
includestuff = """
119119
#include "macglue.h"
120120
#include "pymactoolbox.h"
121+
122+
/* Macro to test whether a weak-loaded CFM function exists */
123+
#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\\
124+
PyErr_SetString(PyExc_NotImplementedError, \\
125+
"Not available in this shared library/OS version"); \\
126+
return NULL; \\
127+
}} while(0)
128+
121129
"""
122130

123131
# Stuff added just before the module's init function
@@ -145,6 +153,16 @@ def makereturnvar(self):
145153
class OSErrFunctionGenerator(OSErrMixIn, FunctionGenerator): pass
146154
class OSErrMethodGenerator(OSErrMixIn, MethodGenerator): pass
147155

156+
class WeakLinkMixIn:
157+
"Mix-in to test the function actually exists (!= NULL) before calling"
158+
159+
def precheck(self):
160+
Output('PyMac_PRECHECK(%s);', self.name)
161+
162+
class WeakLinkFunctionGenerator(WeakLinkMixIn, FunctionGenerator): pass
163+
class WeakLinkMethodGenerator(WeakLinkMixIn, MethodGenerator): pass
164+
class OSErrWeakLinkFunctionGenerator(OSErrMixIn, WeakLinkMixIn, FunctionGenerator): pass
165+
class OSErrWeakLinkMethodGenerator(OSErrMixIn, WeakLinkMixIn, MethodGenerator): pass
148166

149167
class MacModule(Module):
150168
"Subclass which gets the exception initializer from macglue.c"

0 commit comments

Comments
 (0)