public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Zac Medico" <zmedico@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/tests/util/, pym/portage/util/
Date: Tue, 28 Jun 2011 09:09:40 +0000 (UTC)	[thread overview]
Message-ID: <e63823dd8358f50559fa616313cdde3ceaf104ed.zmedico@gentoo> (raw)

commit:     e63823dd8358f50559fa616313cdde3ceaf104ed
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Jun 28 09:06:36 2011 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Tue Jun 28 09:06:36 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=e63823dd

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
 
 from portage.tests import TestCase
@@ -21,6 +21,37 @@ class VarExpandTestCase(TestCase):
 				msg="Got %s != %s, from varexpand( %s, %s )" % \
 					( result, varDict[key], "${%s}" % key, varDict ) )
 
+	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 = {}
+		tests = [
+			("\\", "\\"),
+			("\\\\", "\\"),
+			("\\\\\\", "\\\\"),
+			("\\\\\\\\", "\\\\"),
+			("\\$", "$"),
+			("\\\\$", "\\$"),
+			("\\a", "\\a"),
+			("\\b", "\\b"),
+			("\\n", "\\n"),
+			("\\r", "\\r"),
+			("\\t", "\\t"),
+			("\\\"", "\\\""),
+			("\\'", "\\'"),
+		]
+		for test in tests:
+			result = varexpand( test[0], varDict )
+			self.assertFalse( result != test[1],
+				msg="Got %s != %s from varexpand( %s, %s )" \
+				% ( result, test[1], test[0], varDict ) )
+
 	def testVarExpandDoubleQuotes(self):
 		
 		varDict = { "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=None):
 				newstring=newstring+" "
 				pos=pos+1
 			elif (mystring[pos]=="\\"):
-				#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>=len(mystring)):
 					newstring=newstring+mystring[pos]
 					break
 				else:
-					a=mystring[pos+1]
-					pos=pos+2
-					if a=='a':
-						newstring=newstring+chr(0o07)
-					elif a=='b':
-						newstring=newstring+chr(0o10)
-					elif a=='e':
-						newstring=newstring+chr(0o33)
-					elif (a=='f') or (a=='n'):
-						newstring=newstring+chr(0o12)
-					elif a=='r':
-						newstring=newstring+chr(0o15)
-					elif a=='t':
-						newstring=newstring+chr(0o11)
-					elif a=='v':
-						newstring=newstring+chr(0o13)
-					elif a in ('\'', '"'):
-						# Quote removal is handled by shlex.
+					a = mystring[pos + 1]
+					pos = pos + 2
+					if a in ("\\", "$"):
+						newstring = newstring + a
+					else:
 						newstring = newstring + mystring[pos-2:pos]
-						continue
-					elif a!='\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=newstring+mystring[pos-1:pos]
-						continue
+					continue
 			elif (mystring[pos]=="$") and (mystring[pos-1]!="\\"):
 				pos=pos+1
 				if mystring[pos]=="{":



             reply	other threads:[~2011-06-28  9:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-28  9:09 Zac Medico [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-07-01  8:47 [gentoo-commits] proj/portage:master commit in: pym/portage/tests/util/, pym/portage/util/ Zac Medico
2012-05-12 22:17 Zac Medico
2014-01-22  4:49 Jesus Rivero
2017-04-20 19:39 Zac Medico
2018-06-27  3:32 Zac Medico

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e63823dd8358f50559fa616313cdde3ceaf104ed.zmedico@gentoo \
    --to=zmedico@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox