diff options
author | Zac Medico <zmedico@gentoo.org> | 2009-07-04 19:16:35 +0000 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2009-07-04 19:16:35 +0000 |
commit | 137fc96206a58ba80533e280583aa34e31b129fc (patch) | |
tree | de82711997aea1b372fd10ab045215251942dcfa | |
parent | d56ba800426a26e93c022367068ceb4471579a0e (diff) | |
download | portage-137fc96206a58ba80533e280583aa34e31b129fc.tar.gz portage-137fc96206a58ba80533e280583aa34e31b129fc.tar.bz2 portage-137fc96206a58ba80533e280583aa34e31b129fc.zip |
As a performance optimization, use StringIO instead of _insert_newline_eof to
solve bug #228117. Thanks to Marat Radchenko <slonopotamusorama@gmail.com>
for this patch.
svn path=/main/trunk/; revision=13779
-rw-r--r-- | pym/portage/util.py | 64 |
1 files 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) |