From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QbUIp-0007IN-Po for garchives@archives.gentoo.org; Tue, 28 Jun 2011 09:09:52 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B31631C023; Tue, 28 Jun 2011 09:09:42 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 6E9F01C023 for ; Tue, 28 Jun 2011 09:09:42 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id B1F7B1BC017 for ; Tue, 28 Jun 2011 09:09:41 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 7469E8003C for ; Tue, 28 Jun 2011 09:09:40 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/tests/util/, pym/portage/util/ X-VCS-Repository: proj/portage X-VCS-Files: pym/portage/tests/util/test_varExpand.py pym/portage/util/__init__.py X-VCS-Directories: pym/portage/tests/util/ pym/portage/util/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: e63823dd8358f50559fa616313cdde3ceaf104ed Date: Tue, 28 Jun 2011 09:09:40 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 63f73f47e7ba4ec943eda43e126a281f commit: e63823dd8358f50559fa616313cdde3ceaf104ed Author: Zac Medico gentoo org> AuthorDate: Tue Jun 28 09:06:36 2011 +0000 Commit: Zac Medico gentoo org> CommitDate: Tue Jun 28 09:06:36 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/portage.git;a= =3Dcommit;h=3De63823dd varexpand: handle backslashes like more like bash For backslash expansion, this function used to behave like echo -e, but that's not needed for our purposes. We want to behave like bash does when expanding a variable assignment in a sourced file, in which case it performs backslash removal for \\ and \$ but nothing more. This will fix bash compatibility for the case reported in bug #365033. --- pym/portage/tests/util/test_varExpand.py | 33 ++++++++++++++++++++++++= - pym/portage/util/__init__.py | 39 ++++++++++--------------= ------ 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/pym/portage/tests/util/test_varExpand.py b/pym/portage/tests= /util/test_varExpand.py index 30aa390..9dd488e 100644 --- a/pym/portage/tests/util/test_varExpand.py +++ b/pym/portage/tests/util/test_varExpand.py @@ -1,5 +1,5 @@ # test_varExpand.py -- Portage Unit Testing Functionality -# Copyright 2006 Gentoo Foundation +# Copyright 2006-2010 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 =20 from portage.tests import TestCase @@ -21,6 +21,37 @@ class VarExpandTestCase(TestCase): msg=3D"Got %s !=3D %s, from varexpand( %s, %s )" % \ ( result, varDict[key], "${%s}" % key, varDict ) ) =20 + def testVarExpandBackslashes(self): + """ + We want to behave like bash does when expanding a variable + assignment in a sourced file, in which case it performs + backslash removal for \\ and \$ but nothing more. Note that + we don't handle escaped quotes here, since genconfig() uses + shlex to handle that earlier. + """ + + varDict =3D {} + tests =3D [ + ("\\", "\\"), + ("\\\\", "\\"), + ("\\\\\\", "\\\\"), + ("\\\\\\\\", "\\\\"), + ("\\$", "$"), + ("\\\\$", "\\$"), + ("\\a", "\\a"), + ("\\b", "\\b"), + ("\\n", "\\n"), + ("\\r", "\\r"), + ("\\t", "\\t"), + ("\\\"", "\\\""), + ("\\'", "\\'"), + ] + for test in tests: + result =3D varexpand( test[0], varDict ) + self.assertFalse( result !=3D test[1], + msg=3D"Got %s !=3D %s from varexpand( %s, %s )" \ + % ( result, test[1], test[0], varDict ) ) + def testVarExpandDoubleQuotes(self): =09 varDict =3D { "a":"5" } diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index 8c53522..ece0806 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -683,37 +683,24 @@ def varexpand(mystring, mydict=3DNone): newstring=3Dnewstring+" " pos=3Dpos+1 elif (mystring[pos]=3D=3D"\\"): - #backslash expansion time + # For backslash expansion, this function used to behave like + # echo -e, but that's not needed for our purposes. We want to + # behave like bash does when expanding a variable assignment + # in a sourced file, in which case it performs backslash + # removal for \\ and \$ but nothing more. Note that we don't + # handle escaped quotes here, since genconfig() uses shlex + # to handle that earlier. if (pos+1>=3Dlen(mystring)): newstring=3Dnewstring+mystring[pos] break else: - a=3Dmystring[pos+1] - pos=3Dpos+2 - if a=3D=3D'a': - newstring=3Dnewstring+chr(0o07) - elif a=3D=3D'b': - newstring=3Dnewstring+chr(0o10) - elif a=3D=3D'e': - newstring=3Dnewstring+chr(0o33) - elif (a=3D=3D'f') or (a=3D=3D'n'): - newstring=3Dnewstring+chr(0o12) - elif a=3D=3D'r': - newstring=3Dnewstring+chr(0o15) - elif a=3D=3D't': - newstring=3Dnewstring+chr(0o11) - elif a=3D=3D'v': - newstring=3Dnewstring+chr(0o13) - elif a in ('\'', '"'): - # Quote removal is handled by shlex. + a =3D mystring[pos + 1] + pos =3D pos + 2 + if a in ("\\", "$"): + newstring =3D newstring + a + else: newstring =3D newstring + mystring[pos-2:pos] - continue - elif a!=3D'\n': - # Remove backslash only, as bash does. This takes care - # of \\. Note that we don't handle quotes here since - # quote removal is handled by shlex. - newstring=3Dnewstring+mystring[pos-1:pos] - continue + continue elif (mystring[pos]=3D=3D"$") and (mystring[pos-1]!=3D"\\"): pos=3Dpos+1 if mystring[pos]=3D=3D"{":