public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-portage-dev] [PATCH] chpathtool.py: avoid unnecessary optparse import
@ 2015-01-19  9:13 Zac Medico
  2015-01-19 15:33 ` Brian Dolbec
  0 siblings, 1 reply; 2+ messages in thread
From: Zac Medico @ 2015-01-19  9:13 UTC (permalink / raw
  To: gentoo-portage-dev; +Cc: Zac Medico

Since commit d217db2bc76e4c1a2e75685b4a00e25f7d8142a8, the optparse
module has been imported unconditionally, even when the argparse module
is available. Fix it to import optparse only if the argparse import
fails.

Fixes: d217db2bc76e ("Make use of optparse to fix argument parsing for Python 2.6 in bin/chpathtool.py.")
---
 bin/chpathtool.py | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/bin/chpathtool.py b/bin/chpathtool.py
index 9b26086..842f1f4 100755
--- a/bin/chpathtool.py
+++ b/bin/chpathtool.py
@@ -13,14 +13,12 @@ import os
 import stat
 import sys
 
-from portage.util._argparse import ArgumentParser
-
-# Argument parsing compatibility for Python 2.6 using optparse.
-if sys.hexversion < 0x2070000:
+try:
+	from argparse import ArgumentParser
+except ImportError:
+	ArgumentParser = None
 	from optparse import OptionParser
 
-from optparse import OptionError
-
 CONTENT_ENCODING = 'utf_8'
 FS_ENCODING = 'utf_8'
 
@@ -154,8 +152,8 @@ def chpath_inplace_symlink(filename, st, old, new):
 
 def main(argv):
 
-	parser = ArgumentParser(description=doc)
-	try:
+	if ArgumentParser is not None:
+		parser = ArgumentParser(description=doc)
 		parser.add_argument('location', default=None,
 			help='root directory (e.g. $D)')
 		parser.add_argument('old', default=None,
@@ -165,9 +163,8 @@ def main(argv):
 		opts = parser.parse_args(argv)
 
 		location, old, new = opts.location, opts.old, opts.new
-	except OptionError:
+	else:
 		# Argument parsing compatibility for Python 2.6 using optparse.
-		if sys.hexversion < 0x2070000:
 			parser = OptionParser(description=doc,
 				usage="usage: %prog [-h] location old new\n\n" + \
 				"  location: root directory (e.g. $D)\n" + \
@@ -178,13 +175,10 @@ def main(argv):
 
 			if len(args) != 3:
 				parser.print_usage()
-				print("%s: error: expected 3 arguments, got %i"
+				parser.error("%s: error: expected 3 arguments, got %i"
 					% (__file__, len(args)))
-				return
 
 			location, old, new = args[0:3]
-		else:
-			raise
 
 	is_text_file = IsTextFile()
 
-- 
2.0.5



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [gentoo-portage-dev] [PATCH] chpathtool.py: avoid unnecessary optparse import
  2015-01-19  9:13 [gentoo-portage-dev] [PATCH] chpathtool.py: avoid unnecessary optparse import Zac Medico
@ 2015-01-19 15:33 ` Brian Dolbec
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Dolbec @ 2015-01-19 15:33 UTC (permalink / raw
  To: gentoo-portage-dev

On Mon, 19 Jan 2015 01:13:17 -0800
Zac Medico <zmedico@gentoo.org> wrote:

> Since commit d217db2bc76e4c1a2e75685b4a00e25f7d8142a8, the optparse
> module has been imported unconditionally, even when the argparse
> module is available. Fix it to import optparse only if the argparse
> import fails.
> 
> Fixes: d217db2bc76e ("Make use of optparse to fix argument parsing
> for Python 2.6 in bin/chpathtool.py.") ---
>  bin/chpathtool.py | 22 ++++++++--------------
>  1 file changed, 8 insertions(+), 14 deletions(-)
> 
>

Looks good. :)

-- 
Brian Dolbec <dolsen>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-01-19 15:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-19  9:13 [gentoo-portage-dev] [PATCH] chpathtool.py: avoid unnecessary optparse import Zac Medico
2015-01-19 15:33 ` Brian Dolbec

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox