~pythonregexp2.7/python/issue2636

« back to all changes in this revision

Viewing changes to Mac/Modules/MacOS.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-06-09 14:36:32 UTC
  • mfrom: (39021.1.402 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080609143632-wwwkx92u1t5l7yd3
Merged in changes from the latest python source snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <Carbon/Carbon.h>
31
31
#include <ApplicationServices/ApplicationServices.h>
32
32
 
 
33
#ifndef HAVE_MACOS105_SDK
 
34
typedef SInt16  FSIORefNum;
 
35
#endif
 
36
 
33
37
static PyObject *MacOS_Error; /* Exception MacOS.Error */
34
38
 
35
39
#define PATHNAMELEN 1024
40
44
 
41
45
typedef struct {
42
46
        PyObject_HEAD
43
 
        short fRefNum;
 
47
        FSIORefNum fRefNum;
44
48
        int isclosed;
45
49
} rfobject;
46
50
 
54
58
do_close(rfobject *self)
55
59
{
56
60
        if (self->isclosed ) return;
57
 
        (void)FSClose(self->fRefNum);
 
61
        (void)FSCloseFork(self->fRefNum);
58
62
        self->isclosed = 1;
59
63
}
60
64
 
68
72
        long n;
69
73
        PyObject *v;
70
74
        OSErr err;
 
75
        ByteCount n2;
71
76
        
72
77
        if (self->isclosed) {
73
78
                PyErr_SetString(PyExc_ValueError, "Operation on closed file");
77
82
        if (!PyArg_ParseTuple(args, "l", &n))
78
83
                return NULL;
79
84
                
80
 
        v = PyString_FromStringAndSize((char *)NULL, n);
 
85
        v = PyBytes_FromStringAndSize((char *)NULL, n);
81
86
        if (v == NULL)
82
87
                return NULL;
83
88
                
84
 
        err = FSRead(self->fRefNum, &n, PyString_AsString(v));
 
89
        err = FSReadFork(self->fRefNum, fsAtMark, 0, n, PyString_AsString(v), &n2);
85
90
        if (err && err != eofErr) {
86
91
                PyMac_Error(err);
87
92
                Py_DECREF(v);
88
93
                return NULL;
89
94
        }
90
 
        _PyString_Resize(&v, n);
 
95
        _PyString_Resize(&v, n2);
91
96
        return v;
92
97
}
93
98
 
109
114
        }
110
115
        if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
111
116
                return NULL;
112
 
        err = FSWrite(self->fRefNum, &size, buffer);
 
117
        err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL);
