Jack Jansen | b3928d2 | 1997-02-17 16:56:56 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | #include "Python.h" |
| 33 | #include "macglue.h" |
| 34 | #include "macdefs.h" |
| 35 | |
| 36 | extern PyObject *ResObj_New Py_PROTO((Handle)); |
| 37 | extern int ResObj_Convert Py_PROTO((PyObject *, Handle *)); |
| 38 | |
| 39 | #include <CodeFragments.h> |
| 40 | |
| 41 | static PyObject *ErrorObject; |
| 42 | |
| 43 | #define PARANOID(arg) \ |
| 44 | if ( arg == 0 ) {PyErr_SetString(ErrorObject, "Internal error: NULL arg!"); return 0; } |
| 45 | |
| 46 | /* Prototype we use for routines */ |
| 47 | |
| 48 | typedef long anything; |
| 49 | typedef anything (*anyroutine) Py_PROTO((...)); |
| 50 | |
| 51 | #define MAXNAME 31 /* Maximum size of names, for printing only */ |
| 52 | #define MAXARG 8 /* Maximum number of arguments */ |
| 53 | |
| 54 | /* |
| 55 | ** Routines to convert arguments between Python and C |
| 56 | */ |
| 57 | typedef anything (*py2c_converter) Py_PROTO((PyObject *)); |
| 58 | typedef PyObject *(*c2py_converter) Py_PROTO((anything)); |
| 59 | |
| 60 | /* Dummy routine for arguments that are output-only */ |
| 61 | static anything |
| 62 | py2c_dummy(arg) |
| 63 | PyObject *arg; |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /* Routine to allocate storage for output integers */ |
| 69 | static anything |
| 70 | py2c_alloc(arg) |
| 71 | PyObject *arg; |
| 72 | { |
| 73 | char *ptr; |
| 74 | |
| 75 | if( (ptr=malloc(sizeof(anything))) == 0 ) |
| 76 | PyErr_NoMemory(); |
| 77 | return (anything)ptr; |
| 78 | } |
| 79 | |
| 80 | /* Dummy routine for arguments that are input-only */ |
| 81 | static PyObject * |
| 82 | c2py_dummy(arg) |
| 83 | anything arg; |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* Routine to de-allocate storage for input-only arguments */ |
| 89 | static PyObject * |
| 90 | c2py_free(arg) |
| 91 | anything arg; |
| 92 | { |
| 93 | if ( arg ) |
| 94 | free((char *)arg); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | ** None |
| 100 | */ |
| 101 | static PyObject * |
| 102 | c2py_none(arg) |
| 103 | anything arg; |
| 104 | { |
| 105 | if ( arg ) |
| 106 | free((char *)arg); |
| 107 | Py_INCREF(Py_None); |
| 108 | return Py_None; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | ** OSErr |
| 113 | */ |
| 114 | static PyObject * |
| 115 | c2py_oserr(arg) |
| 116 | anything arg; |
| 117 | { |
| 118 | OSErr *ptr = (OSErr *)arg; |
| 119 | |
| 120 | PARANOID(arg); |
| 121 | if (*ptr) { |
| 122 | PyErr_Mac(PyMac_OSErrException, *ptr); |
| 123 | free(ptr); |
| 124 | return NULL; |
| 125 | } |
| 126 | Py_INCREF(Py_None); |
| 127 | return Py_None; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** integers of all sizes (PPC only) |
| 132 | */ |
| 133 | static anything |
| 134 | py2c_in_int(arg) |
| 135 | PyObject *arg; |
| 136 | { |
| 137 | return PyInt_AsLong(arg); |
| 138 | } |
| 139 | |
| 140 | static PyObject * |
| 141 | c2py_out_long(arg) |
| 142 | anything arg; |
| 143 | { |
| 144 | PyObject *rv; |
| 145 | |
| 146 | PARANOID(arg); |
| 147 | rv = PyInt_FromLong(*(long *)arg); |
| 148 | free((char *)arg); |
| 149 | return rv; |
| 150 | } |
| 151 | |
| 152 | static PyObject * |
| 153 | c2py_out_short(arg) |
| 154 | anything arg; |
| 155 | { |
| 156 | PyObject *rv; |
| 157 | |
| 158 | PARANOID(arg); |
| 159 | rv = PyInt_FromLong((long)*(short *)arg); |
| 160 | free((char *)arg); |
| 161 | return rv; |
| 162 | } |
| 163 | |
| 164 | static PyObject * |
| 165 | c2py_out_byte(arg) |
| 166 | anything arg; |
| 167 | { |
| 168 | PyObject *rv; |
| 169 | |
| 170 | PARANOID(arg); |
| 171 | rv = PyInt_FromLong((long)*(char *)arg); |
| 172 | free((char *)arg); |
| 173 | return rv; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | ** Strings |
| 178 | */ |
| 179 | static anything |
| 180 | py2c_in_string(arg) |
| 181 | PyObject *arg; |
| 182 | { |
| 183 | return (anything)PyString_AsString(arg); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | ** Pascal-style strings |
| 188 | */ |
| 189 | static anything |
| 190 | py2c_in_pstring(arg) |
| 191 | PyObject *arg; |
| 192 | { |
| 193 | unsigned char *p; |
| 194 | int size; |
| 195 | |
| 196 | if( (size = PyString_Size(arg)) < 0) |
| 197 | return 0; |
| 198 | if ( size > 255 ) { |
| 199 | PyErr_SetString(ErrorObject, "Pstring must be <= 255 chars"); |
| 200 | return 0; |
| 201 | } |
| 202 | if( (p=(unsigned char *)malloc(256)) == 0 ) { |
| 203 | PyErr_NoMemory(); |
| 204 | return 0; |
| 205 | } |
| 206 | p[0] = size; |
| 207 | memcpy(p+1, PyString_AsString(arg), size); |
| 208 | return (anything)p; |
| 209 | } |
| 210 | |
| 211 | static anything |
| 212 | py2c_out_pstring(arg) |
| 213 | PyObject *arg; |
| 214 | { |
| 215 | unsigned char *p; |
| 216 | |
| 217 | if( (p=(unsigned char *)malloc(256)) == 0 ) { |
| 218 | PyErr_NoMemory(); |
| 219 | return 0; |
| 220 | } |
| 221 | p[0] = 0; |
| 222 | return (anything)p; |
| 223 | } |
| 224 | |
| 225 | static PyObject * |
| 226 | c2py_out_pstring(arg) |
| 227 | anything arg; |
| 228 | { |
| 229 | unsigned char *p = (unsigned char *)arg; |
| 230 | PyObject *rv; |
| 231 | |
| 232 | PARANOID(arg); |
| 233 | rv = PyString_FromStringAndSize((char *)p+1, p[0]); |
| 234 | free(p); |
| 235 | return rv; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | ** C objects. |
| 240 | */ |
| 241 | static anything |
| 242 | py2c_in_cobject(arg) |
| 243 | PyObject *arg; |
| 244 | { |
| 245 | if ( arg == Py_None ) |
| 246 | return 0; |
| 247 | return (anything)PyCObject_AsVoidPtr(arg); |
| 248 | } |
| 249 | |
| 250 | static PyObject * |
| 251 | c2py_out_cobject(arg) |
| 252 | anything arg; |
| 253 | { |
| 254 | void **ptr = (void **)arg; |
| 255 | PyObject *rv; |
| 256 | |
| 257 | PARANOID(arg); |
| 258 | if ( *ptr == 0 ) { |
| 259 | Py_INCREF(Py_None); |
| 260 | rv = Py_None; |
| 261 | } else { |
| 262 | rv = PyCObject_FromVoidPtr(*ptr, 0); |
| 263 | } |
| 264 | free((char *)ptr); |
| 265 | return rv; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | ** Handles. |
| 270 | */ |
| 271 | static anything |
| 272 | py2c_in_handle(arg) |
| 273 | PyObject *arg; |
| 274 | { |
| 275 | Handle h = 0; |
| 276 | ResObj_Convert(arg, &h); |
| 277 | return (anything)h; |
| 278 | } |
| 279 | |
| 280 | static PyObject * |
| 281 | c2py_out_handle(arg) |
| 282 | anything arg; |
| 283 | { |
| 284 | Handle *rv = (Handle *)arg; |
| 285 | PyObject *prv; |
| 286 | |
| 287 | PARANOID(arg); |
| 288 | if ( *rv == 0 ) { |
| 289 | Py_INCREF(Py_None); |
| 290 | prv = Py_None; |
| 291 | } else { |
| 292 | prv = ResObj_New(*rv); |
| 293 | } |
| 294 | free((char *)rv); |
| 295 | return prv; |
| 296 | } |
| 297 | |
| 298 | typedef struct { |
| 299 | char *name; /* Name */ |
| 300 | py2c_converter get; /* Get argument */ |
| 301 | int get_uses_arg; /* True if the above consumes an argument */ |
| 302 | c2py_converter put; /* Put result value */ |
| 303 | int put_gives_result; /* True if above produces a result */ |
| 304 | } conventry; |
| 305 | |
| 306 | static conventry converters[] = { |
| 307 | {"OutNone", py2c_alloc, 0, c2py_none, 1}, |
| 308 | {"OutOSErr", py2c_alloc, 0, c2py_oserr, 1}, |
| 309 | #define OSERRORCONVERTER (&converters[1]) |
| 310 | {"InInt", py2c_in_int, 1, c2py_dummy, 0}, |
| 311 | {"OutLong", py2c_alloc, 0, c2py_out_long, 1}, |
| 312 | {"OutShort", py2c_alloc, 0, c2py_out_short, 1}, |
| 313 | {"OutByte", py2c_alloc, 0, c2py_out_byte, 1}, |
| 314 | {"InString", py2c_in_string, 1, c2py_dummy, 0}, |
| 315 | {"InPstring", py2c_in_pstring,1, c2py_free, 0}, |
| 316 | {"OutPstring", py2c_out_pstring,0, c2py_out_pstring,1}, |
| 317 | {"InCobject", py2c_in_cobject,1, c2py_dummy, 0}, |
| 318 | {"OutCobject", py2c_alloc, 0, c2py_out_cobject,0}, |
| 319 | {"InHandle", py2c_in_handle, 1, c2py_dummy, 0}, |
| 320 | {"OutHandle", py2c_alloc, 0, c2py_out_handle,1}, |
| 321 | {0, 0, 0, 0, 0} |
| 322 | }; |
| 323 | |
| 324 | static conventry * |
| 325 | getconverter(name) |
| 326 | char *name; |
| 327 | { |
| 328 | int i; |
| 329 | char buf[256]; |
| 330 | |
| 331 | for(i=0; converters[i].name; i++ ) |
| 332 | if ( strcmp(name, converters[i].name) == 0 ) |
| 333 | return &converters[i]; |
| 334 | sprintf(buf, "Unknown argtype: %s", name); |
| 335 | PyErr_SetString(ErrorObject, buf); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int |
| 340 | argparse_conv(obj, ptr) |
| 341 | PyObject *obj; |
| 342 | conventry **ptr; |
| 343 | { |
| 344 | char *name; |
| 345 | int i; |
| 346 | conventry *item; |
| 347 | |
| 348 | if( (name=PyString_AsString(obj)) == NULL ) |
| 349 | return 0; |
| 350 | if( (item=getconverter(name)) == NULL ) |
| 351 | return 0; |
| 352 | *ptr = item; |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | /* ----------------------------------------------------- */ |
| 357 | |
| 358 | /* Declarations for objects of type fragment */ |
| 359 | |
| 360 | typedef struct { |
| 361 | PyObject_HEAD |
| 362 | CFragConnectionID conn_id; |
| 363 | char name[MAXNAME+1]; |
| 364 | } cdfobject; |
| 365 | |
| 366 | staticforward PyTypeObject Cdftype; |
| 367 | |
| 368 | |
| 369 | |
| 370 | /* ---------------------------------------------------------------- */ |
| 371 | |
| 372 | /* Declarations for objects of type routine */ |
| 373 | |
| 374 | typedef struct { |
| 375 | PyObject_HEAD |
| 376 | anyroutine rtn; |
| 377 | char name[MAXNAME+1]; |
| 378 | } cdrobject; |
| 379 | |
| 380 | staticforward PyTypeObject Cdrtype; |
| 381 | |
| 382 | |
| 383 | |
| 384 | /* ---------------------------------------------------------------- */ |
| 385 | |
| 386 | /* Declarations for objects of type callable */ |
| 387 | |
| 388 | typedef struct { |
| 389 | PyObject_HEAD |
| 390 | cdrobject *routine; /* The routine to call */ |
| 391 | int npargs; /* Python argument count */ |
| 392 | int npreturn; /* Python return value count */ |
| 393 | int ncargs; /* C argument count + 1 */ |
| 394 | conventry *argconv[MAXARG+1]; /* Value converter list */ |
| 395 | } cdcobject; |
| 396 | |
| 397 | staticforward PyTypeObject Cdctype; |
| 398 | |
| 399 | |
| 400 | |
| 401 | /* -------------------------------------------------------- */ |
| 402 | |
| 403 | |
| 404 | static struct PyMethodDef cdr_methods[] = { |
| 405 | |
| 406 | {NULL, NULL} /* sentinel */ |
| 407 | }; |
| 408 | |
| 409 | /* ---------- */ |
| 410 | |
| 411 | |
| 412 | static cdrobject * |
| 413 | newcdrobject(name, routine) |
| 414 | unsigned char *name; |
| 415 | anyroutine routine; |
| 416 | { |
| 417 | cdrobject *self; |
| 418 | int nlen; |
| 419 | |
| 420 | self = PyObject_NEW(cdrobject, &Cdrtype); |
| 421 | if (self == NULL) |
| 422 | return NULL; |
| 423 | if ( name[0] > MAXNAME ) |
| 424 | nlen = MAXNAME; |
| 425 | else |
| 426 | nlen = name[0]; |
| 427 | memcpy(self->name, name+1, nlen); |
| 428 | self->name[nlen] = '\0'; |
| 429 | self->rtn = routine; |
| 430 | return self; |
| 431 | } |
| 432 | |
| 433 | static void |
| 434 | cdr_dealloc(self) |
| 435 | cdrobject *self; |
| 436 | { |
| 437 | PyMem_DEL(self); |
| 438 | } |
| 439 | |
| 440 | static PyObject * |
| 441 | cdr_repr(self) |
| 442 | cdrobject *self; |
| 443 | { |
| 444 | PyObject *s; |
| 445 | char buf[256]; |
| 446 | |
| 447 | sprintf(buf, "<Calldll routine %s address 0x%x>", self->name, self->rtn); |
| 448 | s = PyString_FromString(buf); |
| 449 | return s; |
| 450 | } |
| 451 | |
| 452 | static char Cdrtype__doc__[] = |
| 453 | "C Routine address" |
| 454 | ; |
| 455 | |
| 456 | static PyTypeObject Cdrtype = { |
| 457 | PyObject_HEAD_INIT(&PyType_Type) |
| 458 | 0, /*ob_size*/ |
| 459 | "routine", /*tp_name*/ |
| 460 | sizeof(cdrobject), /*tp_basicsize*/ |
| 461 | 0, /*tp_itemsize*/ |
| 462 | /* methods */ |
| 463 | (destructor)cdr_dealloc, /*tp_dealloc*/ |
| 464 | (printfunc)0, /*tp_print*/ |
| 465 | (getattrfunc)0, /*tp_getattr*/ |
| 466 | (setattrfunc)0, /*tp_setattr*/ |
| 467 | (cmpfunc)0, /*tp_compare*/ |
| 468 | (reprfunc)cdr_repr, /*tp_repr*/ |
| 469 | 0, /*tp_as_number*/ |
| 470 | 0, /*tp_as_sequence*/ |
| 471 | 0, /*tp_as_mapping*/ |
| 472 | (hashfunc)0, /*tp_hash*/ |
| 473 | (ternaryfunc)0, /*tp_call*/ |
| 474 | (reprfunc)0, /*tp_str*/ |
| 475 | |
| 476 | /* Space for future expansion */ |
| 477 | 0L,0L,0L,0L, |
| 478 | Cdrtype__doc__ /* Documentation string */ |
| 479 | }; |
| 480 | |
| 481 | /* End of code for routine objects */ |
| 482 | /* -------------------------------------------------------- */ |
| 483 | |
| 484 | |
| 485 | static struct PyMethodDef cdc_methods[] = { |
| 486 | |
| 487 | {NULL, NULL} /* sentinel */ |
| 488 | }; |
| 489 | |
| 490 | /* ---------- */ |
| 491 | |
| 492 | |
| 493 | static cdcobject * |
| 494 | newcdcobject(routine, npargs, npreturn, ncargs, argconv) |
| 495 | cdrobject *routine; |
| 496 | int npargs; |
| 497 | int npreturn; |
| 498 | int ncargs; |
| 499 | conventry *argconv[]; |
| 500 | { |
| 501 | cdcobject *self; |
| 502 | int i; |
| 503 | |
| 504 | self = PyObject_NEW(cdcobject, &Cdctype); |
| 505 | if (self == NULL) |
| 506 | return NULL; |
| 507 | self->routine = routine; |
| 508 | Py_INCREF(routine); |
| 509 | self->npargs = npargs; |
| 510 | self->npreturn = npreturn; |
| 511 | self->ncargs = ncargs; |
| 512 | for(i=0; i<MAXARG+1; i++) |
| 513 | if ( i < ncargs ) |
| 514 | self->argconv[i] = argconv[i]; |
| 515 | else |
| 516 | self->argconv[i] = 0; |
| 517 | return self; |
| 518 | } |
| 519 | |
| 520 | static void |
| 521 | cdc_dealloc(self) |
| 522 | cdcobject *self; |
| 523 | { |
| 524 | Py_XDECREF(self->routine); |
| 525 | PyMem_DEL(self); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | static PyObject * |
| 530 | cdc_repr(self) |
| 531 | cdcobject *self; |
| 532 | { |
| 533 | PyObject *s; |
| 534 | char buf[256]; |
| 535 | int i; |
| 536 | |
| 537 | sprintf(buf, "<callable %s = %s(", self->argconv[0]->name, self->routine->name); |
| 538 | for(i=1; i< self->ncargs; i++) { |
| 539 | strcat(buf, self->argconv[i]->name); |
| 540 | if ( i < self->ncargs-1 ) |
| 541 | strcat(buf, ", "); |
| 542 | } |
| 543 | strcat(buf, ") >"); |
| 544 | |
| 545 | s = PyString_FromString(buf); |
| 546 | return s; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | ** And this is what we all do it for: call a C function. |
| 551 | */ |
| 552 | static PyObject * |
| 553 | cdc_call(self, args, kwargs) |
| 554 | cdcobject *self; |
| 555 | PyObject *args; |
| 556 | PyObject *kwargs; |
| 557 | { |
| 558 | char buf[256]; |
| 559 | int i, pargindex; |
| 560 | anything c_args[MAXARG+1] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 561 | conventry *cp; |
| 562 | PyObject *curarg; |
| 563 | anyroutine func; |
| 564 | PyObject *rv0, *rv; |
| 565 | |
| 566 | if( kwargs ) { |
| 567 | PyErr_SetString(PyExc_TypeError, "Keyword args not allowed"); |
| 568 | return 0; |
| 569 | } |
| 570 | if( !PyTuple_Check(args) ) { |
| 571 | PyErr_SetString(PyExc_TypeError, "Arguments not in tuple"); |
| 572 | return 0; |
| 573 | } |
| 574 | if( PyTuple_Size(args) != self->npargs ) { |
| 575 | sprintf(buf, "%d arguments, expected %d", PyTuple_Size(args), self->npargs); |
| 576 | PyErr_SetString(PyExc_TypeError, buf); |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /* Decode arguments */ |
| 581 | pargindex = 0; |
| 582 | for(i=0; i<self->ncargs; i++) { |
| 583 | cp = self->argconv[i]; |
| 584 | if ( cp->get_uses_arg ) { |
| 585 | curarg = PyTuple_GET_ITEM(args, pargindex); |
| 586 | pargindex++; |
| 587 | } else { |
| 588 | curarg = (PyObject *)NULL; |
| 589 | } |
| 590 | c_args[i] = (*cp->get)(curarg); |
| 591 | } |
| 592 | if (PyErr_Occurred()) |
| 593 | return 0; |
| 594 | |
| 595 | /* Call function */ |
| 596 | func = self->routine->rtn; |
| 597 | *(anything *)c_args[0] = (*func)(c_args[1], c_args[2], c_args[3], c_args[4], |
| 598 | c_args[5], c_args[6], c_args[7], c_args[8]); |
| 599 | |
| 600 | /* Build return tuple (always a tuple, for now */ |
| 601 | if( (rv=PyTuple_New(self->npreturn)) == NULL ) |
| 602 | return NULL; |
| 603 | pargindex = 0; |
| 604 | for(i=0; i<self->ncargs; i++) { |
| 605 | cp = self->argconv[i]; |
| 606 | curarg = (*cp->put)(c_args[i]); |
| 607 | if( cp->put_gives_result ) |
| 608 | PyTuple_SET_ITEM(rv, pargindex, curarg); |
| 609 | /* NOTE: We only check errors at the end (so we free() everything) */ |
| 610 | } |
| 611 | if ( PyErr_Occurred() ) { |
| 612 | Py_DECREF(rv); |
| 613 | return NULL; |
| 614 | } |
| 615 | return rv; |
| 616 | } |
| 617 | |
| 618 | static char Cdctype__doc__[] = |
| 619 | "" |
| 620 | ; |
| 621 | |
| 622 | static PyTypeObject Cdctype = { |
| 623 | PyObject_HEAD_INIT(&PyType_Type) |
| 624 | 0, /*ob_size*/ |
| 625 | "callable", /*tp_name*/ |
| 626 | sizeof(cdcobject), /*tp_basicsize*/ |
| 627 | 0, /*tp_itemsize*/ |
| 628 | /* methods */ |
| 629 | (destructor)cdc_dealloc, /*tp_dealloc*/ |
| 630 | (printfunc)0, /*tp_print*/ |
| 631 | (getattrfunc)0, /*tp_getattr*/ |
| 632 | (setattrfunc)0, /*tp_setattr*/ |
| 633 | (cmpfunc)0, /*tp_compare*/ |
| 634 | (reprfunc)cdc_repr, /*tp_repr*/ |
| 635 | 0, /*tp_as_number*/ |
| 636 | 0, /*tp_as_sequence*/ |
| 637 | 0, /*tp_as_mapping*/ |
| 638 | (hashfunc)0, /*tp_hash*/ |
| 639 | (ternaryfunc)cdc_call, /*tp_call*/ |
| 640 | (reprfunc)0, /*tp_str*/ |
| 641 | |
| 642 | /* Space for future expansion */ |
| 643 | 0L,0L,0L,0L, |
| 644 | Cdctype__doc__ /* Documentation string */ |
| 645 | }; |
| 646 | |
| 647 | /* End of code for callable objects */ |
| 648 | /* ---------------------------------------------------------------- */ |
| 649 | |
| 650 | static struct PyMethodDef cdf_methods[] = { |
| 651 | |
| 652 | {NULL, NULL} /* sentinel */ |
| 653 | }; |
| 654 | |
| 655 | /* ---------- */ |
| 656 | |
| 657 | |
| 658 | static cdfobject * |
| 659 | newcdfobject(conn_id, name) |
| 660 | CFragConnectionID conn_id; |
| 661 | unsigned char *name; |
| 662 | { |
| 663 | cdfobject *self; |
| 664 | int nlen; |
| 665 | |
| 666 | self = PyObject_NEW(cdfobject, &Cdftype); |
| 667 | if (self == NULL) |
| 668 | return NULL; |
| 669 | self->conn_id = conn_id; |
| 670 | if ( name[0] > MAXNAME ) |
| 671 | nlen = MAXNAME; |
| 672 | else |
| 673 | nlen = name[0]; |
| 674 | strncpy(self->name, (char *)name+1, nlen); |
| 675 | self->name[nlen] = '\0'; |
| 676 | return self; |
| 677 | } |
| 678 | |
| 679 | static void |
| 680 | cdf_dealloc(self) |
| 681 | cdfobject *self; |
| 682 | { |
| 683 | PyMem_DEL(self); |
| 684 | } |
| 685 | |
| 686 | static PyObject * |
| 687 | cdf_repr(self) |
| 688 | cdfobject *self; |
| 689 | { |
| 690 | PyObject *s; |
| 691 | char buf[256]; |
| 692 | |
| 693 | sprintf(buf, "<fragment %s connection, id 0x%x>", self->name, self->conn_id); |
| 694 | s = PyString_FromString(buf); |
| 695 | return s; |
| 696 | } |
| 697 | |
| 698 | static PyObject * |
| 699 | cdf_getattr(self, name) |
| 700 | cdfobject *self; |
| 701 | char *name; |
| 702 | { |
| 703 | unsigned char *rtn_name; |
| 704 | anyroutine rtn; |
| 705 | OSErr err; |
| 706 | Str255 errMessage; |
| 707 | CFragSymbolClass class; |
| 708 | char buf[256]; |
| 709 | |
| 710 | rtn_name = Pstring(name); |
| 711 | err = FindSymbol(self->conn_id, rtn_name, (Ptr *)&rtn, &class); |
| 712 | if ( err ) { |
| 713 | sprintf(buf, "%.*s: %s", rtn_name[0], rtn_name+1, PyMac_StrError(err)); |
| 714 | PyErr_SetString(ErrorObject, buf); |
| 715 | return NULL; |
| 716 | } |
| 717 | if( class != kTVectorCFragSymbol ) { |
| 718 | PyErr_SetString(ErrorObject, "Symbol is not a routine"); |
| 719 | return NULL; |
| 720 | } |
| 721 | |
| 722 | return (PyObject *)newcdrobject(rtn_name, rtn); |
| 723 | } |
| 724 | /* -------------------------------------------------------- */ |
| 725 | |
| 726 | static char Cdftype__doc__[] = |
| 727 | "Code Fragment library symbol table" |
| 728 | ; |
| 729 | |
| 730 | static PyTypeObject Cdftype = { |
| 731 | PyObject_HEAD_INIT(&PyType_Type) |
| 732 | 0, /*ob_size*/ |
| 733 | "fragment", /*tp_name*/ |
| 734 | sizeof(cdfobject), /*tp_basicsize*/ |
| 735 | 0, /*tp_itemsize*/ |
| 736 | /* methods */ |
| 737 | (destructor)cdf_dealloc, /*tp_dealloc*/ |
| 738 | (printfunc)0, /*tp_print*/ |
| 739 | (getattrfunc)cdf_getattr, /*tp_getattr*/ |
| 740 | (setattrfunc)0, /*tp_setattr*/ |
| 741 | (cmpfunc)0, /*tp_compare*/ |
| 742 | (reprfunc)cdf_repr, /*tp_repr*/ |
| 743 | 0, /*tp_as_number*/ |
| 744 | 0, /*tp_as_sequence*/ |
| 745 | 0, /*tp_as_mapping*/ |
| 746 | (hashfunc)0, /*tp_hash*/ |
| 747 | (ternaryfunc)0, /*tp_call*/ |
| 748 | (reprfunc)0, /*tp_str*/ |
| 749 | |
| 750 | /* Space for future expansion */ |
| 751 | 0L,0L,0L,0L, |
| 752 | Cdftype__doc__ /* Documentation string */ |
| 753 | }; |
| 754 | |
| 755 | /* End of code for fragment objects */ |
| 756 | /* -------------------------------------------------------- */ |
| 757 | |
| 758 | |
| 759 | static char cdll_getlibrary__doc__[] = |
| 760 | "Load a shared library fragment and return the symbol table" |
| 761 | ; |
| 762 | |
| 763 | static PyObject * |
| 764 | cdll_getlibrary(self, args) |
| 765 | PyObject *self; /* Not used */ |
| 766 | PyObject *args; |
| 767 | { |
| 768 | Str255 frag_name; |
| 769 | OSErr err; |
| 770 | Str255 errMessage; |
| 771 | Ptr main_addr; |
| 772 | CFragConnectionID conn_id; |
| 773 | char buf[256]; |
| 774 | |
| 775 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, frag_name)) |
| 776 | return NULL; |
| 777 | |
| 778 | /* Find the library connection ID */ |
| 779 | err = GetSharedLibrary(frag_name, kCurrentCFragArch, kLoadCFrag, &conn_id, &main_addr, |
| 780 | errMessage); |
| 781 | if ( err ) { |
| 782 | sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err)); |
| 783 | PyErr_SetString(ErrorObject, buf); |
| 784 | return NULL; |
| 785 | } |
| 786 | return (PyObject *)newcdfobject(conn_id, frag_name); |
| 787 | } |
| 788 | |
| 789 | static char cdll_getdiskfragment__doc__[] = |
| 790 | "Load a fragment from a disk file and return the symbol table" |
| 791 | ; |
| 792 | |
| 793 | static PyObject * |
| 794 | cdll_getdiskfragment(self, args) |
| 795 | PyObject *self; /* Not used */ |
| 796 | PyObject *args; |
| 797 | { |
| 798 | FSSpec fsspec; |
| 799 | Str255 frag_name; |
| 800 | OSErr err; |
| 801 | Str255 errMessage; |
| 802 | Ptr main_addr; |
| 803 | CFragConnectionID conn_id; |
| 804 | char buf[256]; |
| 805 | Boolean isfolder, didsomething; |
| 806 | |
| 807 | if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetFSSpec, &fsspec, |
| 808 | PyMac_GetStr255, frag_name)) |
| 809 | return NULL; |
| 810 | err = ResolveAliasFile(&fsspec, 1, &isfolder, &didsomething); |
| 811 | if ( err ) |
| 812 | return PyErr_Mac(ErrorObject, err); |
| 813 | |
| 814 | /* Load the fragment (or return the connID if it is already loaded */ |
| 815 | err = GetDiskFragment(&fsspec, 0, 0, frag_name, |
| 816 | kLoadCFrag, &conn_id, &main_addr, |
| 817 | errMessage); |
| 818 | if ( err ) { |
| 819 | sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err)); |
| 820 | PyErr_SetString(ErrorObject, buf); |
| 821 | return NULL; |
| 822 | } |
| 823 | return (PyObject *)newcdfobject(conn_id, frag_name); |
| 824 | } |
| 825 | |
| 826 | static char cdll_newcall__doc__[] = |
| 827 | "" |
| 828 | ; |
| 829 | |
| 830 | static PyObject * |
| 831 | cdll_newcall(self, args) |
| 832 | PyObject *self; /* Not used */ |
| 833 | PyObject *args; |
| 834 | { |
| 835 | cdrobject *routine; |
| 836 | conventry *argconv[MAXARG+1] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 837 | int npargs, npreturn, ncargs; |
| 838 | |
| 839 | /* Note: the next format depends on MAXARG+1 */ |
| 840 | if (!PyArg_ParseTuple(args, "O!O&|O&O&O&O&O&O&O&O&", &Cdrtype, &routine, |
| 841 | argparse_conv, &argconv[0], argparse_conv, &argconv[1], |
| 842 | argparse_conv, &argconv[2], argparse_conv, &argconv[3], |
| 843 | argparse_conv, &argconv[4], argparse_conv, &argconv[5], |
| 844 | argparse_conv, &argconv[6], argparse_conv, &argconv[7], |
| 845 | argparse_conv, &argconv[8])) |
| 846 | return NULL; |
| 847 | npargs = npreturn = 0; |
| 848 | for(ncargs=0; ncargs < MAXARG+1 && argconv[ncargs]; ncargs++) { |
| 849 | if( argconv[ncargs]->get_uses_arg ) npargs++; |
| 850 | if( argconv[ncargs]->put_gives_result ) npreturn++; |
| 851 | } |
| 852 | return (PyObject *)newcdcobject(routine, npargs, npreturn, ncargs, argconv); |
| 853 | } |
| 854 | |
| 855 | /* List of methods defined in the module */ |
| 856 | |
| 857 | static struct PyMethodDef cdll_methods[] = { |
| 858 | {"getlibrary", (PyCFunction)cdll_getlibrary, METH_VARARGS, |
| 859 | cdll_getlibrary__doc__}, |
| 860 | {"getdiskfragment", (PyCFunction)cdll_getdiskfragment, METH_VARARGS, |
| 861 | cdll_getdiskfragment__doc__}, |
| 862 | {"newcall", (PyCFunction)cdll_newcall, METH_VARARGS, |
| 863 | cdll_newcall__doc__}, |
| 864 | |
| 865 | {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */ |
| 866 | }; |
| 867 | |
| 868 | |
| 869 | /* Initialization function for the module (*must* be called initcalldll) */ |
| 870 | |
| 871 | static char calldll_module_documentation[] = |
| 872 | "" |
| 873 | ; |
| 874 | |
| 875 | void |
| 876 | initcalldll() |
| 877 | { |
| 878 | PyObject *m, *d; |
| 879 | |
| 880 | /* Create the module and add the functions */ |
| 881 | m = Py_InitModule4("calldll", cdll_methods, |
| 882 | calldll_module_documentation, |
| 883 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 884 | |
| 885 | /* Add some symbolic constants to the module */ |
| 886 | d = PyModule_GetDict(m); |
| 887 | ErrorObject = PyString_FromString("calldll.error"); |
| 888 | PyDict_SetItemString(d, "error", ErrorObject); |
| 889 | |
| 890 | /* XXXX Add constants here */ |
| 891 | |
| 892 | /* Check for errors */ |
| 893 | if (PyErr_Occurred()) |
| 894 | Py_FatalError("can't initialize module calldll"); |
| 895 | } |
| 896 | |
| 897 | /* Test routine */ |
| 898 | int calldlltester(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8) |
| 899 | { |
| 900 | printf("Tester1: %x %x %x %x %x %x %x %x\n", a1, a2, a3, a4, a5, a6, a7, a8); |
| 901 | return a1; |
| 902 | } |
| 903 | |