minor, use sys.exit print shorthand

This commit is contained in:
Ebrahim Byagowi 2020-05-28 22:51:29 +04:30
parent 08f1d95a50
commit 7554f618ec
17 changed files with 39 additions and 65 deletions

View File

@ -47,8 +47,7 @@ def get_arch(filename):
try: try:
return type2arch[pe.PE_TYPE] return type2arch[pe.PE_TYPE]
except KeyError: except KeyError:
sys.stderr.write('Error: unknown architecture') sys.exit ('Error: unknown architecture')
sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':
filename = sys.argv[1] filename = sys.argv[1]

View File

@ -11,8 +11,7 @@ Input files:
import io, os.path, sys import io, os.path, sys
if len (sys.argv) != 4: if len (sys.argv) != 4:
print (__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit (1)
files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]] files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]]

View File

@ -12,8 +12,7 @@ from collections import OrderedDict
import packTab import packTab
if len (sys.argv) != 2: if len (sys.argv) != 2:
print(__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit (1)
f = open(sys.argv[1]) f = open(sys.argv[1])
header = [f.readline () for _ in range(10)] header = [f.readline () for _ in range(10)]

View File

@ -5,7 +5,7 @@
import io, os, re, sys import io, os, re, sys
if len (sys.argv) < 3: if len (sys.argv) < 3:
sys.exit(__doc__) sys.exit (__doc__)
output_file = sys.argv[1] output_file = sys.argv[1]
source_paths = sys.argv[2:] source_paths = sys.argv[2:]

View File

@ -11,8 +11,7 @@ Input files:
import io, sys import io, sys
if len (sys.argv) != 4: if len (sys.argv) != 4:
print (__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit (1)
ALLOWED_SINGLES = [0x00A0, 0x25CC] ALLOWED_SINGLES = [0x00A0, 0x25CC]
ALLOWED_BLOCKS = [ ALLOWED_BLOCKS = [

View File

@ -36,8 +36,7 @@ import sys
import unicodedata import unicodedata
if len (sys.argv) != 3: if len (sys.argv) != 3:
print (__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit (1)
from html import unescape from html import unescape
def html_unescape (parser, entity): def html_unescape (parser, entity):

View File

@ -11,8 +11,7 @@ import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
if len (sys.argv) not in (2, 3): if len (sys.argv) not in (2, 3):
print(__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit(1)
# https://github.com/harfbuzz/packtab # https://github.com/harfbuzz/packtab
import packTab import packTab

View File

@ -14,8 +14,7 @@ import io
import sys import sys
if len (sys.argv) != 5: if len (sys.argv) != 5:
print (__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit (1)
BLACKLISTED_BLOCKS = ["Thai", "Lao"] BLACKLISTED_BLOCKS = ["Thai", "Lao"]

View File

@ -23,8 +23,7 @@ import io
import sys import sys
if len (sys.argv) != 3: if len (sys.argv) != 3:
print (__doc__, file=sys.stderr) sys.exit (__doc__)
sys.exit (1)
with io.open (sys.argv[2], encoding='utf-8') as f: with io.open (sys.argv[2], encoding='utf-8') as f:
scripts_header = [f.readline () for i in range (2)] scripts_header = [f.readline () for i in range (2)]

View File

@ -28,9 +28,8 @@ hb_draw_fuzzer = os.path.join (top_builddir, "hb-draw-fuzzer" + EXEEXT)
if not os.path.exists (hb_draw_fuzzer): if not os.path.exists (hb_draw_fuzzer):
if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]): if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]):
print ("""Failed to find hb-draw-fuzzer binary automatically, sys.exit ("""Failed to find hb-draw-fuzzer binary automatically,
please provide it as the first argument to the tool""") please provide it as the first argument to the tool""")
sys.exit (1)
hb_draw_fuzzer = sys.argv[1] hb_draw_fuzzer = sys.argv[1]
@ -42,8 +41,7 @@ valgrind = None
if os.environ.get ('RUN_VALGRIND', ''): if os.environ.get ('RUN_VALGRIND', ''):
valgrind = shutil.which ('valgrind') valgrind = shutil.which ('valgrind')
if valgrind is None: if valgrind is None:
print ("""Valgrind requested but not found.""") sys.exit ("""Valgrind requested but not found.""")
sys.exit (1)
if libtool is None: if libtool is None:
print ("""Valgrind support is currently autotools only and needs libtool but not found.""") print ("""Valgrind support is currently autotools only and needs libtool but not found.""")
@ -69,5 +67,4 @@ for file in os.listdir (parent_path):
if fails: if fails:
print ("%i draw fuzzer related tests failed." % fails) sys.exit ("%d draw fuzzer related tests failed." % fails)
sys.exit (1)

View File

@ -28,9 +28,8 @@ hb_shape_fuzzer = os.path.join (top_builddir, "hb-shape-fuzzer" + EXEEXT)
if not os.path.exists (hb_shape_fuzzer): if not os.path.exists (hb_shape_fuzzer):
if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]): if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]):
print ("""Failed to find hb-shape-fuzzer binary automatically, sys.exit ("""Failed to find hb-shape-fuzzer binary automatically,
please provide it as the first argument to the tool""") please provide it as the first argument to the tool""")
sys.exit (1)
hb_shape_fuzzer = sys.argv[1] hb_shape_fuzzer = sys.argv[1]
@ -42,8 +41,7 @@ valgrind = None
if os.environ.get ('RUN_VALGRIND', ''): if os.environ.get ('RUN_VALGRIND', ''):
valgrind = shutil.which ('valgrind') valgrind = shutil.which ('valgrind')
if valgrind is None: if valgrind is None:
print ("""Valgrind requested but not found.""") sys.exit ("""Valgrind requested but not found.""")
sys.exit (1)
if libtool is None: if libtool is None:
print ("""Valgrind support is currently autotools only and needs libtool but not found.""") print ("""Valgrind support is currently autotools only and needs libtool but not found.""")
@ -68,5 +66,4 @@ for file in os.listdir (parent_path):
if fails: if fails:
print ("%i shape fuzzer related tests failed." % fails) sys.exit ("%d shape fuzzer related tests failed." % fails)
sys.exit (1)

