Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7a94e4

Browse files
committed
SF patch# 1761465 by Jeffrey Yasskin.
Fix test_aepack and test_applesingle.
1 parent 67feb09 commit f7a94e4

7 files changed

Lines changed: 127 additions & 105 deletions

File tree

Lib/plat-mac/Carbon/AppleEvents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated from 'AEDataModel.h'
22

3-
def FOUR_CHAR_CODE(x): return x
3+
def FOUR_CHAR_CODE(x): return bytes(x)
44
typeBoolean = FOUR_CHAR_CODE('bool')
55
typeChar = FOUR_CHAR_CODE('TEXT')
66
typeSInt16 = FOUR_CHAR_CODE('shor')

Lib/plat-mac/aepack.py

Lines changed: 88 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,36 @@
2525
# These ones seem to be missing from AppleEvents
2626
# (they're in AERegistry.h)
2727

28-
#typeColorTable = 'clrt'
29-
#typeDrawingArea = 'cdrw'
30-
#typePixelMap = 'cpix'
31-
#typePixelMapMinus = 'tpmm'
32-
#typeRotation = 'trot'
33-
#typeTextStyles = 'tsty'
34-
#typeStyledText = 'STXT'
35-
#typeAEText = 'tTXT'
36-
#typeEnumeration = 'enum'
37-
28+
#typeColorTable = b'clrt'
29+
#typeDrawingArea = b'cdrw'
30+
#typePixelMap = b'cpix'
31+
#typePixelMapMinus = b'tpmm'
32+
#typeRotation = b'trot'
33+
#typeTextStyles = b'tsty'
34+
#typeStyledText = b'STXT'
35+
#typeAEText = b'tTXT'
36+
#typeEnumeration = b'enum'
37+
38+
def b2i(byte_string):
39+
result = 0
40+
for byte in byte_string:
41+
result <<= 8
42+
result += byte
43+
return result
3844
#
3945
# Some AE types are immedeately coerced into something
4046
# we like better (and which is equivalent)
4147
#
4248
unpacker_coercions = {
43-
typeComp : typeFloat,
44-
typeColorTable : typeAEList,
45-
typeDrawingArea : typeAERecord,
46-
typeFixed : typeFloat,
47-
typeExtended : typeFloat,
48-
typePixelMap : typeAERecord,
49-
typeRotation : typeAERecord,
50-
typeStyledText : typeAERecord,
51-
typeTextStyles : typeAERecord,
49+
b2i(typeComp) : typeFloat,
50+
b2i(typeColorTable) : typeAEList,
51+
b2i(typeDrawingArea) : typeAERecord,
52+
b2i(typeFixed) : typeFloat,
53+
b2i(typeExtended) : typeFloat,
54+
b2i(typePixelMap) : typeAERecord,
55+
b2i(typeRotation) : typeAERecord,
56+
b2i(typeStyledText) : typeAERecord,
57+
b2i(typeTextStyles) : typeAERecord,
5258
};
5359

5460
#
@@ -72,33 +78,35 @@ def pack(x, forcetype = None):
7278
"""Pack a python object into an AE descriptor"""
7379

7480
if forcetype:
75-
if isinstance(x, str):
81+
if isinstance(x, bytes):
7682
return AE.AECreateDesc(forcetype, x)
7783
else:
7884
return pack(x).AECoerceDesc(forcetype)
7985

8086
if x == None:
81-
return AE.AECreateDesc('null', '')
87+
return AE.AECreateDesc(b'null', '')
8288

