-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpython-exec.in
More file actions
99 lines (83 loc) · 2.95 KB
/
python-exec.in
File metadata and controls
99 lines (83 loc) · 2.95 KB
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
#!@bindir@/python-exec2c@exeext@
# vim:fileencoding=utf-8:ft=python
# (c) 2012-2021 Michał Górny
# Released under the terms of the 2-clause BSD license.
#
# This is not the script you are looking for. This is just a wrapper.
# The actual scripts of this application were installed
# in subdirectories of @PYTHON_SCRIPTROOT@.
# You are most likely looking for one of these.
from __future__ import with_statement
# prepare a clean globals for exec()
new_globals = dict(globals())
# we have to keep the import top, so delete it from globals
del new_globals['with_statement']
import errno
import os
import os.path
import sys
try:
from epython import EPYTHON
except ImportError:
EPYTHON = os.path.basename(sys.executable)
if '@exeext@' and EPYTHON.endswith('@exeext@'):
EPYTHON = EPYTHON[:-len('@exeext@')]
# Alike python-exec2c, perform symlink resolution on target until
# EINVAL is hit. If the final target is python-exec2, use the last
# symlink name preceding it. Otherwise, use the final target.
prev_target = None
target = sys.argv[0]
while True:
try:
next_target = os.path.join(os.path.dirname(target),
os.readlink(target))
except OSError as e:
if e.errno == errno.EINTR:
# retry
continue
if e.errno == errno.EINVAL:
# if the final target is python-exec2, use last symlink
if os.path.basename(target) in ('python-exec2',
'python-exec2@exeext@'):
if prev_target is None:
sys.stderr.write(
'{}: python-exec2 is a wrapper, it must not '
'be run directly.\n'.format(target))
sys.exit(127)
target = prev_target
break
raise
else:
prev_target = target
target = next_target
target = os.path.join('@PYTHON_SCRIPTROOT@', EPYTHON,
os.path.basename(target))
data = None
while data is None:
try:
kwargs = {}
if sys.version_info >= (3,):
import tokenize
# need to provide encoding
with open(target, 'rb') as f:
kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0]
with open(target, 'r', **kwargs) as f:
data = f.read()
except IOError as e:
if e.errno == errno.EINTR:
# retry
continue
elif e.errno == errno.ENOENT:
sys.stderr.write(
'{}: this Python implementation ({}) is not supported '
'by the script.\n'.format(target, EPYTHON))
sys.exit(127)
else:
raise
sys.argv[0] = target
os.environ["PATH"] += os.pathsep + os.path.join('/usr/lib/python-exec', EPYTHON)
# in python3.9+, __file__ paths are absolute
if sys.version_info >= (3, 9):
target = os.path.abspath(target)
new_globals['__file__'] = target
exec(data, new_globals)