View File

@ -28,9 +28,8 @@ hb_subset_fuzzer = os.path.join (top_builddir, "hb-subset-fuzzer" + EXEEXT)
if not os.path.exists (hb_subset_fuzzer): if not os.path.exists (hb_subset_fuzzer):
if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]): if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
print ("""Failed to find hb-subset-fuzzer binary automatically, sys.exit ("""Failed to find hb-subset-fuzzer binary automatically,
please provide it as the first argument to the tool""") please provide it as the first argument to the tool""")
sys.exit (1)
hb_subset_fuzzer = sys.argv[1] hb_subset_fuzzer = sys.argv[1]
@ -42,8 +41,7 @@ valgrind = None
if os.environ.get('RUN_VALGRIND', ''): if os.environ.get('RUN_VALGRIND', ''):
valgrind = shutil.which ('valgrind') valgrind = shutil.which ('valgrind')
if valgrind is None: if valgrind is None:
print ("""Valgrind requested but not found.""") sys.exit ("""Valgrind requested but not found.""")
sys.exit (1)
if libtool is None: if libtool is None:
print ("""Valgrind support is currently autotools only and needs libtool but not found.""") print ("""Valgrind support is currently autotools only and needs libtool but not found.""")
@ -75,5 +73,4 @@ run_dir (os.path.join (srcdir, "..", "subset", "data", "fonts"))
run_dir (os.path.join (srcdir, "fonts")) run_dir (os.path.join (srcdir, "fonts"))
if fails: if fails:
print ("%i subset fuzzer related tests failed." % fails) sys.exit ("%d subset fuzzer related tests failed." % fails)
sys.exit (1)

View File

@ -57,4 +57,5 @@ for elt in html.findall(".//*[@class='expected-no-crash'][@ft:id]", namespaces):
opts = '--variations=%s' % variations opts = '--variations=%s' % variations
print ("../fonts/%s:%s:%s:*" % (font, opts, unistr(text))) print ("../fonts/%s:%s:%s:*" % (font, opts, unistr(text)))
sys.exit(0 if found else 1) if not found:
sys.exit (1)

View File

@ -4,7 +4,6 @@ from hb_test_tools import *
import sys, os import sys, os
if len (sys.argv) < 2: if len (sys.argv) < 2:
print ("usage: %s FILES..." % sys.argv[0]) sys.exit ("usage: %s FILES..." % sys.argv[0])
sys.exit (1)
ZipDiffer.diff_files (FileHelpers.open_file_or_stdin (f) for f in sys.argv[1:]) ZipDiffer.diff_files (FileHelpers.open_file_or_stdin (f) for f in sys.argv[1:])

View File

