1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
# This script generates a Python interface for an Apple Macintosh Manager.
# It uses the "bgen" package to generate C code.
# The function specifications are generated by scanning the mamager's header file,
# using the "scantools" package (customized for this particular manager).
#
# XXXX TO DO:
# - Implement correct missing FSSpec handling for Alias methods
# - Implement FInfo
import string
# Declarations that change for each manager
#MACHEADERFILE = 'Files.h' # The Apple header file
MODNAME = '_File' # The name of the module
LONGMODNAME = 'Carbon.File' # The "normal" external name of the module
# The following is *usually* unchanged but may still require tuning
MODPREFIX = 'File' # The prefix for module-wide routines
INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
from macsupport import *
# Various integers:
SInt64 = Type("SInt64", "L")
UInt64 = Type("UInt64", "L")
FNMessage = Type("FNMessage", "l")
FSAllocationFlags = Type("FSAllocationFlags", "H")
FSCatalogInfoBitmap = Type("FSCatalogInfoBitmap", "l")
FSIteratorFlags = Type("FSIteratorFlags", "l")
FSVolumeRefNum = Type("FSVolumeRefNum", "h")
AliasInfoType = Type("AliasInfoType", "h")
# Various types of strings:
#class UniCharCountBuffer(InputOnlyType):
# pass
class VarReverseInputBufferType(ReverseInputBufferMixin, VarInputBufferType):
pass
FullPathName = VarReverseInputBufferType()
ConstStr31Param = OpaqueArrayType("Str31", "PyMac_BuildStr255", "PyMac_GetStr255")
ConstStr32Param = OpaqueArrayType("Str32", "PyMac_BuildStr255", "PyMac_GetStr255")
ConstStr63Param = OpaqueArrayType("Str63", "PyMac_BuildStr255", "PyMac_GetStr255")
Str63 = OpaqueArrayType("Str63", "PyMac_BuildStr255", "PyMac_GetStr255")
HFSUniStr255 = OpaqueType("HFSUniStr255", "PyMac_BuildHFSUniStr255", "PyMac_GetHFSUniStr255")
UInt8_ptr = InputOnlyType("UInt8 *", "s")
# Other types:
class OptionalFSxxxType(OpaqueByValueType):
def declare(self, name):
Output("%s %s__buf__;", self.typeName, name)
Output("%s *%s = &%s__buf__;", self.typeName, name, name)
class FSCatalogInfoAndBitmapType(InputOnlyType):
def __init__(self):
InputOnlyType.__init__(self, "BUG", "BUG")
def declare(self, name):
Output("PyObject *%s__object = NULL;", name)
Output("FSCatalogInfoBitmap %s__bitmap = 0;", name)
Output("FSCatalogInfo %s;", name)
def getargsFormat(self):
return "lO"
def getargsArgs(self, name):
return "%s__bitmap, %s__object"%(name, name)
def getargsCheck(self, name):
Output("if (!convert_FSCatalogInfo(%s__object, %s__bitmap, &%s)) return NULL;", name, name, name)
def passInput(self, name):
return "%s__bitmap, &%s"% (name, name)
def passOutput(self, name):
return "%s__bitmap, &%s"% (name, name)
def mkvalueFormat(self):
return "O"
def mkvalueArgs(self, name):
return "%s__object" % (name)
def xxxxmkvalueCheck(self, name):
Output("if ((%s__object = new_FSCatalogInfo(%s__bitmap, &%s)) == NULL) return NULL;", name, name)
class FSCatalogInfoAndBitmap_inType(FSCatalogInfoAndBitmapType, InputOnlyMixIn):
def xxxxmkvalueCheck(self, name):
pass
class FSCatalogInfoAndBitmap_outType(FSCatalogInfoAndBitmapType):
def getargsFormat(self):
return "l"
def getargsArgs(self, name):
return "%s__bitmap" % name
def getargsCheck(self, name):
pass
FInfo = OpaqueType("FInfo", "FInfo")
FInfo_ptr = OpaqueType("FInfo", "FInfo")
AliasHandle = OpaqueByValueType("AliasHandle", "Alias")
FSSpec = OpaqueType("FSSpec", "FSSpec")
FSSpec_ptr = OpaqueType("FSSpec", "FSSpec")
OptFSSpecPtr = OptionalFSxxxType("FSSpec", "BUG", "myPyMac_GetOptFSSpecPtr")
FSRef = OpaqueType("FSRef", "FSRef")
FSRef_ptr = OpaqueType("FSRef", "FSRef")
OptFSRefPtr = OptionalFSxxxType("FSRef", "BUG", "myPyMac_GetOptFSRefPtr")
FSCatalogInfo = OpaqueType("FSCatalogInfo", "FSCatalogInfo")
FSCatalogInfo_ptr = OpaqueType("FSCatalogInfo", "FSCatalogInfo")
# To be done:
#CatPositionRec
#FSCatalogInfo
#FSForkInfo
#FSIterator
#FSVolumeInfo
#FSSpecArrayPtr
includestuff = includestuff + """
#ifdef WITHOUT_FRAMEWORKS
#include <Files.h>
#else
#include <Carbon/Carbon.h>
#endif
#ifdef USE_TOOLBOX_OBJECT_GLUE
extern int _PyMac_GetFSSpec(PyObject *v, FSSpec *spec);
extern int _PyMac_GetFSRef(PyObject *v, FSRef *fsr);
extern PyObject *_PyMac_BuildFSSpec(FSSpec *spec);
extern PyObject *_PyMac_BuildFSRef(FSRef *spec);
#define PyMac_GetFSSpec _PyMac_GetFSSpec
#define PyMac_GetFSRef _PyMac_GetFSRef
#define PyMac_BuildFSSpec _PyMac_BuildFSSpec
#define PyMac_BuildFSRef _PyMac_BuildFSRef
#else
extern int PyMac_GetFSSpec(PyObject *v, FSSpec *spec);
extern int PyMac_GetFSRef(PyObject *v, FSRef *fsr);
extern PyObject *PyMac_BuildFSSpec(FSSpec *spec);
extern PyObject *PyMac_BuildFSRef(FSRef *spec);
#endif
/* Forward declarations */
static PyObject *FInfo_New(FInfo *itself);
static PyObject *FSRef_New(FSRef *itself);
static PyObject *FSSpec_New(FSSpec *itself);
static PyObject *Alias_New(AliasHandle itself);
static int FInfo_Convert(PyObject *v, FInfo *p_itself);
#define FSRef_Convert PyMac_GetFSRef
#define FSSpec_Convert PyMac_GetFSSpec
static int Alias_Convert(PyObject *v, AliasHandle *p_itself);
/*
** UTCDateTime records
*/
static int
UTCDateTime_Convert(PyObject *v, UTCDateTime *ptr)
{
return PyArg_Parse(v, "(HlH)", &ptr->highSeconds, &ptr->lowSeconds, &ptr->fraction);
}
static PyObject *
UTCDateTime_New(UTCDateTime *ptr)
{
return Py_BuildValue("(HlH)", ptr->highSeconds, ptr->lowSeconds, ptr->fraction);
}
/*
** Optional fsspec and fsref pointers. None will pass NULL
*/
static int
myPyMac_GetOptFSSpecPtr(PyObject *v, FSSpec **spec)
{
if (v == Py_None) {
*spec = NULL;
return 1;
}
return PyMac_GetFSSpec(v, *spec);
}
static int
myPyMac_GetOptFSRefPtr(PyObject *v, FSRef **ref)
{
if (v == Py_None) {
*ref = NULL;
return 1;
}
return PyMac_GetFSRef(v, *ref);
}
/*
** Parse/generate objsect
*/
static PyObject *
PyMac_BuildHFSUniStr255(HFSUniStr255 *itself)
{
return Py_BuildValue("u#", itself->unicode, itself->length);
}
"""
finalstuff = finalstuff + """
int
PyMac_GetFSSpec(PyObject *v, FSSpec *spec)
{
Str255 path;
short refnum;
long parid;
OSErr err;
FSRef fsr;
if (FSSpec_Check(v)) {
*spec = ((FSSpecObject *)v)->ob_itself;
return 1;
}
if (PyArg_Parse(v, "(hlO&)",
&refnum, &parid, PyMac_GetStr255, &path)) {
err = FSMakeFSSpec(refnum, parid, path, spec);
if ( err && err != fnfErr ) {
PyMac_Error(err);
return 0;
}
return 1;
}
PyErr_Clear();
#if !TARGET_API_MAC_OSX
/* On OS9 we now try a pathname */
if ( PyString_Check(v) ) {
/* It's a pathname */
if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
return 0;
refnum = 0; /* XXXX Should get CurWD here?? */
parid = 0;
err = FSMakeFSSpec(refnum, parid, path, spec);
if ( err && err != fnfErr ) {
PyMac_Error(err);
return 0;
}
return 1;
}
PyErr_Clear();
#endif
/* Otherwise we try to go via an FSRef. On OSX we go all the way,
** on OS9 we accept only a real FSRef object
*/
#if TARGET_API_MAC_OSX
if ( PyMac_GetFSRef(v, &fsr) ) {
#else
if (FSRef_Check(v)) {
fsr = ((FSRefObject *)v)->ob_itself;
#endif
err = FSGetCatalogInfo(&fsr, kFSCatInfoNone, NULL, NULL, spec, NULL);
if (err != noErr) {
PyMac_Error(err);
return 0;
}
return 1;
}
#if !TARGET_API_MAC_OSX
PyErr_SetString(PyExc_TypeError, "FSSpec, FSRef, pathname or (refnum, parid, path) required");
#endif
return 0;
}
int
PyMac_GetFSRef(PyObject *v, FSRef *fsr)
{
OSStatus err;
FSSpec fss;
if (FSRef_Check(v)) {
*fsr = ((FSRefObject *)v)->ob_itself;
return 1;
}
#if TARGET_API_MAC_OSX
/* On OSX we now try a pathname */
if ( PyString_Check(v) || PyUnicode_Check(v)) {
char *path = NULL;
if (!PyArg_Parse(v, "et", Py_FileSystemDefaultEncoding, &path))
return NULL;
if ( (err=FSPathMakeRef(path, fsr, NULL)) ) {
PyMac_Error(err);
return 0;
}
return 1;
}
/* XXXX Should try unicode here too */
#endif
/* Otherwise we try to go via an FSSpec */
#if TARGET_API_MAC_OSX
if (FSSpec_Check(v)) {
fss = ((FSSpecObject *)v)->ob_itself;
#else
if (PyMac_GetFSSpec(v, &fss)) {
#endif
if ((err=FSpMakeFSRef(&fss, fsr)) == 0)
return 1;
PyMac_Error(err);
return 0;
}
PyErr_SetString(PyExc_TypeError, "FSRef, FSSpec or pathname required");
return 0;
}
extern PyObject *
PyMac_BuildFSSpec(FSSpec *spec)
{
return FSSpec_New(spec);
}
extern PyObject *
PyMac_BuildFSRef(FSRef *spec)
{
return FSRef_New(spec);
}
"""
initstuff = initstuff + """
PyMac_INIT_TOOLBOX_OBJECT_NEW(FSSpec *, PyMac_BuildFSSpec);
PyMac_INIT_TOOLBOX_OBJECT_NEW(FSRef *, PyMac_BuildFSRef);
PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec);
PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSRef, PyMac_GetFSRef);
"""
execfile(string.lower(MODPREFIX) + 'typetest.py')
# Our object types:
class FSCatalogInfoDefinition(PEP253Mixin, ObjectDefinition):
getsetlist = [
("nodeFlags",
"return Py_BuildValue(\"H\", self->ob_itself.nodeFlags);",
"return PyArg_Parse(v, \"H\", &self->ob_itself.nodeFlags)-1;",
None
),
("volume",
"return Py_BuildValue(\"h\", self->ob_itself.volume);",
"return PyArg_Parse(v, \"h\", &self->ob_itself.volume)-1;",
None
),
("parentDirID",
"return Py_BuildValue(\"l\", self->ob_itself.parentDirID);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.parentDirID)-1;",
None
),
("nodeID",
"return Py_BuildValue(\"l\", self->ob_itself.nodeID);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.nodeID)-1;",
None
),
("createDate",
"return Py_BuildValue(\"O&\", UTCDateTime_New, &self->ob_itself.createDate);",
"return PyArg_Parse(v, \"O&\", UTCDateTime_Convert, &self->ob_itself.createDate)-1;",
None
),
("contentModDate",
"return Py_BuildValue(\"O&\", UTCDateTime_New, &self->ob_itself.contentModDate);",
"return PyArg_Parse(v, \"O&\", UTCDateTime_Convert, &self->ob_itself.contentModDate)-1;",
None
),
("attributeModDate",
"return Py_BuildValue(\"O&\", UTCDateTime_New, &self->ob_itself.attributeModDate);",
"return PyArg_Parse(v, \"O&\", UTCDateTime_Convert, &self->ob_itself.attributeModDate)-1;",
None
),
("accessDate",
"return Py_BuildValue(\"O&\", UTCDateTime_New, &self->ob_itself.accessDate);",
"return PyArg_Parse(v, \"O&\", UTCDateTime_Convert, &self->ob_itself.accessDate)-1;",
None
),
("backupDate",
"return Py_BuildValue(\"O&\", UTCDateTime_New, &self->ob_itself.backupDate);",
"return PyArg_Parse(v, \"O&\", UTCDateTime_Convert, &self->ob_itself.backupDate)-1;",
None
),
("permissions",
"return Py_BuildValue(\"(llll)\", self->ob_itself.permissions[0], self->ob_itself.permissions[1], self->ob_itself.permissions[2], self->ob_itself.permissions[3]);",
"return PyArg_Parse(v, \"(llll)\", &self->ob_itself.permissions[0], &self->ob_itself.permissions[1], &self->ob_itself.permissions[2], &self->ob_itself.permissions[3])-1;",
None
),
# XXXX FinderInfo TBD
# XXXX FinderXInfo TBD
("valence",
"return Py_BuildValue(\"l\", self->ob_itself.valence);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.valence)-1;",
None
),
("dataLogicalSize",
"return Py_BuildValue(\"l\", self->ob_itself.dataLogicalSize);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.dataLogicalSize)-1;",
None
),
("dataPhysicalSize",
"return Py_BuildValue(\"l\", self->ob_itself.dataPhysicalSize);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.dataPhysicalSize)-1;",
None
),
("rsrcLogicalSize",
"return Py_BuildValue(\"l\", self->ob_itself.rsrcLogicalSize);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.rsrcLogicalSize)-1;",
None
),
("rsrcPhysicalSize",
"return Py_BuildValue(\"l\", self->ob_itself.rsrcPhysicalSize);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.rsrcPhysicalSize)-1;",
None
),
("sharingFlags",
"return Py_BuildValue(\"l\", self->ob_itself.sharingFlags);",
"return PyArg_Parse(v, \"l\", &self->ob_itself.sharingFlags)-1;",
None
),
("userPrivileges",
"return Py_BuildValue(\"b\", self->ob_itself.userPrivileges);",
"return PyArg_Parse(v, \"b\", &self->ob_itself.userPrivileges)-1;",
None
),
]
# The same info, but in a different form
INITFORMAT = "HhllO&O&O&O&O&llllllb"
INITARGS = """&((FSCatalogInfoObject *)self)->ob_itself.nodeFlags,
&((FSCatalogInfoObject *)self)->ob_itself.volume,
&((FSCatalogInfoObject *)self)->ob_itself.parentDirID,
&((FSCatalogInfoObject *)self)->ob_itself.nodeID,
UTCDateTime_Convert, &((FSCatalogInfoObject *)self)->ob_itself.createDate,
UTCDateTime_Convert, &((FSCatalogInfoObject *)self)->ob_itself.contentModDate,
UTCDateTime_Convert, &((FSCatalogInfoObject *)self)->ob_itself.attributeModDate,
UTCDateTime_Convert, &((FSCatalogInfoObject *)self)->ob_itself.accessDate,
UTCDateTime_Convert, &((FSCatalogInfoObject *)self)->ob_itself.backupDate,
&((FSCatalogInfoObject *)self)->ob_itself.valence,
&((FSCatalogInfoObject *)self)->ob_itself.dataLogicalSize,
&((FSCatalogInfoObject *)self)->ob_itself.dataPhysicalSize,
&((FSCatalogInfoObject *)self)->ob_itself.rsrcLogicalSize,
&((FSCatalogInfoObject *)self)->ob_itself.rsrcPhysicalSize,
&((FSCatalogInfoObject *)self)->ob_itself.sharingFlags,
&((FSCatalogInfoObject *)self)->ob_itself.userPrivileges"""
INITNAMES = """
"nodeFlags",
"volume",
"parentDirID",
"nodeID",
"createDate",
"contentModDate",
"atributeModDate",
"accessDate",
"backupDate",
"valence",
"dataLogicalSize",
"dataPhysicalSize",
"rsrcLogicalSize",
"rsrcPhysicalSize",
"sharingFlags",
"userPrivileges"
"""
def __init__(self, name, prefix, itselftype):
ObjectDefinition.__init__(self, name, prefix, itselftype)
self.argref = "*" # Store FSSpecs, but pass them by address
def outputCheckNewArg(self):
Output("if (itself == NULL) return Py_None;")
def output_tp_newBody(self):
Output("PyObject *self;");
Output()
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("memset(&((%s *)self)->ob_itself, 0, sizeof(%s));",
self.objecttype, self.itselftype)
Output("return self;")
def output_tp_initBody(self):
Output("static char *kw[] = {%s, 0};", self.INITNAMES)
Output()
Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|%s\", kw, %s))",
self.INITFORMAT, self.INITARGS)
OutLbrace()
Output("return -1;")
OutRbrace()
Output("return 0;")
class FInfoDefinition(PEP253Mixin, ObjectDefinition):
getsetlist = [
("Type",
"return Py_BuildValue(\"O&\", PyMac_BuildOSType, self->ob_itself.fdType);",
"return PyArg_Parse(v, \"O&\", PyMac_GetOSType, &self->ob_itself.fdType)-1;",
"4-char file type"
),
("Creator",
"return Py_BuildValue(\"O&\", PyMac_BuildOSType, self->ob_itself.fdCreator);",
"return PyArg_Parse(v, \"O&\", PyMac_GetOSType, &self->ob_itself.fdCreator)-1;",
"4-char file creator"
),
("Flags",
"return Py_BuildValue(\"H\", self->ob_itself.fdFlags);",
"return PyArg_Parse(v, \"H\", &self->ob_itself.fdFlags)-1;",
"Finder flag bits"
),
("Location",
"return Py_BuildValue(\"O&\", PyMac_BuildPoint, self->ob_itself.fdLocation);",
"return PyArg_Parse(v, \"O&\", PyMac_GetPoint, &self->ob_itself.fdLocation)-1;",
"(x, y) location of the file's icon in its parent finder window"
),
("Fldr",
"return Py_BuildValue(\"h\", self->ob_itself.fdFldr);",
"return PyArg_Parse(v, \"h\", &self->ob_itself.fdFldr)-1;",
"Original folder, for 'put away'"
),
]
def __init__(self, name, prefix, itselftype):
ObjectDefinition.__init__(self, name, prefix, itselftype)
self.argref = "*" # Store FSSpecs, but pass them by address
def outputCheckNewArg(self):
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
def output_tp_newBody(self):
Output("PyObject *self;");
Output()
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("memset(&((%s *)self)->ob_itself, 0, sizeof(%s));",
self.objecttype, self.itselftype)
Output("return self;")
def output_tp_initBody(self):
Output("%s *itself = NULL;", self.itselftype)
Output("static char *kw[] = {\"itself\", 0};")
Output()
Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"|O&\", kw, FInfo_Convert, &itself))")
OutLbrace()
Output("if (itself) memcpy(&((%s *)self)->ob_itself, itself, sizeof(%s));",
self.objecttype, self.itselftype)
Output("return 0;")
OutRbrace()
Output("return -1;")
class FSSpecDefinition(PEP253Mixin, ObjectDefinition):
getsetlist = [
("data",
"return PyString_FromStringAndSize((char *)&self->ob_itself, sizeof(self->ob_itself));",
None,
"Raw data of the FSSpec object"
)
]
def __init__(self, name, prefix, itselftype):
ObjectDefinition.__init__(self, name, prefix, itselftype)
self.argref = "*" # Store FSSpecs, but pass them by address
def outputCheckNewArg(self):
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
# We do Convert ourselves (with PyMac_GetFSxxx)
def outputConvert(self):
pass
def output_tp_newBody(self):
Output("PyObject *self;");
Output()
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("memset(&((%s *)self)->ob_itself, 0, sizeof(%s));",
self.objecttype, self.itselftype)
Output("return self;")
def output_tp_initBody(self):
Output("PyObject *v = NULL;")
Output("char *rawdata = NULL;")
Output("int rawdatalen = 0;")
Output("static char *kw[] = {\"itself\", \"rawdata\", 0};")
Output()
Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|Os#\", kw, &v, &rawdata, &rawdatalen))")
Output("return -1;")
Output("if (v && rawdata)")
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"Only one of itself or rawdata may be specified\");")
Output("return -1;")
OutRbrace()
Output("if (!v && !rawdata)")
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"One of itself or rawdata must be specified\");")
Output("return -1;")
OutRbrace()
Output("if (rawdata)")
OutLbrace()
Output("if (rawdatalen != sizeof(%s))", self.itselftype)
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"%s rawdata incorrect size\");",
self.itselftype)
Output("return -1;")
OutRbrace()
Output("memcpy(&((%s *)self)->ob_itself, rawdata, rawdatalen);", self.objecttype)
Output("return 0;")
OutRbrace()
Output("if (PyMac_GetFSSpec(v, &((%s *)self)->ob_itself)) return 0;", self.objecttype)
Output("return -1;")
def outputRepr(self):
Output()
Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
OutLbrace()
Output("char buf[512];")
Output("""PyOS_snprintf(buf, sizeof(buf), \"%%s((%%d, %%ld, '%%.*s'))\",
self->ob_type->tp_name,
self->ob_itself.vRefNum,
self->ob_itself.parID,
self->ob_itself.name[0], self->ob_itself.name+1);""")
Output("return PyString_FromString(buf);")
OutRbrace()
class FSRefDefinition(PEP253Mixin, ObjectDefinition):
getsetlist = [
("data",
"return PyString_FromStringAndSize((char *)&self->ob_itself, sizeof(self->ob_itself));",
None,
"Raw data of the FSRef object"
)
]
def __init__(self, name, prefix, itselftype):
ObjectDefinition.__init__(self, name, prefix, itselftype)
self.argref = "*" # Store FSRefs, but pass them by address
def outputCheckNewArg(self):
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
# We do Convert ourselves (with PyMac_GetFSxxx)
def outputConvert(self):
pass
def output_tp_newBody(self):
Output("PyObject *self;");
Output()
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("memset(&((%s *)self)->ob_itself, 0, sizeof(%s));",
self.objecttype, self.itselftype)
Output("return self;")
def output_tp_initBody(self):
Output("PyObject *v = NULL;")
Output("char *rawdata = NULL;")
Output("int rawdatalen = 0;")
Output("static char *kw[] = {\"itself\", \"rawdata\", 0};")
Output()
Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|Os#\", kw, &v, &rawdata, &rawdatalen))")
Output("return -1;")
Output("if (v && rawdata)")
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"Only one of itself or rawdata may be specified\");")
Output("return -1;")
OutRbrace()
Output("if (!v && !rawdata)")
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"One of itself or rawdata must be specified\");")
Output("return -1;")
OutRbrace()
Output("if (rawdata)")
OutLbrace()
Output("if (rawdatalen != sizeof(%s))", self.itselftype)
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"%s rawdata incorrect size\");",
self.itselftype)
Output("return -1;")
OutRbrace()
Output("memcpy(&((%s *)self)->ob_itself, rawdata, rawdatalen);", self.objecttype)
Output("return 0;")
OutRbrace()
Output("if (PyMac_GetFSRef(v, &((%s *)self)->ob_itself)) return 0;", self.objecttype)
Output("return -1;")
class AliasDefinition(PEP253Mixin, ObjectDefinition):
# XXXX Should inherit from resource?
getsetlist = [
("data",
"""int size;
PyObject *rv;
size = GetHandleSize((Handle)self->ob_itself);
HLock((Handle)self->ob_itself);
rv = PyString_FromStringAndSize(*(Handle)self->ob_itself, size);
HUnlock((Handle)self->ob_itself);
return rv;
""",
None,
"Raw data of the alias object"
)
]
def outputCheckNewArg(self):
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
def outputStructMembers(self):
ObjectDefinition.outputStructMembers(self)
Output("void (*ob_freeit)(%s ptr);", self.itselftype)
def outputInitStructMembers(self):
ObjectDefinition.outputInitStructMembers(self)
Output("it->ob_freeit = NULL;")
def outputCleanupStructMembers(self):
Output("if (self->ob_freeit && self->ob_itself)")
OutLbrace()
Output("self->ob_freeit(self->ob_itself);")
OutRbrace()
Output("self->ob_itself = NULL;")
def output_tp_newBody(self):
Output("PyObject *self;");
Output()
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
Output("return self;")
def output_tp_initBody(self):
Output("%s itself = NULL;", self.itselftype)
Output("char *rawdata = NULL;")
Output("int rawdatalen = 0;")
Output("Handle h;")
Output("static char *kw[] = {\"itself\", \"rawdata\", 0};")
Output()
Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|O&s#\", kw, %s_Convert, &itself, &rawdata, &rawdatalen))",
self.prefix)
Output("return -1;")
Output("if (itself && rawdata)")
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"Only one of itself or rawdata may be specified\");")
Output("return -1;")
OutRbrace()
Output("if (!itself && !rawdata)")
OutLbrace()
Output("PyErr_SetString(PyExc_TypeError, \"One of itself or rawdata must be specified\");")
Output("return -1;")
OutRbrace()
Output("if (rawdata)")
OutLbrace()
Output("if ((h = NewHandle(rawdatalen)) == NULL)")
OutLbrace()
Output("PyErr_NoMemory();")
Output("return -1;")
OutRbrace()
Output("HLock(h);")
Output("memcpy((char *)*h, rawdata, rawdatalen);")
Output("HUnlock(h);")
Output("((%s *)self)->ob_itself = (%s)h;", self.objecttype, self.itselftype)
Output("return 0;")
OutRbrace()
Output("((%s *)self)->ob_itself = itself;", self.objecttype)
Output("return 0;")
# Alias methods come in two flavors: those with the alias as arg1 and
# those with the alias as arg 2.
class Arg2MethodGenerator(OSErrMethodGenerator):
"""Similar to MethodGenerator, but has self as second argument"""
def parseArgumentList(self, args):
args0, arg1, argsrest = args[:1], args[1], args[2:]
t0, n0, m0 = arg1
args = args0 + argsrest
if m0 != InMode:
raise ValueError, "method's 'self' must be 'InMode'"
self.itself = Variable(t0, "_self->ob_itself", SelfMode)
FunctionGenerator.parseArgumentList(self, args)
self.argumentList.insert(2, self.itself)
# From here on it's basically all boiler plate...
# Create the generator groups and link them
module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff,
longname=LONGMODNAME)
fscataloginfoobject = FSCatalogInfoDefinition('FSCatalogInfo', 'FSCatalogInfo', 'FSCatalogInfo')
finfoobject = FInfoDefinition('FInfo', 'FInfo', 'FInfo')
aliasobject = AliasDefinition('Alias', 'Alias', 'AliasHandle')
fsspecobject = FSSpecDefinition('FSSpec', 'FSSpec', 'FSSpec')
fsrefobject = FSRefDefinition('FSRef', 'FSRef', 'FSRef')
module.addobject(fscataloginfoobject)
module.addobject(finfoobject)
module.addobject(aliasobject)
module.addobject(fsspecobject)
module.addobject(fsrefobject)
# Create the generator classes used to populate the lists
Function = OSErrFunctionGenerator
Method = OSErrMethodGenerator
# Create and populate the lists
functions = []
alias_methods = []
fsref_methods = []
fsspec_methods = []
execfile(INPUTFILE)
# Manual generators:
FSRefMakePath_body = """
OSStatus _err;
#define MAXPATHNAME 1024
UInt8 path[MAXPATHNAME];
UInt32 maxPathSize = MAXPATHNAME;
if (!PyArg_ParseTuple(_args, ""))
return NULL;
_err = FSRefMakePath(&_self->ob_itself,
path,
maxPathSize);
if (_err != noErr) return PyMac_Error(_err);
_res = Py_BuildValue("s", path);
return _res;
"""
f = ManualGenerator("FSRefMakePath", FSRefMakePath_body)
f.docstring = lambda: "() -> string"
fsref_methods.append(f)
FSRef_as_pathname_body = """
#if TARGET_API_MAC_OSX
if (!PyArg_ParseTuple(_args, ""))
return NULL;
_res = FSRef_FSRefMakePath(_self, _args);
#else
char strbuf[1024];
OSErr err;
FSSpec fss;
if (!PyArg_ParseTuple(_args, ""))
return NULL;
if ( !PyMac_GetFSSpec((PyObject *)_self, &fss))
return NULL;
err = PyMac_GetFullPathname(&fss, strbuf, sizeof(strbuf));
if ( err ) {
PyMac_Error(err);
return NULL;
}
_res = PyString_FromString(strbuf);
#endif
return _res;
"""
f = ManualGenerator("as_pathname", FSRef_as_pathname_body)
f.docstring = lambda: "() -> string"
fsref_methods.append(f)
FSSpec_as_pathname_body = """
char strbuf[1024];
OSErr err;
if (!PyArg_ParseTuple(_args, ""))
return NULL;
err = PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
if ( err ) {
PyMac_Error(err);
return NULL;
}
_res = PyString_FromString(strbuf);
return _res;
"""
f = ManualGenerator("as_pathname", FSSpec_as_pathname_body)
f.docstring = lambda: "() -> string"
fsspec_methods.append(f)
FSSpec_as_tuple_body = """
if (!PyArg_ParseTuple(_args, ""))
return NULL;
_res = Py_BuildValue("(iis#)", _self->ob_itself.vRefNum, _self->ob_itself.parID,
&_self->ob_itself.name[1], _self->ob_itself.name[0]);
return _res;
"""
f = ManualGenerator("as_tuple", FSSpec_as_tuple_body)
f.docstring = lambda: "() -> (vRefNum, dirID, name)"
fsspec_methods.append(f)
pathname_body = """
PyObject *obj;
if (!PyArg_ParseTuple(_args, "O", &obj))
return NULL;
if (PyString_Check(obj)) {
Py_INCREF(obj);
return obj;
}
if (PyUnicode_Check(obj))
return PyUnicode_AsEncodedString(obj, "utf8", "strict");
_res = PyObject_CallMethod(obj, "as_pathname", NULL);
return _res;
"""
f = ManualGenerator("pathname", pathname_body)
f.docstring = lambda: "(str|unicode|FSSpec|FSref) -> pathname"
functions.append(f)
# add the populated lists to the generator groups
# (in a different wordl the scan program would generate this)
for f in functions: module.add(f)
for f in alias_methods: aliasobject.add(f)
for f in fsspec_methods: fsspecobject.add(f)
for f in fsref_methods: fsrefobject.add(f)
# generate output (open the output file as late as possible)
SetOutputFileName(OUTPUTFILE)
module.generate()
|