ยปCore Development>Code coverage>Lib/plat-mac/appletrawmain.py

Python code coverage for Lib/plat-mac/appletrawmain.py

#countcontent
1n/a# Emulate sys.argv and run __main__.py or __main__.pyc in an environment that
2n/a# is as close to "normal" as possible.
3n/a#
4n/a# This script is put into __rawmain__.pyc for applets that need argv
5n/a# emulation, by BuildApplet and friends.
6n/a#
7n/a
8n/afrom warnings import warnpy3k
9n/awarnpy3k("In 3.x, the appletrawmain module is removed.", stacklevel=2)
10n/a
11n/aimport argvemulator
12n/aimport os
13n/aimport sys
14n/aimport marshal
15n/a
16n/a#
17n/a# Make sure we have an argv[0], and make _dir point to the Resources
18n/a# directory.
19n/a#
20n/aif not sys.argv or sys.argv[0][:1] == '-':
21n/a # Insert our (guessed) name.
22n/a _dir = os.path.split(sys.executable)[0] # removes "python"
23n/a _dir = os.path.split(_dir)[0] # Removes "MacOS"
24n/a _dir = os.path.join(_dir, 'Resources')
25n/a sys.argv.insert(0, '__rawmain__')
26n/aelse:
27n/a _dir = os.path.split(sys.argv[0])[0]
28n/a#
29n/a# Add the Resources directory to the path. This is where files installed
30n/a# by BuildApplet.py with the --extra option show up, and if those files are
31n/a# modules this sys.path modification is necessary to be able to import them.
32n/a#
33n/asys.path.insert(0, _dir)
34n/a#
35n/a# Create sys.argv
36n/a#
37n/aargvemulator.ArgvCollector().mainloop()
38n/a#
39n/a# Find the real main program to run
40n/a#
41n/a__file__ = os.path.join(_dir, '__main__.py')
42n/aif os.path.exists(__file__):
43n/a #
44n/a # Setup something resembling a normal environment and go.
45n/a #
46n/a sys.argv[0] = __file__
47n/a del argvemulator, os, sys, _dir
48n/a execfile(__file__)
49n/aelse:
50n/a __file__ = os.path.join(_dir, '__main__.pyc')
51n/a if os.path.exists(__file__):
52n/a #
53n/a # If we have only a .pyc file we read the code object from that
54n/a #
55n/a sys.argv[0] = __file__
56n/a _fp = open(__file__, 'rb')
57n/a _fp.read(8)
58n/a __code__ = marshal.load(_fp)
59n/a #
60n/a # Again, we create an almost-normal environment (only __code__ is
61n/a # funny) and go.
62n/a #
63n/a del argvemulator, os, sys, marshal, _dir, _fp
64n/a exec __code__
65n/a else:
66n/a sys.stderr.write("%s: neither __main__.py nor __main__.pyc found\n"%sys.argv[0])
67n/a sys.exit(1)