Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Mac/Modules/MacOS.c

    r2 r391  
    55                        All Rights Reserved
    66
    7 Permission to use, copy, modify, and distribute this software and its 
    8 documentation for any purpose and without fee is hereby granted, 
     7Permission to use, copy, modify, and distribute this software and its
     8documentation for any purpose and without fee is hereby granted,
    99provided that the above copyright notice appear in all copies and that
    10 both that copyright notice and this permission notice appear in 
     10both that copyright notice and this permission notice appear in
    1111supporting documentation, and that the names of Stichting Mathematisch
    1212Centrum or CWI not be used in advertising or publicity pertaining to
     
    3131#include <ApplicationServices/ApplicationServices.h>
    3232
     33#include <arpa/inet.h>  /* for ntohl, htonl */
     34
     35
    3336#ifndef HAVE_OSX105_SDK
    34 typedef SInt16  FSIORefNum;
     37typedef SInt16  FSIORefNum;
    3538#endif
    3639
     
    4447
    4548typedef struct {
    46         PyObject_HEAD
    47         FSIORefNum fRefNum;
    48         int isclosed;
     49    PyObject_HEAD
     50    FSIORefNum fRefNum;
     51    int isclosed;
    4952} rfobject;
    5053
     
    5861do_close(rfobject *self)
    5962{
    60         if (self->isclosed ) return;
    61         (void)FSCloseFork(self->fRefNum);
    62         self->isclosed = 1;
    63 }
    64 
    65 static char rf_read__doc__[] = 
     63    if (self->isclosed ) return;
     64    (void)FSCloseFork(self->fRefNum);
     65    self->isclosed = 1;
     66}
     67
     68static char rf_read__doc__[] =
    6669"Read data from resource fork"
    6770;
     
    7073rf_read(rfobject *self, PyObject *args)
    7174{
    72         long n;
    73         PyObject *v;
    74         OSErr err;
    75         ByteCount n2;
    76        
    77         if (self->isclosed) {
    78                 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
    79                 return NULL;
    80         }
    81        
    82         if (!PyArg_ParseTuple(args, "l", &n))
    83                 return NULL;
    84                
    85         v = PyBytes_FromStringAndSize((char *)NULL, n);
    86         if (v == NULL)
    87                 return NULL;
    88                
    89         err = FSReadFork(self->fRefNum, fsAtMark, 0, n, PyString_AsString(v), &n2);
    90         if (err && err != eofErr) {
    91                 PyMac_Error(err);
    92                 Py_DECREF(v);
    93                 return NULL;
    94         }
    95         _PyString_Resize(&v, n2);
    96         return v;
    97 }
    98 
    99 
    100 static char rf_write__doc__[] = 
     75    long n;
     76    PyObject *v;
     77    OSErr err;
     78    ByteCount n2;
     79
     80    if (self->isclosed) {
     81        PyErr_SetString(PyExc_ValueError, "Operation on closed file");
     82        return NULL;
     83    }
     84
     85    if (!PyArg_ParseTuple(args, "l", &n))
     86        return NULL;
     87
     88    v = PyBytes_FromStringAndSize((char *)NULL, n);
     89    if (v == NULL)
     90        return NULL;
     91
     92    err = FSReadFork(self->fRefNum, fsAtMark, 0, n, PyString_AsString(v), &n2);
     93    if (err && err != eofErr) {
     94        PyMac_Error(err);
     95        Py_DECREF(v);
     96        return NULL;
     97    }
     98    _PyString_Resize(&v, n2);
     99    return v;
     100}
     101
     102
     103static char rf_write__doc__[] =
    101104"Write to resource fork"
    102105;
     
    105108rf_write(rfobject *self, PyObject *args)
    106109{
    107         char *buffer;
    108         long size;
    109         OSErr err;
    110        
    111         if (self->isclosed) {
    112                 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
    113                 return NULL;
    114         }
    115         if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
    116                 return NULL;
    117         err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL);
    118         if (err) {
    119                 PyMac_Error(err);
    120                 return NULL;
    121         }
    122         Py_INCREF(Py_None);
    123         return Py_None;
    124 }
    125 
    126 
    127 static char rf_seek__doc__[] = 
     110    char *buffer;
     111    long size;
     112    OSErr err;
     113
     114    if (self->isclosed) {
     115        PyErr_SetString(PyExc_ValueError, "Operation on closed file");
     116        return NULL;
     117    }
     118    if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
     119        return NULL;
     120    err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL);
     121    if (err) {
     122        PyMac_Error(err);
     123        return NULL;
     124    }
     125    Py_INCREF(Py_None);
     126    return Py_None;
     127}
     128
     129
     130static char rf_seek__doc__[] =
    128131"Set file position"
    129132;
     
    132135rf_seek(rfobject *self, PyObject *args)
    133136{
    134         long amount;
    135         int whence = SEEK_SET;
    136         int mode;
    137         OSErr err;
    138        
    139         if (self->isclosed) {
    140                 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
    141                 return NULL;
    142         }
    143         if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) {
    144                 return NULL;
    145         }
    146        
    147         switch (whence) {
    148         case SEEK_CUR:
    149                 mode = fsFromMark;
    150                 break;
    151         case SEEK_END:
    152                 mode = fsFromLEOF;
    153                 break;
    154         case SEEK_SET:
    155                 mode = fsFromStart;
    156                 break;
    157         default:
    158                 PyErr_BadArgument();
    159                 return NULL;
    160         }
    161 
    162         err = FSSetForkPosition(self->fRefNum, mode, amount);
    163         if (err != noErr) {
    164                 PyMac_Error(err);
    165                 return NULL;
    166         }
    167         Py_INCREF(Py_None);
    168         return Py_None;
    169 }
    170 
    171 
    172 static char rf_tell__doc__[] = 
     137    long amount;
     138    int whence = SEEK_SET;
     139    int mode;
     140    OSErr err;
     141
     142    if (self->isclosed) {
     143        PyErr_SetString(PyExc_ValueError, "Operation on closed file");
     144        return NULL;
     145    }
     146    if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) {
     147        return NULL;
     148    }
     149
     150    switch (whence) {
     151    case SEEK_CUR:
     152        mode = fsFromMark;
     153        break;
     154    case SEEK_END:
     155        mode = fsFromLEOF;
     156        break;
     157    case SEEK_SET:
     158        mode = fsFromStart;
     159        break;
     160    default:
     161        PyErr_BadArgument();
     162        return NULL;
     163    }
     164
     165    err = FSSetForkPosition(self->fRefNum, mode, amount);
     166    if (err != noErr) {
     167        PyMac_Error(err);
     168        return NULL;
     169    }
     170    Py_INCREF(Py_None);
     171    return Py_None;
     172}
     173
     174
     175static char rf_tell__doc__[] =
    173176"Get file position"
    174177;
     
    177180rf_tell(rfobject *self, PyObject *args)
    178181{
    179         long long where;
    180         OSErr err;
    181        
    182         if (self->isclosed) {
    183                 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
    184                 return NULL;
    185         }
    186         if (!PyArg_ParseTuple(args, ""))
    187                 return NULL;
    188 
    189         err = FSGetForkPosition(self->fRefNum, &where);
    190         if (err != noErr) {
    191                 PyMac_Error(err);
    192                 return NULL;
    193         }
    194         return PyLong_FromLongLong(where);
    195 }
    196 
    197 static char rf_close__doc__[] = 
     182    long long where;
     183    OSErr err;
     184
     185    if (self->isclosed) {
     186        PyErr_SetString(PyExc_ValueError, "Operation on closed file");
     187        return NULL;
     188    }
     189    if (!PyArg_ParseTuple(args, ""))
     190        return NULL;
     191
     192    err = FSGetForkPosition(self->fRefNum, &where);
     193    if (err != noErr) {
     194        PyMac_Error(err);
     195        return NULL;
     196    }
     197    return PyLong_FromLongLong(where);
     198}
     199
     200static char rf_close__doc__[] =
    198201"Close resource fork"
    199202;
     
    202205rf_close(rfobject *self, PyObject *args)
    203206{
    204         if (!PyArg_ParseTuple(args, ""))
    205                 return NULL;
    206         do_close(self);
    207         Py_INCREF(Py_None);
    208         return Py_None;
     207    if (!PyArg_ParseTuple(args, ""))
     208        return NULL;
     209    do_close(self);
     210    Py_INCREF(Py_None);
     211    return Py_None;
    209212}
    210213
    211214
    212215static struct PyMethodDef rf_methods[] = {
    213  {"read",       (PyCFunction)rf_read,   1,      rf_read__doc__},
    214  {"write",      (PyCFunction)rf_write,  1,      rf_write__doc__},
    215  {"seek",       (PyCFunction)rf_seek,   1,      rf_seek__doc__},
    216  {"tell",       (PyCFunction)rf_tell,   1,      rf_tell__doc__},
    217  {"close",      (PyCFunction)rf_close,  1,      rf_close__doc__},
    218  
    219         {NULL,          NULL}           /* sentinel */
     216 {"read",       (PyCFunction)rf_read,   1,      rf_read__doc__},
     217 {"write",      (PyCFunction)rf_write,  1,      rf_write__doc__},
     218 {"seek",       (PyCFunction)rf_seek,   1,      rf_seek__doc__},
     219 {"tell",       (PyCFunction)rf_tell,   1,      rf_tell__doc__},
     220 {"close",      (PyCFunction)rf_close,  1,      rf_close__doc__},
     221
     222    {NULL,              NULL}           /* sentinel */
    220223};
    221224
     
    226229newrfobject(void)
    227230{
    228         rfobject *self;
    229        
    230         self = PyObject_NEW(rfobject, &Rftype);
    231         if (self == NULL)
    232                 return NULL;
    233         self->isclosed = 1;
    234         return self;
     231    rfobject *self;
     232
     233    self = PyObject_NEW(rfobject, &Rftype);
     234    if (self == NULL)
     235        return NULL;
     236    self->isclosed = 1;
     237    return self;
    235238}
    236239
     
    239242rf_dealloc(rfobject *self)
    240243{
    241         do_close(self);
    242         PyObject_DEL(self);
     244    do_close(self);
     245    PyObject_DEL(self);
    243246}
    244247
     
    246249rf_getattr(rfobject *self, char *name)
    247250{
    248         return Py_FindMethod(rf_methods, (PyObject *)self, name);
    249 }
    250 
    251 static char Rftype__doc__[] = 
     251    return Py_FindMethod(rf_methods, (PyObject *)self, name);
     252}
     253
     254static char Rftype__doc__[] =
    252255"Resource fork file object"
    253256;
    254257
    255258static PyTypeObject Rftype = {
    256         PyObject_HEAD_INIT(&PyType_Type)
    257         0,                              /*ob_size*/
    258         "MacOS.ResourceFork",           /*tp_name*/
    259         sizeof(rfobject),               /*tp_basicsize*/
    260         0,                              /*tp_itemsize*/
    261         /* methods */
    262         (destructor)rf_dealloc, /*tp_dealloc*/
    263         (printfunc)0,           /*tp_print*/
    264         (getattrfunc)rf_getattr,        /*tp_getattr*/
    265         (setattrfunc)0, /*tp_setattr*/
    266         (cmpfunc)0,             /*tp_compare*/
    267         (reprfunc)0,            /*tp_repr*/
    268         0,                      /*tp_as_number*/
    269         0,              /*tp_as_sequence*/
    270         0,              /*tp_as_mapping*/
    271         (hashfunc)0,            /*tp_hash*/
    272         (ternaryfunc)0,         /*tp_call*/
    273         (reprfunc)0,            /*tp_str*/
    274 
    275         /* Space for future expansion */
    276         0L,0L,0L,0L,
    277         Rftype__doc__ /* Documentation string */
     259    PyObject_HEAD_INIT(&PyType_Type)
     260    0,                                  /*ob_size*/
     261    "MacOS.ResourceFork",               /*tp_name*/
     262    sizeof(rfobject),                   /*tp_basicsize*/
     263    0,                                  /*tp_itemsize*/
     264    /* methods */
     265    (destructor)rf_dealloc,     /*tp_dealloc*/
     266    (printfunc)0,               /*tp_print*/
     267    (getattrfunc)rf_getattr,            /*tp_getattr*/
     268    (setattrfunc)0,     /*tp_setattr*/
     269    (cmpfunc)0,                 /*tp_compare*/
     270    (reprfunc)0,                /*tp_repr*/
     271    0,                          /*tp_as_number*/
     272    0,                  /*tp_as_sequence*/
     273    0,                  /*tp_as_mapping*/
     274    (hashfunc)0,                /*tp_hash*/
     275    (ternaryfunc)0,             /*tp_call*/
     276    (reprfunc)0,                /*tp_str*/
     277
     278    /* Space for future expansion */
     279    0L,0L,0L,0L,
     280    Rftype__doc__ /* Documentation string */
    278281};
    279282
     
    290293MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
    291294{
    292         PyObject *creator, *type, *res;
    293         OSErr err;
    294         FSRef ref;
    295         FSCatalogInfo   cataloginfo;
    296         FileInfo* finfo;
    297 
    298         if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) {
     295    PyObject *creator, *type, *res;
     296    OSErr err;
     297    FSRef ref;
     298    FSCatalogInfo       cataloginfo;
     299    FileInfo* finfo;
     300
     301    if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) {
    299302#ifndef __LP64__
    300                 /* This function is documented to take an FSSpec as well,
    301                  * which only works in 32-bit mode.
    302                  */
    303                 PyErr_Clear();
    304                 FSSpec fss;
    305                 FInfo info;
    306 
    307                 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
    308                         return NULL;
    309 
    310                 if ((err = FSpGetFInfo(&fss, &info)) != noErr) {
    311                         return PyErr_Mac(MacOS_Error, err);
    312                 }
    313                 creator = PyString_FromStringAndSize(
    314                                 (char *)&info.fdCreator, 4);
    315                 type = PyString_FromStringAndSize((char *)&info.fdType, 4);
    316                 res = Py_BuildValue("OO", creator, type);
    317                 Py_DECREF(creator);
    318                 Py_DECREF(type);
    319                 return res;
    320 #else   /* __LP64__ */
    321                 return NULL;
    322 #endif  /* __LP64__ */
    323         }
    324 
    325         err = FSGetCatalogInfo(&ref,
    326                         kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo,
    327                         NULL, NULL, NULL);
    328         if (err != noErr) {
    329                 PyErr_Mac(MacOS_Error, err);
    330                 return NULL;
    331         }
    332 
    333         if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
    334                 /* Directory: doesn't have type/creator info.
    335                  *
    336                  * The specific error code is for backward compatibility with
    337                  * earlier versions.
    338                  */
    339                 PyErr_Mac(MacOS_Error, fnfErr);
    340                 return NULL;
    341 
    342         }
    343         finfo = (FileInfo*)&(cataloginfo.finderInfo);
    344         creator = PyString_FromStringAndSize((char*)&(finfo->fileCreator), 4);
    345         type = PyString_FromStringAndSize((char*)&(finfo->fileType), 4);
    346 
    347         res = Py_BuildValue("OO", creator, type);
    348         Py_DECREF(creator);
    349         Py_DECREF(type);
    350         return res;
     303        /* This function is documented to take an FSSpec as well,
     304         * which only works in 32-bit mode.
     305         */
     306        PyErr_Clear();
     307        FSSpec fss;
     308        FInfo info;
     309
     310        if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
     311            return NULL;
     312
     313        if ((err = FSpGetFInfo(&fss, &info)) != noErr) {
     314            return PyErr_Mac(MacOS_Error, err);
     315        }
     316
     317        info.fdCreator = ntohl(info.fdCreator);
     318        info.fdType = ntohl(info.fdType);
     319
     320        creator = PyString_FromStringAndSize(
     321                        (char *)&info.fdCreator, 4);
     322        type = PyString_FromStringAndSize((char *)&info.fdType, 4);
     323        res = Py_BuildValue("OO", creator, type);
     324        Py_DECREF(creator);
     325        Py_DECREF(type);
     326        return res;
     327#else   /* __LP64__ */
     328        return NULL;
     329#endif  /* __LP64__ */
     330    }
     331
     332    err = FSGetCatalogInfo(&ref,
     333                    kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo,
     334                    NULL, NULL, NULL);
     335    if (err != noErr) {
     336        PyErr_Mac(MacOS_Error, err);
     337        return NULL;
     338    }
     339
     340    if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
     341        /* Directory: doesn't have type/creator info.
     342         *
     343         * The specific error code is for backward compatibility with
     344         * earlier versions.
     345         */
     346        PyErr_Mac(MacOS_Error, fnfErr);
     347        return NULL;
     348
     349    }
     350    finfo = (FileInfo*)&(cataloginfo.finderInfo);
     351    finfo->fileCreator = ntohl(finfo->fileCreator);
     352    finfo->fileType = ntohl(finfo->fileType);
     353    creator = PyString_FromStringAndSize((char*)&(finfo->fileCreator), 4);
     354    type = PyString_FromStringAndSize((char*)&(finfo->fileType), 4);
     355
     356    res = Py_BuildValue("OO", creator, type);
     357    Py_DECREF(creator);
     358    Py_DECREF(type);
     359    return res;
    351360}
    352361
     
    356365MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
    357366{
    358         ResType creator, type;
    359         FSRef ref;
    360         FileInfo* finfo;
    361         OSErr err;
    362         FSCatalogInfo   cataloginfo;
    363 
    364         if (!PyArg_ParseTuple(args, "O&O&O&",
    365                         PyMac_GetFSRef, &ref, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) {
     367    ResType creator, type;
     368    FSRef ref;
     369    FileInfo* finfo;
     370    OSErr err;
     371    FSCatalogInfo       cataloginfo;
     372
     373    if (!PyArg_ParseTuple(args, "O&O&O&",
     374                    PyMac_GetFSRef, &ref, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) {
    366375#ifndef __LP64__
    367                 /* Try to handle FSSpec arguments, for backward compatibility */
    368                 FSSpec fss;
    369                 FInfo info;
    370 
    371                 if (!PyArg_ParseTuple(args, "O&O&O&",
    372                         PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
    373                         return NULL;
    374 
    375                 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
    376                         return PyErr_Mac(MacOS_Error, err);
    377 
    378                 info.fdCreator = creator;
    379                 info.fdType = type;
    380 
    381                 if ((err = FSpSetFInfo(&fss, &info)) != noErr)
    382                         return PyErr_Mac(MacOS_Error, err);
    383                 Py_INCREF(Py_None);
    384                 return Py_None;
     376        /* Try to handle FSSpec arguments, for backward compatibility */
     377        FSSpec fss;
     378        FInfo info;
     379
     380        if (!PyArg_ParseTuple(args, "O&O&O&",
     381            PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
     382            return NULL;
     383
     384        if ((err = FSpGetFInfo(&fss, &info)) != noErr)
     385            return PyErr_Mac(MacOS_Error, err);
     386
     387        info.fdCreator = creator;
     388        info.fdType = type;
     389
     390        if ((err = FSpSetFInfo(&fss, &info)) != noErr)
     391            return PyErr_Mac(MacOS_Error, err);
     392        Py_INCREF(Py_None);
     393        return Py_None;
    385394#else /* __LP64__ */
    386                 return NULL;
     395        return NULL;
    387396#endif /* __LP64__ */
    388         }
    389        
    390         err = FSGetCatalogInfo(&ref,
    391                         kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo,
    392                         NULL, NULL, NULL);
    393         if (err != noErr) {
    394                 PyErr_Mac(MacOS_Error, err);
    395                 return NULL;
    396         }
    397 
    398         if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
    399                 /* Directory: doesn't have type/creator info.
    400                 *
    401                 * The specific error code is for backward compatibility with
    402                 * earlier versions.
    403                 */
    404                 PyErr_Mac(MacOS_Error, fnfErr);
    405                 return NULL;
    406 
    407         }
    408         finfo = (FileInfo*)&(cataloginfo.finderInfo);
    409         finfo->fileCreator = creator;
    410         finfo->fileType = type;
    411 
    412         err = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &cataloginfo);
    413         if (err != noErr) {
    414                 PyErr_Mac(MacOS_Error, fnfErr);
    415                 return NULL;
    416         }
    417 
    418         Py_INCREF(Py_None);
    419         return Py_None;
     397    }
     398
     399    err = FSGetCatalogInfo(&ref,
     400                    kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo,
     401                    NULL, NULL, NULL);
     402    if (err != noErr) {
     403        PyErr_Mac(MacOS_Error, err);
     404        return NULL;
     405    }
     406
     407    if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
     408        /* Directory: doesn't have type/creator info.
     409        *
     410        * The specific error code is for backward compatibility with
     411        * earlier versions.
     412        */
     413        PyErr_Mac(MacOS_Error, fnfErr);
     414        return NULL;
     415
     416    }
     417    finfo = (FileInfo*)&(cataloginfo.finderInfo);
     418    finfo->fileCreator = creator;
     419    finfo->fileType = type;
     420
     421    err = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &cataloginfo);
     422    if (err != noErr) {
     423        PyErr_Mac(MacOS_Error, fnfErr);
     424        return NULL;
     425    }
     426
     427    Py_INCREF(Py_None);
     428    return Py_None;
    420429}
    421430
     
    426435MacOS_GetErrorString(PyObject *self, PyObject *args)
    427436{
    428         int err;
    429         char buf[256];
    430         Handle h;
    431         char *str;
    432         static int errors_loaded;
    433        
    434         if (!PyArg_ParseTuple(args, "i", &err))
    435                 return NULL;
    436 
    437         h = GetResource('Estr', err);
    438         if (!h && !errors_loaded) {
    439                 /*
    440                 ** Attempt to open the resource file containing the
    441                 ** Estr resources. We ignore all errors. We also try
    442                 ** this only once.
    443                 */
    444                 PyObject *m, *rv;
    445                 errors_loaded = 1;
    446                
    447                 m = PyImport_ImportModuleNoBlock("macresource");
    448                 if (!m) {
    449                         if (Py_VerboseFlag)
    450                                 PyErr_Print();
    451                         PyErr_Clear();
    452                 }
    453                 else {
    454                         rv = PyObject_CallMethod(m, "open_error_resource", "");
    455                         if (!rv) {
    456                                 if (Py_VerboseFlag)
    457                                         PyErr_Print();
    458                                 PyErr_Clear();
    459                         }
    460                         else {
    461                                 Py_DECREF(rv);
    462                                 /* And try again... */
    463                                 h = GetResource('Estr', err);
    464                         }
    465                         Py_DECREF(m);
    466                 }
    467         }
    468         /*
    469         ** Whether the code above succeeded or not, we won't try
    470         ** again.
    471         */
    472         errors_loaded = 1;
    473                
    474         if (h) {
    475                 HLock(h);
    476                 str = (char *)*h;
    477                 memcpy(buf, str+1, (unsigned char)str[0]);
    478                 buf[(unsigned char)str[0]] = '\0';
    479                 HUnlock(h);
    480                 ReleaseResource(h);
    481         }
    482         else {
    483                 PyOS_snprintf(buf, sizeof(buf), "Mac OS error code %d", err);
    484         }
    485 
    486         return Py_BuildValue("s", buf);
     437    int err;
     438    char buf[256];
     439    Handle h;
     440    char *str;
     441    static int errors_loaded;
     442
     443    if (!PyArg_ParseTuple(args, "i", &err))
     444        return NULL;
     445
     446    h = GetResource('Estr', err);
     447    if (!h && !errors_loaded) {
     448        /*
     449        ** Attempt to open the resource file containing the
     450        ** Estr resources. We ignore all errors. We also try
     451        ** this only once.
     452        */
     453        PyObject *m, *rv;
     454        errors_loaded = 1;
     455
     456        m = PyImport_ImportModuleNoBlock("macresource");
     457        if (!m) {
     458            if (Py_VerboseFlag)
     459                PyErr_Print();
     460            PyErr_Clear();
     461        }
     462        else {
     463            rv = PyObject_CallMethod(m, "open_error_resource", "");
     464            if (!rv) {
     465                if (Py_VerboseFlag)
     466                    PyErr_Print();
     467                PyErr_Clear();
     468            }
     469            else {
     470                Py_DECREF(rv);
     471                /* And try again... */
     472                h = GetResource('Estr', err);
     473            }
     474            Py_DECREF(m);
     475        }
     476    }
     477    /*
     478    ** Whether the code above succeeded or not, we won't try
     479    ** again.
     480    */
     481    errors_loaded = 1;
     482
     483    if (h) {
     484        HLock(h);
     485        str = (char *)*h;
     486        memcpy(buf, str+1, (unsigned char)str[0]);
     487        buf[(unsigned char)str[0]] = '\0';
     488        HUnlock(h);
     489        ReleaseResource(h);
     490    }
     491    else {
     492        PyOS_snprintf(buf, sizeof(buf), "Mac OS error code %d", err);
     493    }
     494
     495    return Py_BuildValue("s", buf);
    487496}
    488497
     
    495504MacOS_splash(PyObject *self, PyObject *args)
    496505{
    497         int resid = -1;
    498         static DialogPtr curdialog = NULL;
    499         DialogPtr olddialog;
    500         WindowRef theWindow;
    501         CGrafPtr thePort;
     506    int resid = -1;
     507    static DialogPtr curdialog = NULL;
     508    DialogPtr olddialog;
     509    WindowRef theWindow;
     510    CGrafPtr thePort;
    502511#if 0
    503         short xpos, ypos, width, height, swidth, sheight;
     512    short xpos, ypos, width, height, swidth, sheight;
    504513#endif
    505        
    506         if (!PyArg_ParseTuple(args, "|i", &resid))
    507                 return NULL;
    508         olddialog = curdialog;
    509         curdialog = NULL;
    510 
    511         if ( resid != -1 ) {
    512                 curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
    513                 if ( curdialog ) {
    514                         theWindow = GetDialogWindow(curdialog);
    515                         thePort = GetWindowPort(theWindow);
     514
     515    if (!PyArg_ParseTuple(args, "|i", &resid))
     516        return NULL;
     517    olddialog = curdialog;
     518    curdialog = NULL;
     519
     520    if ( resid != -1 ) {
     521        curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
     522        if ( curdialog ) {
     523            theWindow = GetDialogWindow(curdialog);
     524            thePort = GetWindowPort(theWindow);
    516525#if 0
    517                         width = thePort->portRect.right - thePort->portRect.left;
    518                         height = thePort->portRect.bottom - thePort->portRect.top;
    519                         swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
    520                         sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight();
    521                         xpos = (swidth-width)/2;
    522                         ypos = (sheight-height)/5 + LMGetMBarHeight();
    523                         MoveWindow(theWindow, xpos, ypos, 0);
    524                         ShowWindow(theWindow);
     526            width = thePort->portRect.right - thePort->portRect.left;
     527            height = thePort->portRect.bottom - thePort->portRect.top;
     528            swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
     529            sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight();
     530            xpos = (swidth-width)/2;
     531            ypos = (sheight-height)/5 + LMGetMBarHeight();
     532            MoveWindow(theWindow, xpos, ypos, 0);
     533            ShowWindow(theWindow);
    525534#endif
    526                         DrawDialog(curdialog);
    527                 }
    528         }
    529         if (olddialog)
    530                 DisposeDialog(olddialog);
    531         Py_INCREF(Py_None);
    532         return Py_None;
     535            DrawDialog(curdialog);
     536        }
     537    }
     538    if (olddialog)
     539        DisposeDialog(olddialog);
     540    Py_INCREF(Py_None);
     541    return Py_None;
    533542}
    534543
     
    538547MacOS_DebugStr(PyObject *self, PyObject *args)
    539548{
    540         Str255 message;
    541         PyObject *object = 0;
    542        
    543         if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
    544                 return NULL;
    545        
    546         DebugStr(message);
    547         Py_INCREF(Py_None);
    548         return Py_None;
     549    Str255 message;
     550    PyObject *object = 0;
     551
     552    if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
     553        return NULL;
     554
     555    DebugStr(message);
     556    Py_INCREF(Py_None);
     557    return Py_None;
    549558}
    550559
     
    555564MacOS_SysBeep(PyObject *self, PyObject *args)
    556565{
    557         int duration = 6;
    558        
    559         if (!PyArg_ParseTuple(args, "|i", &duration))
    560                 return NULL;
    561         SysBeep(duration);
    562         Py_INCREF(Py_None);
    563         return Py_None;
     566    int duration = 6;
     567
     568    if (!PyArg_ParseTuple(args, "|i", &duration))
     569        return NULL;
     570    SysBeep(duration);
     571    Py_INCREF(Py_None);
     572    return Py_None;
    564573}
    565574
    566575#endif /* __LP64__ */
    567576
    568 static char WMAvailable_doc[] = 
    569         "True if this process can interact with the display."
    570         "Will foreground the application on the first call as a side-effect."
    571         ;
     577static char WMAvailable_doc[] =
     578    "True if this process can interact with the display."
     579    "Will foreground the application on the first call as a side-effect."
     580    ;
    572581
    573582static PyObject *
    574583MacOS_WMAvailable(PyObject *self, PyObject *args)
    575584{
    576         static PyObject *rv = NULL;
    577        
    578         if (!PyArg_ParseTuple(args, ""))
    579                 return NULL;
    580         if (!rv) {
    581                 ProcessSerialNumber psn;
    582                
    583                 /*
    584                 ** This is a fairly innocuous call to make if we don't have a window
    585                 ** manager, or if we have no permission to talk to it. It will print
    586                 ** a message on stderr, but at least it won't abort the process.
    587                 ** It appears the function caches the result itself, and it's cheap, so
    588                 ** no need for us to cache.
    589                 */
     585    static PyObject *rv = NULL;
     586
     587    if (!PyArg_ParseTuple(args, ""))
     588        return NULL;
     589    if (!rv) {
     590        ProcessSerialNumber psn;
     591
     592        /*
     593        ** This is a fairly innocuous call to make if we don't have a window
     594        ** manager, or if we have no permission to talk to it. It will print
     595        ** a message on stderr, but at least it won't abort the process.
     596        ** It appears the function caches the result itself, and it's cheap, so
     597        ** no need for us to cache.
     598        */
    590599#ifdef kCGNullDirectDisplay
    591                 /* On 10.1 CGMainDisplayID() isn't available, and
    592                 ** kCGNullDirectDisplay isn't defined.
    593                 */
    594                 if (CGMainDisplayID() == 0) {
    595                         rv = Py_False;
    596                 } else {
     600        /* On 10.1 CGMainDisplayID() isn't available, and
     601        ** kCGNullDirectDisplay isn't defined.
     602        */
     603        if (CGMainDisplayID() == 0) {
     604            rv = Py_False;
     605        } else {
    597606#else
    598                 {
     607        {
    599608#endif
    600                         if (GetCurrentProcess(&psn) < 0 ||
    601                                 SetFrontProcess(&psn) < 0) {
    602                                 rv = Py_False;
    603                         } else {
    604                                 rv = Py_True;
    605                         }
    606                 }
    607         }
    608         Py_INCREF(rv);
    609         return rv;
     609            if (GetCurrentProcess(&psn) < 0 ||
     610                SetFrontProcess(&psn) < 0) {
     611                rv = Py_False;
     612            } else {
     613                rv = Py_True;
     614            }
     615        }
     616    }
     617    Py_INCREF(rv);
     618    return rv;
    610619}
    611620
     
    615624MacOS_GetTicks(PyObject *self, PyObject *args)
    616625{
    617         return Py_BuildValue("i", (int)TickCount());
     626    return Py_BuildValue("i", (int)TickCount());
    618627}
    619628
     
    623632MacOS_openrf(PyObject *self, PyObject *args)
    624633{
    625         OSErr err;
    626         char *mode = "r";
    627         FSRef ref;
    628         SInt8 permission = fsRdPerm;
    629         rfobject *fp;
    630         HFSUniStr255 name;
    631                
    632         if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSRef, &ref, &mode))
    633                 return NULL;
    634         while (*mode) {
    635                 switch (*mode++) {
    636                 case '*': break;
    637                 case 'r': permission = fsRdPerm; break;
    638                 case 'w': permission = fsWrPerm; break;
    639                 case 'b': break;
    640                 default:
    641                         PyErr_BadArgument();
    642                         return NULL;
    643                 }
    644         }
    645 
    646         err = FSGetResourceForkName(&name);
    647         if (err != noErr) {
    648                 PyMac_Error(err);
    649                 return NULL;
    650         }
    651        
    652         if ( (fp = newrfobject()) == NULL )
    653                 return NULL;
    654 
    655        
    656         err = FSOpenFork(&ref, name.length, name.unicode, permission, &fp->fRefNum);
    657         if (err != noErr) {
    658                 Py_DECREF(fp);
    659                 PyMac_Error(err);
    660                 return NULL;
    661         }
    662         fp->isclosed = 0;
    663         return (PyObject *)fp;
     634    OSErr err;
     635    char *mode = "r";
     636    FSRef ref;
     637    SInt8 permission = fsRdPerm;
     638    rfobject *fp;
     639    HFSUniStr255 name;
     640
     641    if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSRef, &ref, &mode))
     642        return NULL;
     643    while (*mode) {
     644        switch (*mode++) {
     645        case '*': break;
     646        case 'r': permission = fsRdPerm; break;
     647        case 'w': permission = fsWrPerm; break;
     648        case 'b': break;
     649        default:
     650            PyErr_BadArgument();
     651            return NULL;
     652        }
     653    }
     654
     655    err = FSGetResourceForkName(&name);
     656    if (err != noErr) {
     657        PyMac_Error(err);
     658        return NULL;
     659    }
     660
     661    if ( (fp = newrfobject()) == NULL )
     662        return NULL;
     663
     664
     665    err = FSOpenFork(&ref, name.length, name.unicode, permission, &fp->fRefNum);
     666    if (err != noErr) {
     667        Py_DECREF(fp);
     668        PyMac_Error(err);
     669        return NULL;
     670    }
     671    fp->isclosed = 0;
     672    return (PyObject *)fp;
    664673}
    665674
     
    667676
    668677static PyMethodDef MacOS_Methods[] = {
    669         {"GetCreatorAndType",           MacOS_GetCreatorAndType, 1,     getcrtp_doc},
    670         {"SetCreatorAndType",           MacOS_SetCreatorAndType, 1,     setcrtp_doc},
    671         {"GetErrorString",              MacOS_GetErrorString,   1,      geterr_doc},
    672         {"openrf",                      MacOS_openrf,           1,      openrf_doc},
     678    {"GetCreatorAndType",               MacOS_GetCreatorAndType, 1,     getcrtp_doc},
     679    {"SetCreatorAndType",               MacOS_SetCreatorAndType, 1,     setcrtp_doc},
     680    {"GetErrorString",                  MacOS_GetErrorString,   1,      geterr_doc},
     681    {"openrf",                          MacOS_openrf,           1,      openrf_doc},
    673682#ifndef __LP64__
    674         {"splash",                      MacOS_splash,           1,      splash_doc},
    675         {"DebugStr",                    MacOS_DebugStr,         1,      DebugStr_doc},
    676         {"SysBeep",                     MacOS_SysBeep,          1,      SysBeep_doc},
     683    {"splash",                          MacOS_splash,           1,      splash_doc},
     684    {"DebugStr",                        MacOS_DebugStr,         1,      DebugStr_doc},
     685    {"SysBeep",                         MacOS_SysBeep,          1,      SysBeep_doc},
    677686#endif /* __LP64__ */
    678         {"GetTicks",                    MacOS_GetTicks,         1,      GetTicks_doc},
    679         {"WMAvailable",                 MacOS_WMAvailable,              1,      WMAvailable_doc},
    680         {NULL,                          NULL}            /* Sentinel */
     687    {"GetTicks",                        MacOS_GetTicks,         1,      GetTicks_doc},
     688    {"WMAvailable",                     MacOS_WMAvailable,              1,      WMAvailable_doc},
     689    {NULL,                              NULL}            /* Sentinel */
    681690};
    682691
     
    685694initMacOS(void)
    686695{
    687         PyObject *m, *d;
    688        
    689         if (PyErr_WarnPy3k("In 3.x, MacOS is removed.", 1))
    690                 return;
    691        
    692         m = Py_InitModule("MacOS", MacOS_Methods);
    693         d = PyModule_GetDict(m);
    694        
    695         /* Initialize MacOS.Error exception */
    696         MacOS_Error = PyMac_GetOSErrException();
    697         if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
    698                 return;
    699         Rftype.ob_type = &PyType_Type;
    700         Py_INCREF(&Rftype);
    701         if (PyDict_SetItemString(d, "ResourceForkType", (PyObject *)&Rftype) != 0)
    702                 return;
    703         /*
    704         ** This is a hack: the following constant added to the id() of a string
    705         ** object gives you the address of the data. Unfortunately, it is needed for
    706         ** some of the image and sound processing interfaces on the mac:-(
    707         */
    708         {
    709                 PyStringObject *p = 0;
    710                 long off = (long)&(p->ob_sval[0]);
    711                
    712                 if( PyDict_SetItemString(d, "string_id_to_buffer", Py_BuildValue("i", off)) != 0)
    713                         return;
    714         }
     696    PyObject *m, *d;
     697
     698    if (PyErr_WarnPy3k("In 3.x, the MacOS module is removed.", 1))
     699        return;
     700
     701    m = Py_InitModule("MacOS", MacOS_Methods);
     702    d = PyModule_GetDict(m);
     703
     704    /* Initialize MacOS.Error exception */
     705    MacOS_Error = PyMac_GetOSErrException();
     706    if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
     707        return;
     708    Rftype.ob_type = &PyType_Type;
     709    Py_INCREF(&Rftype);
     710    if (PyDict_SetItemString(d, "ResourceForkType", (PyObject *)&Rftype) != 0)
     711        return;
     712    /*
     713    ** This is a hack: the following constant added to the id() of a string
     714    ** object gives you the address of the data. Unfortunately, it is needed for
     715    ** some of the image and sound processing interfaces on the mac:-(
     716    */
     717    {
     718        PyStringObject *p = 0;
     719        long off = (long)&(p->ob_sval[0]);
     720
     721        if( PyDict_SetItemString(d, "string_id_to_buffer", Py_BuildValue("i", off)) != 0)
     722            return;
     723    }
    715724#define PY_RUNTIMEMODEL "macho"
    716         if (PyDict_SetItemString(d, "runtimemodel",
    717                                 Py_BuildValue("s", PY_RUNTIMEMODEL)) != 0)
    718                 return;
     725    if (PyDict_SetItemString(d, "runtimemodel",
     726                            Py_BuildValue("s", PY_RUNTIMEMODEL)) != 0)
     727        return;
    719728#if defined(WITH_NEXT_FRAMEWORK)
    720729#define PY_LINKMODEL "framework"
     
    724733#define PY_LINKMODEL "static"
    725734#endif
    726         if (PyDict_SetItemString(d, "linkmodel",
    727                                 Py_BuildValue("s", PY_LINKMODEL)) != 0)
    728                 return;
    729 
    730 }
     735    if (PyDict_SetItemString(d, "linkmodel",
     736                            Py_BuildValue("s", PY_LINKMODEL)) != 0)
     737        return;
     738
     739}
Note: See TracChangeset for help on using the changeset viewer.