| 1 | /***********************************************************
|
|---|
| 2 | Copyright 1991-1997 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 not be used in advertising or publicity pertaining to
|
|---|
| 13 | distribution of the software without specific, written prior permission.
|
|---|
| 14 |
|
|---|
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|---|
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|---|
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|---|
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|---|
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|---|
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|---|
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|---|
| 22 |
|
|---|
| 23 | ******************************************************************/
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | #include "Python.h"
|
|---|
| 27 | #include "pymactoolbox.h"
|
|---|
| 28 | #include <arpa/inet.h> /* for ntohl, htonl */
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | /* Like strerror() but for Mac OS error numbers */
|
|---|
| 32 | char *
|
|---|
| 33 | PyMac_StrError(int err)
|
|---|
| 34 | {
|
|---|
| 35 | static char buf[256];
|
|---|
| 36 | PyObject *m;
|
|---|
| 37 | PyObject *rv;
|
|---|
| 38 |
|
|---|
| 39 | m = PyImport_ImportModule("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 | }
|
|---|
| 68 |
|
|---|
| 69 | /* Exception object shared by all Mac specific modules for Mac OS errors */
|
|---|
| 70 | PyObject *PyMac_OSErrException;
|
|---|
| 71 |
|
|---|
| 72 | /* Initialize and return PyMac_OSErrException */
|
|---|
| 73 | PyObject *
|
|---|
| 74 | PyMac_GetOSErrException(void)
|
|---|
| 75 | {
|
|---|
| 76 | if (PyMac_OSErrException == NULL)
|
|---|
| 77 | PyMac_OSErrException = PyErr_NewException("MacOS.Error", NULL, NULL);
|
|---|
| 78 | return PyMac_OSErrException;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */
|
|---|
| 82 | PyObject *
|
|---|
| 83 | PyErr_Mac(PyObject *eobj, int err)
|
|---|
| 84 | {
|
|---|
| 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 | }
|
|---|
| 100 |
|
|---|
| 101 | /* Call PyErr_Mac with PyMac_OSErrException */
|
|---|
| 102 | PyObject *
|
|---|
| 103 | PyMac_Error(OSErr err)
|
|---|
| 104 | {
|
|---|
| 105 | return PyErr_Mac(PyMac_GetOSErrException(), err);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | OSErr
|
|---|
| 110 | PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
|
|---|
| 111 | {
|
|---|
| 112 | PyObject *fs, *exc;
|
|---|
| 113 | PyObject *rv = NULL;
|
|---|
| 114 | char *input;
|
|---|
| 115 | OSErr err = noErr;
|
|---|
| 116 |
|
|---|
| 117 | *path = '\0';
|
|---|
| 118 |
|
|---|
| 119 | fs = PyMac_BuildFSSpec(fss);
|
|---|
| 120 | if (!fs)
|
|---|
| 121 | goto error;
|
|---|
| 122 |
|
|---|
| 123 | rv = PyObject_CallMethod(fs, "as_pathname", "");
|
|---|
| 124 | if (!rv)
|
|---|
| 125 | goto error;
|
|---|
| 126 |
|
|---|
| 127 | input = PyString_AsString(rv);
|
|---|
| 128 | if (!input)
|
|---|
| 129 | goto error;
|
|---|
|
|---|