As a performance optimization, use StringIO instead of _insert_newline_eof to
authorZac Medico <zmedico@gentoo.org>
Sat, 4 Jul 2009 19:16:35 +0000 (19:16 -0000)
committerZac Medico <zmedico@gentoo.org>
Sat, 4 Jul 2009 19:16:35 +0000 (19:16 -0000)
solve bug #228117. Thanks to Marat Radchenko <slonopotamusorama@gmail.com>
for this patch.

svn path=/main/trunk/; revision=13779

pym/portage/util.py

index fad1a9b27c48b1bc0dc6c55d2cb6380d27c55516..d57b1bc3545af807cdc190a27cf3c979d6bd597c 100644 (file)
@@ -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)