Changeset 388 for python/vendor/current/Python/mactoolboxglue.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Python/mactoolboxglue.c
r2 r388 5 5 All Rights Reserved 6 6 7 Permission to use, copy, modify, and distribute this software and its 8 documentation for any purpose and without fee is hereby granted, 7 Permission to use, copy, modify, and distribute this software and its 8 documentation for any purpose and without fee is hereby granted, 9 9 provided that the above copyright notice appear in all copies and that 10 both that copyright notice and this permission notice appear in 10 both that copyright notice and this permission notice appear in 11 11 supporting documentation, and that the names of Stichting Mathematisch 12 12 Centrum or CWI not be used in advertising or publicity pertaining to … … 26 26 #include "Python.h" 27 27 #include "pymactoolbox.h" 28 #include <arpa/inet.h> 28 #include <arpa/inet.h> /* for ntohl, htonl */ 29 29 30 30 … … 33 33 PyMac_StrError(int err) 34 34 { 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 35 static char buf[256]; 36 PyObject *m; 37 PyObject *rv; 38 39 m = PyImport_ImportModuleNoBlock("MacOS"); 40 if (!m) { 41 if (Py_VerboseFlag) 42 PyErr_Print(); 43 PyErr_Clear(); 44 rv = NULL; 45 } 46 else { 47 rv = PyObject_CallMethod(m, "GetErrorString", "i", err); 48 if (!rv) 49 PyErr_Clear(); 50 } 51 if (!rv) { 52 buf[0] = '\0'; 53 } 54 else { 55 char *input = PyString_AsString(rv); 56 if (!input) { 57 PyErr_Clear(); 58 buf[0] = '\0'; 59 } else { 60 strncpy(buf, input, sizeof(buf) - 1); 61 buf[sizeof(buf) - 1] = '\0'; 62 } 63 Py_DECREF(rv); 64 } 65 Py_XDECREF(m); 66 return buf; 67 67 } 68 68 … … 74 74 PyMac_GetOSErrException(void) 75 75 { 76 77 78 76 if (PyMac_OSErrException == NULL) 77 PyMac_OSErrException = PyErr_NewException("MacOS.Error", NULL, NULL); 78 return PyMac_OSErrException; 79 79 } 80 80 81 81 /* Set a MAC-specific error from errno, and return NULL; return None if no error */ 82 PyObject * 82 PyObject * 83 83 PyErr_Mac(PyObject *eobj, int err) 84 84 { 85 86 87 88 89 90 91 92 93 94 95 96 97 98 85 char *msg; 86 PyObject *v; 87 88 if (err == 0 && !PyErr_Occurred()) { 89 Py_INCREF(Py_None); 90 return Py_None; 91 } 92 if (err == -1 && PyErr_Occurred()) 93 return NULL; 94 msg = PyMac_StrError(err); 95 v = Py_BuildValue("(is)", err, msg); 96 PyErr_SetObject(eobj, v); 97 Py_DECREF(v); 98 return NULL; 99 99 } 100 100 … … 103 103 PyMac_Error(OSErr err) 104 104 { 105 105 return PyErr_Mac(PyMac_GetOSErrException(), err); 106 106 } 107 107 … … 111 111 PyMac_GetFullPathname(FSSpec *fss, char *path, int len) 112 112 { 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 113 PyObject *fs, *exc; 114 PyObject *rv = NULL; 115 char *input; 116 OSErr err = noErr; 117 118 *path = '\0'; 119 120 fs = PyMac_BuildFSSpec(fss); 121 if (!fs) 122 goto error; 123 124 rv = PyObject_CallMethod(fs, "as_pathname", ""); 125 if (!rv) 126 goto error; 127 128 input = PyString_AsString(rv); 129 if (!input) 130 goto error; 131 132 strncpy(path, input, len - 1); 133 path[len - 1] = '\0'; 134 135 Py_XDECREF(rv); 136 Py_XDECREF(fs); 137 return err; 138 138 139 139 error: 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 140 exc = PyErr_Occurred(); 141 if (exc && PyErr_GivenExceptionMatches(exc, 142 PyMac_GetOSErrException())) { 143 PyObject *args = PyObject_GetAttrString(exc, "args"); 144 if (args) { 145 char *ignore; 146 PyArg_ParseTuple(args, "is", &err, &ignore); 147 Py_XDECREF(args); 148 } 149 } 150 if (err == noErr) 151 err = -1; 152 PyErr_Clear(); 153 Py_XDECREF(rv); 154 Py_XDECREF(fs); 155 return err; 156 156 } 157 157 #endif /* !__LP64__ */ … … 161 161 PyMac_GetOSType(PyObject *v, OSType *pr) 162 162 { 163 164 165 166 167 168 169 170 171 163 uint32_t tmp; 164 if (!PyString_Check(v) || PyString_Size(v) != 4) { 165 PyErr_SetString(PyExc_TypeError, 166 "OSType arg must be string of 4 chars"); 167 return 0; 168 } 169 memcpy((char *)&tmp, PyString_AsString(v), 4); 170 *pr = (OSType)ntohl(tmp); 171 return 1; 172 172 } 173 173 … … 176 176 PyMac_BuildOSType(OSType t) 177 177 { 178 179 178 uint32_t tmp = htonl((uint32_t)t); 179 return PyString_FromStringAndSize((char *)&tmp, 4); 180 180 } 181 181 … … 184 184 PyMac_BuildNumVersion(NumVersion t) 185 185 { 186 186 return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); 187 187 } 188 188 … … 192 192 PyMac_GetStr255(PyObject *v, Str255 pbuf) 193 193 { 194 195 196 197 198 199 200 201 202 194 int len; 195 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { 196 PyErr_SetString(PyExc_TypeError, 197 "Str255 arg must be string of at most 255 chars"); 198 return 0; 199 } 200 pbuf[0] = len; 201 memcpy((char *)(pbuf+1), PyString_AsString(v), len); 202 return 1; 203 203 } 204 204 … … 207 207 PyMac_BuildStr255(Str255 s) 208 208 { 209 210 211 212 213 209 if ( s == NULL ) { 210 PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL"); 211 return NULL; 212 } 213 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); 214 214 } 215 215 … … 217 217 PyMac_BuildOptStr255(Str255 s) 218 218 { 219 220 221 222 223 219 if ( s == NULL ) { 220 Py_INCREF(Py_None); 221 return Py_None; 222 } 223 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); 224 224 } 225 225 … … 233 233 PyMac_GetRect(PyObject *v, Rect *r) 234 234 { 235 235 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom); 236 236 } 237 237 … … 240 240 PyMac_BuildRect(Rect *r) 241 241 { 242 242 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom); 243 243 } 244 244 … … 251 251 PyMac_GetPoint(PyObject *v, Point *p) 252 252 { 253 253 return PyArg_Parse(v, "(hh)", &p->h, &p->v); 254 254 } 255 255 … … 258 258 PyMac_BuildPoint(Point p) 259 259 { 260 260 return Py_BuildValue("(hh)", p.h, p.v); 261 261 } 262 262 … … 267 267 PyMac_GetEventRecord(PyObject *v, EventRecord *e) 268 268 { 269 270 271 272 273 274 &e->where.v, 275 269 return PyArg_Parse(v, "(Hkk(hh)H)", 270 &e->what, 271 &e->message, 272 &e->when, 273 &e->where.h, 274 &e->where.v, 275 &e->modifiers); 276 276 } 277 277 … … 280 280 PyMac_BuildEventRecord(EventRecord *e) 281 281 { 282 283 284 285 286 287 288 282 return Py_BuildValue("(hll(hh)h)", 283 e->what, 284 e->message, 285 e->when, 286 e->where.h, 287 e->where.v, 288 e->modifiers); 289 289 } 290 290 … … 293 293 PyMac_GetFixed(PyObject *v, Fixed *f) 294 294 { 295 296 297 298 299 300 295 double d; 296 297 if( !PyArg_Parse(v, "d", &d)) 298 return 0; 299 *f = (Fixed)(d * 0x10000); 300 return 1; 301 301 } 302 302 … … 305 305 PyMac_BuildFixed(Fixed f) 306 306 { 307 308 309 310 311 307 double d; 308 309 d = f; 310 d = d / 0x10000; 311 return Py_BuildValue("d", d); 312 312 } 313 313 … … 316 316 PyMac_Getwide(PyObject *v, wide *rv) 317 317 { 318 319 320 321 322 323 324 325 318 if (PyInt_Check(v)) { 319 rv->hi = 0; 320 rv->lo = PyInt_AsLong(v); 321 if( rv->lo & 0x80000000 ) 322 rv->hi = -1; 323 return 1; 324 } 325 return PyArg_Parse(v, "(kk)", &rv->hi, &rv->lo); 326 326 } 327 327 … … 330 330 PyMac_Buildwide(wide *w) 331 331 { 332 333 334 335 332 if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || 333 (w->hi == -1 && (w->lo & 0x80000000) ) ) 334 return PyInt_FromLong(w->lo); 335 return Py_BuildValue("(ll)", w->hi, w->lo); 336 336 } 337 337 … … 359 359 if (!PyImport_ImportModule(module)) return NULL; \ 360 360 if (!PyMacGluePtr_##routinename) { \ 361 362 361 PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \ 362 return NULL; \ 363 363 } \ 364 364 } \ … … 373 373 if (!PyImport_ImportModule(module)) return 0; \ 374 374 if (!PyMacGluePtr_##routinename) { \ 375 376 375 PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \ 376 return 0; \ 377 377 } \ 378 378 } \
Note:
See TracChangeset
for help on using the changeset viewer.