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

Skip to content

Commit cd219d5

Browse files
committed
For reasons unknown these files were never checked in to CVS.
1 parent 8368453 commit cd219d5

3 files changed

Lines changed: 213 additions & 0 deletions

File tree

Mac/Demo/calldll/readme

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Preliminary notes/documentation for the calldll module, version 0.2.
2+
====================================================================
3+
4+
Calldll allows you to call random C functions from python without writing any
5+
C code. It is mainly meant to call MacOS toolbox routines for which no Python
6+
wrapper module is available. It is also incomplete, in that only a few argument
7+
types are currently supported. Please let me know which other argument types
8+
you need, and/or whether you have any ideas on a general "escape" allowing people
9+
to pass anything.
10+
11+
The module works *only* on PowerPC currently. It is distributed complete
12+
with source and project files, so that people willing to look at a CFM68K port
13+
are welcome to do so. A classic 68K implementation is impossible, I think (so
14+
prove me wrong, please:-).
15+
16+
The module exports three functions:
17+
- symtable = getlibrary(libraryname)
18+
Get a reference to import library libraryname. "InterfaceLib" is the most commonly
19+
used one, containing most toolbox routines. The symbol table can be used
20+
to lookup routines to be passed to newcall: "symtable.WaitNextEvent" will
21+
return the address of routine WaitNextEvent. and so will "symtable['WaitNextEvent']".
22+
The symtable is a mapping, so you can use keys() and len(...) to inspect it.
23+
- symtable = getdiskfragment(file)
24+
Load the specified file (given by fsspec or filename) and return a reference to
25+
its symboltable.
26+
- callable = newcall(routine, returntype, [argtype, ...])
27+
Return a callable object. You specify the C routine to be called (as explained above),
28+
the type of the return value and the argument types. The resulting object can
29+
be called from Python code in the normal way, and typechecking on arguments is
30+
performed (but, of course, if you specify incorrect argument types in this call
31+
you may well crash your machine). Printing a callable will give you a description
32+
of the (C-) calling sequence.
33+
34+
The C return value can be one of 'None', 'Byte', 'Short', 'Long', 'Pstring' (a pascal
35+
string returned by address, copied to a Python string), 'Cobject' (a wrapper around a void
36+
pointer), 'Handle' (a new handle, returned as a Res.Resource object) or 'OSErr' (which raises
37+
MacOS.Error if non-zero).
38+
39+
Arguments can be any of 'InByte', 'InShort', 'InLong', 'InString' (a python string, with the
40+
address of the data passed to the C routine, so be careful!), 'InPstring' (a python string copied
41+
to a Str255 and passed by address), 'InCobject', 'InHandle', 'OutByte' (storage is allocated for
42+
a single byte, the address passed to C and the resulting value returned to Python), 'OutShort',
43+
'OutLong', 'OutPstring' (again: storage pre-allocated and the address passed to C), 'OutCobject'
44+
(storage for a void * is allocated, this void ** is passed to C and the resulting void * is
45+
encapsulated in the Cobject returned) or 'OutHandle' (ditto, which means that this is usually *not*
46+
what you use, you normally use 'InHandle' because most toolbox calls expect you to preallocate
47+
the handle).
48+
49+
All values to be returned (from the return value and the Out arguments) are collected. If there
50+
aren't any None is returned, if there is one value this value is returned, if there are multiple
51+
values a tuple is returned.
52+
53+
There is test code in testcalldll.py, and a minimal example in samplecalldll.py.
54+
55+
Have fun, and let's discuss the design of this thingy on pythonmac-sig,
56+
57+
Jack Jansen, [email protected], 23-May-97.

Mac/Demo/calldll/samplecalldll.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Test calldll. Tell the user how often menus flash, and let her change it.
3+
#
4+
5+
import calldll
6+
import sys
7+
8+
# Obtain a reference to the library with the toolbox calls
9+
interfacelib = calldll.getlibrary('InterfaceLib')
10+
11+
# Get the routines we need (see LowMem.h for details)
12+
LMGetMenuFlash = calldll.newcall(interfacelib.LMGetMenuFlash, 'Short')
13+
LMSetMenuFlash = calldll.newcall(interfacelib.LMSetMenuFlash, 'None', 'InShort')
14+
15+
print "Menus currently flash",LMGetMenuFlash(),"times."
16+
print "How often would you like them to flash?",
17+
18+
# Note: we use input(), so you can try passing non-integer objects
19+
newflash = input()
20+
LMSetMenuFlash(newflash)
21+
22+
print "Okay, menus now flash", LMGetMenuFlash(),"times."
23+
24+
sys.exit(1) # So the window stays on-screen