113
118
        if (err) {
114
119
                PyMac_Error(err);
115
120
                return NULL;
126
131
static PyObject *
127
132
rf_seek(rfobject *self, PyObject *args)
128
133
{
129
 
        long amount, pos;
 
134
        long amount;
130
135
        int whence = SEEK_SET;
131
 
        long eof;
 
136
        int mode;
132
137
        OSErr err;
133
138
        
134
139
        if (self->isclosed) {
135
140
                PyErr_SetString(PyExc_ValueError, "Operation on closed file");
136
141
                return NULL;
137
142
        }
138
 
        if (!PyArg_ParseTuple(args, "l|i", &amount, &whence))
 
143
        if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) {
139
144
                return NULL;
140
 
        
141
 
        if ((err = GetEOF(self->fRefNum, &eof)))
142
 
                goto ioerr;
 
145
        }
143
146
        
144
147
        switch (whence) {
145
148
        case SEEK_CUR:
146
 
                if ((err = GetFPos(self->fRefNum, &pos)))
147
 
                        goto ioerr; 
 
149
                mode = fsFromMark;
148
150
                break;
149
151
        case SEEK_END:
150
 
                pos = eof;
 
152
                mode = fsFromLEOF;
151
153
                break;
152
154
        case SEEK_SET:
153
 
                pos = 0;
 
155
                mode = fsFromStart;
154
156
                break;
155
157
        default:
156
158
                PyErr_BadArgument();
157
159
                return NULL;
158
160
        }
159
 
        
160
 
        pos += amount;
161
 
        
162
 
        /* Don't bother implementing seek past EOF */
163
 
        if (pos > eof || pos < 0) {
164
 
                PyErr_BadArgument();
165
 
                return NULL;
166
 
        }
167
 
        
168
 
        if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) {
169
 
ioerr:
 
161
 
 
162
        err = FSSetForkPosition(self->fRefNum, mode, amount);
 
163
        if (err != noErr) {
170
164
                PyMac_Error(err);
171
165
                return NULL;
172
166
        }
182
176
static PyObject *
183
177
rf_tell(rfobject *self, PyObject *args)
184
178
{
185
 
        long where;
 
179
        long long where;
186
180
        OSErr err;
187
181
        
188
182
        if (self->isclosed) {
191
185
        }
192
186
        if (!PyArg_ParseTuple(args, ""))
193
187
                return NULL;
194
 
        if ((err = GetFPos(self->fRefNum, &where)) ) {
 
188
 
 
189
        err = FSGetForkPosition(self->fRefNum, &where);
 
190
        if (err != noErr) {
195
191
                PyMac_Error(err);
196
192
                return NULL;
197
193
        }
198
 
        return PyInt_FromLong(where);
 
194
        return PyLong_FromLongLong(where);
199
195
}
200
196
 
201
197
static char rf_close__doc__[] = 
281
277
        Rftype__doc__ /* Documentation string */
282
278
};
283
279
 
 
280
 
284
281
/* End of code for Resource fork objects */
285
282
/* -------------------------------------------------------- */
286
283
 
292
289
static PyObject *
293
290
MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
294
291
{
295
 
        FSSpec fss;
296
 
        FInfo info;
297
292
        PyObject *creator, *type, *res;
298
293
        OSErr err;
299
 
        
300
 
        if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
301
 
                return NULL;
302
 
        if ((err = FSpGetFInfo(&fss, &info)) != noErr)
303
 
                return PyErr_Mac(MacOS_Error, err);
304
 
        creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
305
 
        type = PyString_FromStringAndSize((char *)&info.fdType, 4);
 
294
        FSRef ref;
 
295
        FSCatalogInfo   cataloginfo;
 
296
        FileInfo* finfo;
 
297
 
 
298
        if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) {
 
299
#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
 
306
347
        res = Py_BuildValue("OO", creator, type);
307
348
        Py_DECREF(creator);
308
349
        Py_DECREF(type);
314
355
static PyObject *
315
356
MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
316
357
{
317
 
        FSSpec fss;
318
358
        ResType creator, type;
319
 
        FInfo info;
 
359
        FSRef ref;
 
360
        FileInfo* finfo;
320
361
        OSErr err;
321
 
        
 
362
        FSCatalogInfo   cataloginfo;
 
363
 
322
364
        if (!PyArg_ParseTuple(args, "O&O&O&",
 
365
                        PyMac_GetFSRef, &ref, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) {
 
366
#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&",
323
372
                        PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
324
 
                return NULL;
325
 
        if ((err = FSpGetFInfo(&fss, &info)) != noErr)
326
 
                return PyErr_Mac(MacOS_Error, err);
327
 
        info.fdCreator = creator;
328
 
        info.fdType = type;
329
 
        if ((err = FSpSetFInfo(&fss, &info)) != noErr)
330
 
                return PyErr_Mac(MacOS_Error, err);
 
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;
 
385
#else /* __LP64__ */
 
386
                return NULL;
 
387
#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
 
331
418
        Py_INCREF(Py_None);
332
419
        return Py_None;
333
420
}
399
486
        return Py_BuildValue("s", buf);
400
487
}
401
488
 
 
489
 
 
490
#ifndef __LP64__
 
491
 
402
492
static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)";
403
493
 
404
494
static PyObject *
417
507
                return NULL;
418
508
        olddialog = curdialog;
419
509
        curdialog = NULL;
420
 
                
 
510
 
421
511
        if ( resid != -1 ) {
422
512
                curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
423
513
                if ( curdialog ) {
452
542
        
453
543
        if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
454
544
                return NULL;
 
545
        
455
546
        DebugStr(message);
456
547
        Py_INCREF(Py_None);
457
548
        return Py_None;
458
549
}
459
550
 
 
551
 
460
552
static char SysBeep_doc[] = "BEEEEEP!!!";
461
553
 
462
554
static PyObject *
471
563
        return Py_None;
472
564
}
473
565
 
 
566
#endif /* __LP64__ */
 
567
 
474
568
static char WMAvailable_doc[] = 
475
569
        "True if this process can interact with the display."
476
570
        "Will foreground the application on the first call as a side-effect."
530
624
{
531
625
        OSErr err;
532
626
        char *mode = "r";
533
 
        FSSpec fss;
534
 
        SignedByte permission = 1;
 
627
        FSRef ref;
 
628
        SInt8 permission = fsRdPerm;
535
629
        rfobject *fp;
 
630
        HFSUniStr255 name;
536
631
                
537
 
        if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSSpec, &fss, &mode))
 
632
        if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSRef, &ref, &mode))
