From 137fc96206a58ba80533e280583aa34e31b129fc Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sat, 4 Jul 2009 19:16:35 +0000 Subject: As a performance optimization, use StringIO instead of _insert_newline_eof to solve bug #228117. Thanks to Marat Radchenko for this patch. svn path=/main/trunk/; revision=13779 --- pym/portage/util.py | 64 ++++------------------------------------------------- 1 file changed, 4 insertions(+), 60 deletions(-) diff --git a/pym/portage/util.py b/pym/portage/util.py index fad1a9b27..d57b1bc35 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -353,65 +353,6 @@ class _tolerant_shlex(shlex.shlex): (self.infile, str(e)), noiselevel=-1) return (newfile, StringIO()) -class _insert_newline_eof(ObjectProxy): - """ - Read functions insert anywhere from 0 and 2 newlines just before eof. - This is useful as a workaround for avoiding a silent error in shlex that - is triggered by a source statement at the end of the file without a - trailing newline after the source statement. - """ - - def __init__(self, *pargs, **kargs): - ObjectProxy.__init__(self) - object.__setattr__(self, '_file', open(*pargs, **kargs)) - - def _get_target(self): - return object.__getattribute__(self, '_file') - - def __getattribute__(self, attr): - if attr in ('read', 'readline', 'readlines'): - return object.__getattribute__(self, attr) - return getattr(object.__getattribute__(self, '_file'), attr) - - def read(self, *args): - try: - object.__getattribute__(self, '_got_eof') - return "" - except AttributeError: - pass - rval = object.__getattribute__(self, '_file').read(*args) - if rval and not args and rval[-1:] != "\n": - rval += "\n" - if not rval: - object.__setattr__(self, '_got_eof', True) - return "\n" - return rval - - def readline(self, *args): - try: - object.__getattribute__(self, '_got_eof') - return "" - except AttributeError: - pass - rval = object.__getattribute__(self, '_file').readline(*args) - if rval and rval[-1:] != "\n": - rval += "\n" - if not rval: - object.__setattr__(self, '_got_eof', True) - rval = "\n" - return rval - - def readlines(self, *args): - try: - object.__getattribute__(self, '_got_eof') - return [] - except AttributeError: - pass - lines = object.__getattribute__(self, '_file').readlines(*args) - if lines and lines[-1][-1:] != "\n": - lines[-1] += "\n" - return lines - def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): if isinstance(expand, dict): # Some existing variable definitions have been @@ -422,7 +363,10 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): expand_map = {} mykeys = {} try: - f = _insert_newline_eof(mycfg) + # Workaround for avoiding a silent error in shlex that + # is triggered by a source statement at the end of the file without a + # trailing newline after the source statement + f = StringIO("\n".join(open(mycfg, 'r').readlines()) + "\n") except IOError, e: if e.errno == PermissionDenied.errno: raise PermissionDenied(mycfg) -- cgit v1.2.3-1-g7c22