@ -145,8 +145,7 @@ class ZipDiffer:
sys.stdout.writelines ([symbols[i], l]) sys.stdout.writelines ([symbols[i], l])
except IOError as e: except IOError as e:
if e.errno != errno.EPIPE: if e.errno != errno.EPIPE:
print ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror), file=sys.stderr) sys.exit ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror))
sys.exit (1)
class DiffFilters: class DiffFilters:
@ -321,8 +320,7 @@ class UtilMains:
def process_multiple_files (callback, mnemonic = "FILE"): def process_multiple_files (callback, mnemonic = "FILE"):
if "--help" in sys.argv: if "--help" in sys.argv:
print ("Usage: %s %s..." % (sys.argv[0], mnemonic)) sys.exit ("Usage: %s %s..." % (sys.argv[0], mnemonic))
sys.exit (1)
try: try:
files = sys.argv[1:] if len (sys.argv) > 1 else ['-'] files = sys.argv[1:] if len (sys.argv) > 1 else ['-']
@ -330,23 +328,20 @@ class UtilMains:
callback (FileHelpers.open_file_or_stdin (s)) callback (FileHelpers.open_file_or_stdin (s))
except IOError as e: except IOError as e:
if e.errno != errno.EPIPE: if e.errno != errno.EPIPE:
print ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror), file=sys.stderr) sys.exit ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror))
sys.exit (1)
@staticmethod @staticmethod
def process_multiple_args (callback, mnemonic): def process_multiple_args (callback, mnemonic):
if len (sys.argv) == 1 or "--help" in sys.argv: if len (sys.argv) == 1 or "--help" in sys.argv:
print ("Usage: %s %s..." % (sys.argv[0], mnemonic)) sys.exit ("Usage: %s %s..." % (sys.argv[0], mnemonic))
sys.exit (1)
try: try:
for s in sys.argv[1:]: for s in sys.argv[1:]:
callback (s) callback (s)
except IOError as e: except IOError as e:
if e.errno != errno.EPIPE: if e.errno != errno.EPIPE:
print ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror), file=sys.stderr) sys.exit ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror))
sys.exit (1)
@staticmethod @staticmethod
def filter_multiple_strings_or_stdin (callback, mnemonic, \ def filter_multiple_strings_or_stdin (callback, mnemonic, \
@ -354,9 +349,12 @@ class UtilMains:
concat_separator = False): concat_separator = False):
if "--help" in sys.argv: if "--help" in sys.argv:
print ("Usage:\n %s %s...\nor:\n %s\n\nWhen called with no arguments, input is read from standard input." \ sys.exit ("""Usage:
% (sys.argv[0], mnemonic, sys.argv[0])) %s %s...
sys.exit (1) or:
%s
When called with no arguments, input is read from standard input.
""" % (sys.argv[0], mnemonic, sys.argv[0]))
try: try:
if len (sys.argv) == 1: if len (sys.argv) == 1:
@ -374,8 +372,7 @@ class UtilMains:
print (separator.join (callback (x) for x in (args))) print (separator.join (callback (x) for x in (args)))
except IOError as e: except IOError as e:
if e.errno != errno.EPIPE: if e.errno != errno.EPIPE:
print ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror), file=sys.stderr) sys.exit ("%s: %s: %s" % (sys.argv[0], e.filename, e.strerror))
sys.exit (1)
class Unicode: class Unicode:
@ -448,8 +445,7 @@ class Manifest:
if not os.path.exists (s): if not os.path.exists (s):
if strict: if strict:
print ("%s: %s does not exist" % (sys.argv[0], s), file=sys.stderr) sys.exit ("%s: %s does not exist" % (sys.argv[0], s))
sys.exit (1)
return return
s = os.path.normpath (s) s = os.path.normpath (s)
@ -464,8 +460,7 @@ class Manifest:
yield p yield p
except IOError: except IOError:
if strict: if strict:
print ("%s: %s does not exist" % (sys.argv[0], os.path.join (s, "MANIFEST")), file=sys.stderr) sys.exit ("%s: %s does not exist" % (sys.argv[0], os.path.join (s, "MANIFEST")))
sys.exit (1)
return return
else: else:
yield s yield s

View File

@ -11,8 +11,7 @@ if len (args) and args[0] == "--reference":
args = args[1:] args = args[1:]
if not args or args[0].find('hb-shape') == -1 or not os.path.exists (args[0]): if not args or args[0].find('hb-shape') == -1 or not os.path.exists (args[0]):
print ("""First argument does not seem to point to usable hb-shape.""") sys.exit ("""First argument does not seem to point to usable hb-shape.""")
sys.exit (1)
hb_shape, args = args[0], args[1:] hb_shape, args = args[0], args[1:]
def cmd(command): def cmd(command):

View File

@ -127,13 +127,11 @@ def check_ots (path):
args = sys.argv[1:] args = sys.argv[1:]
if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]): if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
print ("First argument does not seem to point to usable hb-subset.") sys.exit ("First argument does not seem to point to usable hb-subset.")
sys.exit (1)
hb_subset, args = args[0], args[1:] hb_subset, args = args[0], args[1:]
if not len (args): if not len (args):
print ("No tests supplied.") sys.exit ("No tests supplied.")
sys.exit (1)
has_ots = has_ots() has_ots = has_ots()
@ -146,7 +144,6 @@ for path in args:
fails += run_test (test, has_ots) fails += run_test (test, has_ots)
if fails != 0: if fails != 0:
print (str (fails) + " test(s) failed.") sys.exit ("%d test(s) failed." % fails)
sys.exit(1)
else: else:
print ("All tests passed.") print ("All tests passed.")