Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH] chpathtool.py: avoid unnecessary optparse import
Date: Mon, 19 Jan 2015 09:13:35
Message-Id: 1421658797-19090-1-git-send-email-zmedico@gentoo.org
1 Since commit d217db2bc76e4c1a2e75685b4a00e25f7d8142a8, the optparse
2 module has been imported unconditionally, even when the argparse module
3 is available. Fix it to import optparse only if the argparse import
4 fails.
5
6 Fixes: d217db2bc76e ("Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.")
7 ---
8 bin/chpathtool.py | 22 ++++++++--------------
9 1 file changed, 8 insertions(+), 14 deletions(-)
10
11 diff --git a/bin/chpathtool.py b/bin/chpathtool.py
12 index 9b26086..842f1f4 100755
13 --- a/bin/chpathtool.py
14 +++ b/bin/chpathtool.py
15 @@ -13,14 +13,12 @@ import os
16 import stat
17 import sys
18
19 -from portage.util._argparse import ArgumentParser
20 -
21 -# Argument parsing compatibility for Python 2.6 using optparse.
22 -if sys.hexversion < 0x2070000:
23 +try:
24 + from argparse import ArgumentParser
25 +except ImportError:
26 + ArgumentParser = None
27 from optparse import OptionParser
28
29 -from optparse import OptionError
30 -
31 CONTENT_ENCODING = 'utf_8'
32 FS_ENCODING = 'utf_8'
33
34 @@ -154,8 +152,8 @@ def chpath_inplace_symlink(filename, st, old, new):
35
36 def main(argv):
37
38 - parser = ArgumentParser(description=doc)
39 - try:
40 + if ArgumentParser is not None:
41 + parser = ArgumentParser(description=doc)
42 parser.add_argument('location', default=None,
43 help='root directory (e.g. $D)')
44 parser.add_argument('old', default=None,
45 @@ -165,9 +163,8 @@ def main(argv):
46 opts = parser.parse_args(argv)
47
48 location, old, new = opts.location, opts.old, opts.new
49 - except OptionError:
50 + else:
51 # Argument parsing compatibility for Python 2.6 using optparse.
52 - if sys.hexversion < 0x2070000:
53 parser = OptionParser(description=doc,
54 usage="usage: %prog [-h] location old new\n\n" + \
55 " location: root directory (e.g. $D)\n" + \
56 @@ -178,13 +175,10 @@ def main(argv):
57
58 if len(args) != 3:
59 parser.print_usage()
60 - print("%s: error: expected 3 arguments, got %i"
61 + parser.error("%s: error: expected 3 arguments, got %i"
62 % (__file__, len(args)))
63 - return
64
65 location, old, new = args[0:3]
66 - else:
67 - raise
68
69 is_text_file = IsTextFile()
70
71 --
72 2.0.5

Replies