538
633
                return NULL;
539
634
        while (*mode) {
540
635
                switch (*mode++) {
541
636
                case '*': break;
542
 
                case 'r': permission = 1; break;
543
 
                case 'w': permission = 2; break;
 
637
                case 'r': permission = fsRdPerm; break;
 
638
                case 'w': permission = fsWrPerm; break;
544
639
                case 'b': break;
545
640
                default:
546
641
                        PyErr_BadArgument();
547
642
                        return NULL;
548
643
                }
549
644
        }
 
645
 
 
646
        err = FSGetResourceForkName(&name);
 
647
        if (err != noErr) {
 
648
                PyMac_Error(err);
 
649
                return NULL;
 
650
        }
550
651
        
551
652
        if ( (fp = newrfobject()) == NULL )
552
653
                return NULL;
553
 
                
554
 
        err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
 
654
 
555
655
        
556
 
        if ( err == fnfErr ) {
557
 
                /* In stead of doing complicated things here to get creator/type
558
 
                ** correct we let the standard i/o library handle it
559
 
                */
560
 
                FILE *tfp;
561
 
                char pathname[PATHNAMELEN];
562
 
                
563
 
                if ( (err=PyMac_GetFullPathname(&fss, pathname, PATHNAMELEN)) ) {
564
 
                        PyMac_Error(err);
565
 
                        Py_DECREF(fp);
566
 
                        return NULL;
567
 
                }
568
 
                
569
 
                if ( (tfp = fopen(pathname, "w")) == NULL ) {
570
 
                        PyMac_Error(fnfErr); /* What else... */
571
 
                        Py_DECREF(fp);
572
 
                        return NULL;
573
 
                }
574
 
                fclose(tfp);
575
 
                err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
576
 
        }
577
 
        if ( err ) {
 
656
        err = FSOpenFork(&ref, name.length, name.unicode, permission, &fp->fRefNum);
 
657
        if (err != noErr) {
578
658
                Py_DECREF(fp);
579
659
                PyMac_Error(err);
580
660
                return NULL;
584
664
}
585
665
 
586
666
 
 
667
 
587
668
static PyMethodDef MacOS_Methods[] = {
588
669
        {"GetCreatorAndType",           MacOS_GetCreatorAndType, 1,     getcrtp_doc},
589
670
        {"SetCreatorAndType",           MacOS_SetCreatorAndType, 1,     setcrtp_doc},
590
671
        {"GetErrorString",              MacOS_GetErrorString,   1,      geterr_doc},
591
672
        {"openrf",                      MacOS_openrf,           1,      openrf_doc},
 
673
#ifndef __LP64__
592
674
        {"splash",                      MacOS_splash,           1,      splash_doc},
593
675
        {"DebugStr",                    MacOS_DebugStr,         1,      DebugStr_doc},
 
676
        {"SysBeep",                     MacOS_SysBeep,          1,      SysBeep_doc},
 
677
#endif /* __LP64__ */
594
678
        {"GetTicks",                    MacOS_GetTicks,         1,      GetTicks_doc},
595
 
        {"SysBeep",                     MacOS_SysBeep,          1,      SysBeep_doc},
596
679
        {"WMAvailable",                 MacOS_WMAvailable,              1,      WMAvailable_doc},
597
680
        {NULL,                          NULL}            /* Sentinel */
598
681
};