Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH v2] repoman: unroll escaped lines so we can check the entirety of it
Date: Thu, 24 May 2012 21:16:05
Message-Id: 4FBE9177.6080709@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH v2] repoman: unroll escaped lines so we can check the entirety of it by Mike Frysinger
1 On 05/24/2012 12:20 PM, Mike Frysinger wrote:
2 > + # A normal line will end in the two bytes: <\> <\n>. So decoding
3 > + # that will result in python thinking the <\n> is being escaped
4 > + # and eat the single <\> which makes it hard for us to detect.
5 > + # Instead, strip the newline (which we know all lines have), and
6 > + # append a <0>. Then when python escapes it, if the line ended
7 > + # in a <\>, we'll end up with a <\0> marker to key off of. This
8 > + # shouldn't be a problem with any valid ebuild ...
9 > + line_escaped = (line.rstrip('\n') + '0').decode('string_escape')
10
11 That decode('string_escape') method won't work in python3, because the
12 str object doesn't have a decode method. I think something like this
13 will work with both python3 and python2:
14
15 import codecs
16
17 unicode_escape_codec = codecs.lookup('unicode_escape')
18
19 def unicode_escape(s):
20 return unicode_escape_codec(s)[0]
21
22 line_escaped = unicode_escape(line.rstrip('\n') + '0')
23 --
24 Thanks,
25 Zac

Replies