8389
if isinstance(x, AEDescType):
8490
return x
8591
if isinstance(x, FSSType):
86-
return AE.AECreateDesc('fss ', x.data)
92+
return AE.AECreateDesc(b'fss ', x.data)
8793
if isinstance(x, FSRefType):
88-
return AE.AECreateDesc('fsrf', x.data)
94+
return AE.AECreateDesc(b'fsrf', x.data)
8995
if isinstance(x, AliasType):
90-
return AE.AECreateDesc('alis', x.data)
96+
return AE.AECreateDesc(b'alis', x.data)
9197
if isinstance(x, int):
92-
return AE.AECreateDesc('long', struct.pack('l', x))
98+
return AE.AECreateDesc(b'long', struct.pack('l', x))
9399
if isinstance(x, float):
94-
return AE.AECreateDesc('doub', struct.pack('d', x))
100+
return AE.AECreateDesc(b'doub', struct.pack('d', x))
101+
if isinstance(x, (bytes, str8)):
102+
return AE.AECreateDesc(b'TEXT', x)
95103
if isinstance(x, str):
96-
return AE.AECreateDesc('TEXT', x)
97-
if isinstance(x, unicode):
104+
# See http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/doc/constant_group/typeUnicodeText
105+
# for the possible encodings.
98106
data = x.encode('utf16')
99107
if data[:2] == '\xfe\xff':
100108
data = data[2:]
101-
return AE.AECreateDesc('utxt', data)
109+
return AE.AECreateDesc(b'utxt', data)
102110
if isinstance(x, list):
103111
lst = AE.AECreateList('', 0)
104112
for item in x:
@@ -112,37 +120,37 @@ def pack(x, forcetype = None):
112120
return record
113121
if isinstance(x, type) and issubclass(x, ObjectSpecifier):
114122
# Note: we are getting a class object here, not an instance
115-
return AE.AECreateDesc('type', x.want)
123+
return AE.AECreateDesc(b'type', x.want)
116124
if hasattr(x, '__aepack__'):
117125
return x.__aepack__()
118126
if hasattr(x, 'which'):
119-
return AE.AECreateDesc('TEXT', x.which)
127+
return AE.AECreateDesc(b'TEXT', x.which)
120128
if hasattr(x, 'want'):
121-
return AE.AECreateDesc('TEXT', x.want)
122-
return AE.AECreateDesc('TEXT', repr(x)) # Copout
129+
return AE.AECreateDesc(b'TEXT', x.want)
130+
return AE.AECreateDesc(b'TEXT', repr(x)) # Copout
123131

124132
def unpack(desc, formodulename=""):
125133
"""Unpack an AE descriptor to a python object"""
126134
t = desc.type
127135

128-
if t in unpacker_coercions:
129-
desc = desc.AECoerceDesc(unpacker_coercions[t])
136+
if b2i(t) in unpacker_coercions:
137+
desc = desc.AECoerceDesc(unpacker_coercions[b2i(t)])
130138
t = desc.type # This is a guess by Jack....
131139

132140
if t == typeAEList:
133141
l = []
134142
for i in range(desc.AECountItems()):
135-
keyword, item = desc.AEGetNthDesc(i+1, '****')
143+
keyword, item = desc.AEGetNthDesc(i+1, b'****')
136144
l.append(unpack(item, formodulename))
137145
return l
138146
if t == typeAERecord:
139147
d = {}
140148
for i in range(desc.AECountItems()):
141-
keyword, item = desc.AEGetNthDesc(i+1, '****')
142-
d[keyword] = unpack(item, formodulename)
149+
keyword, item = desc.AEGetNthDesc(i+1, b'****')
150+
d[b2i(keyword)] = unpack(item, formodulename)
143151
return d
144152
if t == typeAEText:
145-
record = desc.AECoerceDesc('reco')
153+
record = desc.AECoerceDesc(b'reco')
146154
return mkaetext(unpack(record, formodulename))
147155
if t == typeAlias:
148156
return Carbon.File.Alias(rawdata=desc.data)
@@ -170,7 +178,7 @@ def unpack(desc, formodulename=""):
170178
if t == typeFSRef:
171179
return Carbon.File.FSRef(rawdata=desc.data)
172180
if t == typeInsertionLoc:
173-
record = desc.AECoerceDesc('reco')
181+
record = desc.AECoerceDesc(b'reco')
174182
return mkinsertionloc(unpack(record, formodulename))
175183
# typeInteger equal to typeLongInteger
176184
if t == typeIntlText:
@@ -194,7 +202,7 @@ def unpack(desc, formodulename=""):
194202
v = 0x100000000 + v
195203
return v
196204
if t == typeObjectSpecifier:
197-
record = desc.AECoerceDesc('reco')
205+
record = desc.AECoerceDesc(b'reco')
198206
# If we have been told the name of the module we are unpacking aedescs for,
199207
# we can attempt to create the right type of python object from that module.
200208
if formodulename:
@@ -234,14 +242,14 @@ def unpack(desc, formodulename=""):
234242
#
235243
# The following are special
236244
#
237-
if t == 'rang':
238-
record = desc.AECoerceDesc('reco')
245+
if t == b'rang':
246+
record = desc.AECoerceDesc(b'reco')
239247
return mkrange(unpack(record, formodulename))
240-
if t == 'cmpd':
241-
record = desc.AECoerceDesc('reco')
248+
if t == b'cmpd':
249+
record = desc.AECoerceDesc(b'reco')
242250
return mkcomparison(unpack(record, formodulename))
243-
if t == 'logi':
244-
record = desc.AECoerceDesc('reco')
251+
if t == b'logi':
252+
record = desc.AECoerceDesc(b'reco')
245253
return mklogical(unpack(record, formodulename))
246254
return mkunknown(desc.type, desc.data)
247255