Mac/Demo/calldll/testcalldll.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import calldll
2+
import macfs
3+
import sys
4+
import MacOS
5+
import Res
6+
7+
fss, ok = macfs.PromptGetFile("Show me calldll.ppc.slb")
8+
9+
lib = calldll.getdiskfragment(fss, 'calldll.ppc.slb')
10+
11+
cdll_b_bbbbbbbb = calldll.newcall(lib.cdll_b_bbbbbbbb, 'Byte', 'InByte', 'InByte',
12+
'InByte', 'InByte','InByte', 'InByte','InByte', 'InByte')
13+
cdll_h_hhhhhhhh = calldll.newcall(lib.cdll_h_hhhhhhhh, 'Short', 'InShort', 'InShort',
14+
'InShort', 'InShort','InShort', 'InShort','InShort', 'InShort')
15+
cdll_l_llllllll = calldll.newcall(lib.cdll_l_llllllll, 'Long', 'InLong', 'InLong',
16+
'InLong', 'InLong','InLong', 'InLong','InLong', 'InLong')
17+
18+
cdll_N_ssssssss = calldll.newcall(lib.cdll_N_ssssssss, 'None', 'InString', 'InString',
19+
'InString', 'InString', 'InString', 'InString', 'InString', 'InString')
20+
21+
cdll_o_l = calldll.newcall(lib.cdll_o_l, 'OSErr', 'InLong')
22+
23+
cdll_N_pp = calldll.newcall(lib.cdll_N_pp, 'None', 'InPstring', 'OutPstring')
24+
25+
cdll_N_bb = calldll.newcall(lib.cdll_N_bb, 'None', 'InByte', 'OutByte')
26+
cdll_N_hh = calldll.newcall(lib.cdll_N_hh, 'None', 'InShort', 'OutShort')
27+
cdll_N_ll = calldll.newcall(lib.cdll_N_ll, 'None', 'InLong', 'OutLong')
28+
cdll_N_sH = calldll.newcall(lib.cdll_N_sH, 'None', 'InString', 'InHandle')
29+
30+
print 'Test cdll_b_bbbbbbbb'
31+
rv = cdll_b_bbbbbbbb(1, 2, 3, 4, 5, 6, 7, 8)
32+
if rv == 36:
33+
print 'ok.'
34+
else:
35+
print 'Failed, returned', rv
36+
37+
print 'Test cdll_b_bbbbbbbb negative'
38+
rv = cdll_b_bbbbbbbb(-1, -2, -3, -4, -5, -6, -7, -8)
39+
if rv == -36:
40+
print 'ok.'
41+
else:
42+
print 'Failed, returned', rv
43+
44+
print 'Test cdll_h_hhhhhhhh'
45+
rv = cdll_h_hhhhhhhh(1, 2, 3, 4, 5, 6, 7, 8)
46+
if rv == 36:
47+
print 'ok.'
48+
else:
49+
print 'Failed, returned', rv
50+
51+
print 'Test cdll_h_hhhhhhhh negative'
52+
rv = cdll_h_hhhhhhhh(-1, -2, -3, -4, -5, -6, -7, -8)
53+
if rv == -36:
54+
print 'ok.'
55+
else:
56+
print 'Failed, returned', rv
57+
58+
print 'Test cdll_l_llllllll'
59+
rv = cdll_l_llllllll(1, 2, 3, 4, 5, 6, 7, 8)
60+
if rv == 36:
61+
print 'ok.'
62+
else:
63+
print 'Failed, returned', rv
64+
65+
print 'Test cdll_l_llllllll negative'
66+
rv = cdll_l_llllllll(-1, -2, -3, -4, -5, -6, -7, -8)
67+
if rv == -36:
68+
print 'ok.'
69+
else:
70+
print 'Failed, returned', rv
71+
72+
print 'Test cdll_N_ssssssss'
73+
print 'Should print one two three four five six seven eight'
74+
rv = cdll_N_ssssssss('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight')
75+
if rv == None:
76+
print 'ok.'
77+
else:
78+
print 'Failed, returned', rv
79+
80+
print 'Test cdll_o_l(0)'
81+
rv = cdll_o_l(0)
82+
if rv == None:
83+
print 'ok.'
84+
else:
85+
print 'Error, returned', rv
86+
87+
print 'Test cdll_o_l(-100)'
88+
try:
89+
rv = cdll_o_l(-100)
90+
print 'Error, did not raise exception, returned', rv
91+
except MacOS.Error, arg:
92+
if arg[0] == -100:
93+
print 'ok.'
94+
else:
95+
print 'Error, returned incorrect exception arg:', arg[0]
96+
97+
print 'Test cdll_N_pp'
98+
rv = cdll_N_pp('pascal string')
99+
if rv == 'Was: pascal string':
100+
print 'ok.'
101+
else:
102+
print 'Failed, returned', `rv`
103+
104+
print 'Test cdll_N_bb'
105+
rv = cdll_N_bb(-100)
106+
if rv == -100:
107+
print 'ok.'
108+
else:
109+
print 'Failed, returned', rv
110+
111+
print 'Test cdll_N_hh'
112+
rv = cdll_N_hh(-100)
113+
if rv == -100:
114+
print 'ok.'
115+
else:
116+
print 'Failed, returned', rv
117+
118+
print 'Test cdll_N_ll'
119+
rv = cdll_N_ll(-100)
120+
if rv == -100:
121+
print 'ok.'
122+
else:
123+
print 'Failed, returned', rv
124+
125+
print 'Test cdll_N_sH'
126+
h = Res.Resource('xyz')
127+
rv = cdll_N_sH('new data', h)
128+
if rv == None and h.data == 'new data':
129+
print 'ok.'
130+
else:
131+
print 'Failed, rv is', rv, 'and handle data is', `rv.data`
132+
sys.exit(1)

0 commit comments

Comments
 (0)