From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id B7F7E138A1A for ; Mon, 19 Jan 2015 09:13:35 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id C5C5CE08FF; Mon, 19 Jan 2015 09:13:34 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 5CA0FE08FC for ; Mon, 19 Jan 2015 09:13:34 +0000 (UTC) Received: from localhost.localdomain (ip174-67-205-96.oc.oc.cox.net [174.67.205.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: zmedico) by smtp.gentoo.org (Postfix) with ESMTPSA id 2EE35340614; Mon, 19 Jan 2015 09:13:33 +0000 (UTC) From: Zac Medico To: gentoo-portage-dev@lists.gentoo.org Cc: Zac Medico Subject: [gentoo-portage-dev] [PATCH] chpathtool.py: avoid unnecessary optparse import Date: Mon, 19 Jan 2015 01:13:17 -0800 Message-Id: <1421658797-19090-1-git-send-email-zmedico@gentoo.org> X-Mailer: git-send-email 2.0.5 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-portage-dev@lists.gentoo.org Reply-to: gentoo-portage-dev@lists.gentoo.org X-Archives-Salt: 08570e7b-8c90-46c6-9b65-eb0f23aeda79 X-Archives-Hash: 23f9a7f278607f00ab44839b1a639760 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