@@ -297,39 +305,44 @@ def mkkeyword(keyword):
297305
return aetypes.Keyword(keyword)
298306

299307
def mkrange(dict):
300-
return aetypes.Range(dict['star'], dict['stop'])
308+
return aetypes.Range(dict[b2i(b'star')], dict[b2i(b'stop')])
301309

302310
def mkcomparison(dict):
303-
return aetypes.Comparison(dict['obj1'], dict['relo'].enum, dict['obj2'])
311+
return aetypes.Comparison(dict[b2i(b'obj1')],
312+
dict[b2i(b'relo')].enum,
313+
dict[b2i(b'obj2')])
304314

305315
def mklogical(dict):
306-
return aetypes.Logical(dict['logc'], dict['term'])
316+
return aetypes.Logical(dict[b2i(b'logc')], dict[b2i(b'term')])
307317

308318
def mkstyledtext(dict):
309-
return aetypes.StyledText(dict['ksty'], dict['ktxt'])
319+
return aetypes.StyledText(dict[b2i(b'ksty')], dict[b2i(b'ktxt')])
310320

311321
def mkaetext(dict):
312-
return aetypes.AEText(dict[keyAEScriptTag], dict[keyAEStyles], dict[keyAEText])
322+
return aetypes.AEText(dict[b2i(keyAEScriptTag)],
323+
dict[b2i(keyAEStyles)],
324+
dict[b2i(keyAEText)])
313325

314326
def mkinsertionloc(dict):
315-
return aetypes.InsertionLoc(dict[keyAEObject], dict[keyAEPosition])
327+
return aetypes.InsertionLoc(dict[b2i(keyAEObject)],
328+
dict[b2i(keyAEPosition)])
316329

317330
def mkobject(dict):
318-
want = dict['want'].type
319-
form = dict['form'].enum
320-
seld = dict['seld']
321-
fr = dict['from']
322-
if form in ('name', 'indx', 'rang', 'test'):
323-
if want == 'text': return aetypes.Text(seld, fr)
324-
if want == 'cha ': return aetypes.Character(seld, fr)
325-
if want == 'cwor': return aetypes.Word(seld, fr)
326-
if want == 'clin': return aetypes.Line(seld, fr)
327-
if want == 'cpar': return aetypes.Paragraph(seld, fr)
328-
if want == 'cwin': return aetypes.Window(seld, fr)
329-
if want == 'docu': return aetypes.Document(seld, fr)
330-
if want == 'file': return aetypes.File(seld, fr)
331-
if want == 'cins': return aetypes.InsertionPoint(seld, fr)
332-
if want == 'prop' and form == 'prop' and aetypes.IsType(seld):
331+
want = dict[b2i(b'want')].type
332+
form = dict[b2i(b'form')].enum
333+
seld = dict[b2i(b'seld')]
334+
fr = dict[b2i(b'from')]
335+
if form in (b'name', b'indx', b'rang', b'test'):
336+
if want == b'text': return aetypes.Text(seld, fr)
337+
if want == b'cha ': return aetypes.Character(seld, fr)
338+
if want == b'cwor': return aetypes.Word(seld, fr)
339+
if want == b'clin': return aetypes.Line(seld, fr)
340+
if want == b'cpar': return aetypes.Paragraph(seld, fr)
341+
if want == b'cwin': return aetypes.Window(seld, fr)
342+
if want == b'docu': return aetypes.Document(seld, fr)
343+
if want == b'file': return aetypes.File(seld, fr)
344+
if want == b'cins': return aetypes.InsertionPoint(seld, fr)
345+
if want == b'prop' and form == b'prop' and aetypes.IsType(seld):
333346
return aetypes.Property(seld.type, fr)
334347
return aetypes.ObjectSpecifier(want, form, seld, fr)
335348

