Skip to content

Commit 33dcab6

Browse files
committed
Modernize the coding style
Signed-off-by: Michał Górny <mgorny@gentoo.org>
1 parent 7c34451 commit 33dcab6

File tree

1 file changed

+60
-51
lines changed

1 file changed

+60
-51
lines changed

src/python-exec.in

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,84 +6,93 @@
66
# This is not the script you are looking for. This is just a wrapper.
77
# The actual scripts of this application were installed
88
# in subdirectories of @PYTHON_SCRIPTROOT@.
9-
# You are most likely looking for one of those.
9+
# You are most likely looking for one of these.
1010

1111
from __future__ import with_statement
1212

13+
1314
# prepare a clean globals for exec()
1415
new_globals = dict(globals())
1516
# we have to keep the import top, so delete it from globals
1617
del new_globals['with_statement']
1718

18-
import errno, os, os.path, sys
19+
20+
import errno
21+
import os
22+
import os.path
23+
import sys
24+
1925

2026
try:
21-
from epython import EPYTHON
27+
from epython import EPYTHON
2228
except ImportError:
23-
EPYTHON = os.path.basename(sys.executable)
24-
if '@exeext@' and EPYTHON.endswith('@exeext@'):
25-
EPYTHON = EPYTHON[:-len('@exeext@')]
29+
EPYTHON = os.path.basename(sys.executable)
30+
if '@exeext@' and EPYTHON.endswith('@exeext@'):
31+
EPYTHON = EPYTHON[:-len('@exeext@')]
32+
2633

2734
# Alike python-exec2c, perform symlink resolution on target until
2835
# EINVAL is hit. If the final target is python-exec2, use the last
2936
# symlink name preceding it. Otherwise, use the final target.
3037
prev_target = None
3138
target = sys.argv[0]
3239
while True:
33-
try:
34-
next_target = os.path.join(os.path.dirname(target),
35-
os.readlink(target))
36-
except OSError as e:
37-
if e.errno == errno.EINTR:
38-
# retry
39-
continue
40-
elif e.errno == errno.EINVAL:
41-
# if the final target is python-exec2, use last symlink
42-
if os.path.basename(target) in ( 'python-exec2', 'python-exec2@exeext@' ):
43-
if prev_target is None:
44-
sys.stderr.write('%s: python-exec2 is a wrapper, it must not be run directly.\n'
45-
% target)
46-
sys.exit(127)
47-
48-
target = prev_target
49-
break
50-
else:
51-
raise
52-
else:
53-
prev_target = target
54-
target = next_target
40+
try:
41+
next_target = os.path.join(os.path.dirname(target),
42+
os.readlink(target))
43+
except OSError as e:
44+
if e.errno == errno.EINTR:
45+
# retry
46+
continue
47+
if e.errno == errno.EINVAL:
48+
# if the final target is python-exec2, use last symlink
49+
if os.path.basename(target) in ('python-exec2',
50+
'python-exec2@exeext@'):
51+
if prev_target is None:
52+
sys.stderr.write(
53+
'{}: python-exec2 is a wrapper, it must not '
54+
'be run directly.\n'.format(target))
55+
sys.exit(127)
56+
57+
target = prev_target
58+
break
59+
raise
60+
else:
61+
prev_target = target
62+
target = next_target
5563

5664
target = os.path.join('@PYTHON_SCRIPTROOT@', EPYTHON,
57-
os.path.basename(target))
65+
os.path.basename(target))
5866

5967
data = None
6068
while data is None:
61-
try:
62-
kwargs = {}
63-
if sys.version_info[0] >= 3:
64-
import tokenize
65-
66-
# need to provide encoding
67-
with open(target, 'rb') as f:
68-
kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0]
69-
70-
with open(target, 'r', **kwargs) as f:
71-
data = f.read()
72-
except IOError as e:
73-
if e.errno == errno.EINTR:
74-
# retry
75-
continue
76-
elif e.errno == errno.ENOENT:
77-
sys.stderr.write('%s: this Python implementation (%s) is not supported by the script.\n'
78-
% (target, EPYTHON))
79-
sys.exit(127)
80-
else:
81-
raise
69+
try:
70+
kwargs = {}
71+
if sys.version_info >= (3,):
72+
import tokenize
73+
74+
# need to provide encoding
75+
with open(target, 'rb') as f:
76+
kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0]
77+
78+
with open(target, 'r', **kwargs) as f:
79+
data = f.read()
80+
except IOError as e:
81+
if e.errno == errno.EINTR:
82+
# retry
83+
continue
84+
elif e.errno == errno.ENOENT:
85+
sys.stderr.write(
86+
'{}: this Python implementation ({}) is not supported '
87+
'by the script.\n'.format(target, EPYTHON))
88+
sys.exit(127)
89+
else:
90+
raise
8291

8392
sys.argv[0] = target
8493
# in python3.9+, __file__ paths are absolute
8594
if sys.version_info >= (3, 9):
86-
target = os.path.abspath(target)
95+
target = os.path.abspath(target)
8796
new_globals['__file__'] = target
8897

8998
exec(data, new_globals)

0 commit comments

Comments
 (0)