@@ -71,6 +71,18 @@ char *macstrerror(int err)
7171 return buf ;
7272}
7373
74+ /* Exception object shared by all Mac specific modules for Mac OS errors */
75+ PyObject * PyMac_OSErrException ;
76+
77+ /* Initialize and return PyMac_OSErrException */
78+ PyObject *
79+ PyMac_GetOSErrException ()
80+ {
81+ if (PyMac_OSErrException == NULL )
82+ PyMac_OSErrException = PyString_FromString ("Mac OS Error" );
83+ return PyMac_OSErrException ;
84+ }
85+
7486/* Set a MAC-specific error from errno, and return NULL; return None if no error */
7587PyObject *
7688PyErr_Mac (PyObject * eobj , int err )
@@ -91,6 +103,13 @@ PyErr_Mac(PyObject *eobj, int err)
91103 return NULL ;
92104}
93105
106+ /* Call PyErr_Mac with PyMac_OSErrException */
107+ PyObject *
108+ PyMac_Error (OSErr err )
109+ {
110+ return PyErr_Mac (PyMac_GetOSErrException (), err );
111+ }
112+
94113/*
95114** Idle routine for busy-wait loops.
96115** This is rather tricky: if we see an event we check whether it is
@@ -117,9 +136,9 @@ PyMac_Idle()
117136}
118137
119138
120- /* Convert a ResType argument */
139+ /* Convert a 4-char string object argument to an OSType value */
121140int
122- PyMac_GetOSType (PyObject * v , ResType * pr )
141+ PyMac_GetOSType (PyObject * v , OSType * pr )
123142{
124143 if (!PyString_Check (v ) || PyString_Size (v ) != 4 ) {
125144 PyErr_SetString (PyExc_TypeError ,
@@ -130,7 +149,15 @@ PyMac_GetOSType(PyObject *v, ResType *pr)
130149 return 1 ;
131150}
132151
133- /* Convert a Python string to a Str255 */
152+ /* Convert an OSType value to a 4-char string object */
153+ PyObject *
154+ PyMac_BuildOSType (OSType t )
155+ {
156+ return PyString_FromStringAndSize ((char * )& t , 4 );
157+ }
158+
159+
160+ /* Convert a Python string object to a Str255 */
134161int
135162PyMac_GetStr255 (PyObject * v , Str255 pbuf )
136163{
@@ -145,8 +172,18 @@ PyMac_GetStr255(PyObject *v, Str255 pbuf)
145172 return 1 ;
146173}
147174
175+ /* Convert a Str255 to a Python string object */
176+ PyObject *
177+ PyMac_BuildStr255 (Str255 s )
178+ {
179+ return PyString_FromStringAndSize ((char * )& s [1 ], (int )s [0 ]);
180+ }
181+
182+
148183/*
149- ** Convert anything resembling an FSSpec argument
184+ ** Convert a Python object to an FSSpec.
185+ ** The object may either be a full pathname or a triple
186+ ** (vrefnum, dirid, path).
150187** NOTE: This routine will fail on pre-sys7 machines.
151188** The caller is responsible for not calling this routine
152189** in those cases (which is fine, since everyone calling
@@ -180,23 +217,71 @@ PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
180217 return 1 ;
181218}
182219
183- /* Return a Python object that describes an FSSpec */
220+ /* Convert an FSSpec to a Python object -- a triple (vrefnum, dirid, path) */
184221PyObject *
185222PyMac_BuildFSSpec (FSSpec * fs )
186223{
187224 return Py_BuildValue ("(iis#)" , fs -> vRefNum , fs -> parID , & fs -> name [1 ], fs -> name [0 ]);
188225}
189226
190- /* Convert an OSType value to a 4-char string object */
227+
228+ /* Convert a Python object to a Rect.
229+ The object must be a (top, left, bottom, right) tuple.
230+ (Unfortunately this is different from STDWIN's convention). */
231+ int
232+ PyMac_GetRect (PyObject * v , Rect * r )
233+ {
234+ return PyArg_Parse (v , "(hhhh)" , & r -> top , & r -> left , & r -> bottom , & r -> right );
235+ }
236+
237+ /* Convert a Rect to a Python object */
191238PyObject *
192- PyMac_BuildOSType ( OSType t )
239+ PyMac_BuildRect ( Rect * r )
193240{
194- return PyString_FromStringAndSize ((char * )& t , 4 );
241+ return Py_BuildValue ("(hhhh)" , r -> top , r -> left , r -> bottom , r -> right );
242+ }
243+
244+
245+ /* Convert a Python object to a Point.
246+ The object must be a (v, h) tuple.
247+ (Unfortunately this is different from STDWIN's convention). */
248+ int
249+ PyMac_GetPoint (PyObject * v , Point * p )
250+ {
251+ return PyArg_Parse (v , "(hh)" , & p -> v , & p -> h );
195252}
196253
197- /* Convert a Str255 to a Python string */
254+ /* Convert a Point to a Python object */
198255PyObject *
199- PyMac_BuildStr255 ( Str255 s )
256+ PyMac_BuildPoint ( Point p )
200257{
201- return PyString_FromStringAndSize ((char * )& s [1 ], (int )s [0 ]);
258+ return Py_BuildValue ("(hh)" , p .v , p .h );
259+ }
260+
261+
262+ /* Convert a Python object to an EventRecord.
263+ The object must be a (what, message, when, (v, h), modifiers) tuple. */
264+ int
265+ PyMac_GetEventRecord (PyObject * v , EventRecord * e )
266+ {
267+ return PyArg_Parse (v , "(hll(hh)h)" ,
268+ & e -> what ,
269+ & e -> message ,
270+ & e -> when ,
271+ & e -> where .v ,
272+ & e -> where .h ,
273+ & e -> modifiers );
274+ }
275+
276+ /* Convert a Rect to an EventRecord object */
277+ PyObject *
278+ PyMac_BuildEventRecord (EventRecord * e )
279+ {
280+ return Py_BuildValue ("(hll(hh)h)" ,
281+ e -> what ,
282+ e -> message ,
283+ e -> when ,
284+ e -> where .v ,
285+ e -> where .h ,
286+ e -> modifiers );
202287}
0 commit comments