@@ -338,14 +351,15 @@ def mkobject(dict):
338351
# to __class__ is safe. Moreover, shouldn't there be a better
339352
# initializer for the classes in the suites?
340353
def mkobjectfrommodule(dict, modulename):
341-
if isinstance(dict['want'], type) and issubclass(dict['want'], ObjectSpecifier):
354+
if (isinstance(dict[b2i(b'want')], type) and
355+
issubclass(dict[b2i(b'want')], ObjectSpecifier)):
342356
# The type has already been converted to Python. Convert back:-(
343-
classtype = dict['want']
344-
dict['want'] = aetypes.mktype(classtype.want)
345-
want = dict['want'].type
357+
classtype = dict[b2i(b'want')]
358+
dict[b2i(b'want')] = aetypes.mktype(classtype.want)
359+
want = dict[b2i(b'want')].type
346360
module = __import__(modulename)
347361
codenamemapper = module._classdeclarations
348-
classtype = codenamemapper.get(want, None)
362+
classtype = codenamemapper.get(b2i(want), None)
349363
newobj = mkobject(dict)
350364
if classtype:
351365
assert issubclass(classtype, ObjectSpecifier)
@@ -356,7 +370,7 @@ def mktype(typecode, modulename=None):
356370
if modulename:
357371
module = __import__(modulename)
358372
codenamemapper = module._classdeclarations
359-
classtype = codenamemapper.get(typecode, None)
373+
classtype = codenamemapper.get(b2i(typecode), None)
360374
if classtype:
361375
return classtype
362376
return aetypes.mktype(typecode)

Lib/plat-mac/aetypes.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def nice(s):
1616
if isinstance(s, str): return repr(s)
1717
else: return str(s)
1818

19+
def _four_char_code(four_chars):
20+
"""Convert a str or bytes object into a 4-byte array.
21+
22+
four_chars must contain only ASCII characters.
23+
24+
"""
25+
return bytes("%-4.4s" % str(four_chars))
26+
1927
class Unknown:
2028
"""An uninterpreted AE object"""
2129

@@ -33,13 +41,13 @@ class Enum:
3341
"""An AE enumeration value"""
3442

3543
def __init__(self, enum):
36-
self.enum = "%-4.4s" % str(enum)
44+
self.enum = _four_char_code(enum)
3745

3846
def __repr__(self):
3947
return "Enum(%r)" % (self.enum,)
4048

4149
def __str__(self):
42-
return self.enum.strip()
50+
return self.enum.strip(b' ')
4351

4452
def __aepack__(self):
4553
return pack(self.enum, typeEnumeration)
@@ -100,7 +108,7 @@ class Type:
100108
"""An AE 4-char typename object"""
101109

102110
def __init__(self, type):
103-
self.type = "%-4.4s" % str(type)
111+
self.type = _four_char_code(type)
104112

105113
def __repr__(self):
106114
return "Type(%r)" % (self.type,)
@@ -123,7 +131,7 @@ class Keyword:
123131
"""An AE 4-char keyword object"""
124132

125133
def __init__(self, keyword):
126-
self.keyword = "%-4.4s" % str(keyword)
134+
self.keyword = _four_char_code(keyword)
127135

128136
def __repr__(self):
129137
return "Keyword(%r)" % self.keyword
@@ -161,7 +169,7 @@ class Comparison:
161169

162170
def __init__(self, obj1, relo, obj2):
163171
self.obj1 = obj1
164-
self.relo = "%-4.4s" % str(relo)
172+
self.relo = _four_char_code(relo)
165173
self.obj2 = obj2
166174

167175
def __repr__(self):
@@ -190,7 +198,7 @@ class Ordinal:
190198

191199
def __init__(self, abso):
192200
# self.obj1 = obj1
193-
self.abso = "%-4.4s" % str(abso)
201+
self.abso = _four_char_code(abso)
194202

195203
def __repr__(self):
196204
return "Ordinal(%r)" % (self.abso,)
@@ -214,7 +222,7 @@ class Logical:
214222
"""An AE logical expression object"""
215223

216224
def __init__(self, logc, term):
217-
self.logc = "%-4.4s" % str(logc)
225+
self.logc = _four_char_code(logc)
218226
self.term = term
219227

220228
def __repr__(self):
@@ -554,12 +562,12 @@ def __str__(self):
554562
class %s(ComponentItem): want = '%s'
555563
"""
556564

557-
exec(template % ("Text", 'text'))
558-
exec(template % ("Character", 'cha '))
559-
exec(template % ("Word", 'cwor'))
560-
exec(template % ("Line", 'clin'))
561-
exec(template % ("paragraph", 'cpar'))
562-
exec(template % ("Window", 'cwin'))
563-
exec(template % ("Document", 'docu'))
564-
exec(template % ("File", 'file'))
565-
exec(template % ("InsertionPoint", 'cins'))
565+
exec(template % ("Text", b'text'))
566+
exec(template % ("Character", b'cha '))
567+
exec(template % ("Word", b'cwor'))
568+
exec(template % ("Line", b'clin'))
569+
exec(template % ("paragraph", b'cpar'))
570+
exec(template % ("Window", b'cwin'))
571+
exec(template % ("Document", b'docu'))
572+
exec(template % ("File", b'file'))
573+
exec(template % ("InsertionPoint", b'cins'))

0 commit comments

Comments
 (0)