From aa109884a0168656c3548cc90ca5a4854f3928c2 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 15 May 2012 17:06:38 -0700 Subject: getconfig: use shlex.error_leader() more This fixes it to show the correct file/line, even when one file sources another. --- pym/portage/util/__init__.py | 45 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'pym/portage/util/__init__.py') diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index 57e8c37b7..4797f53e4 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -41,7 +41,7 @@ from portage import _os_merge from portage import _unicode_encode from portage import _unicode_decode from portage.exception import InvalidAtom, PortageException, FileNotFound, \ - OperationNotPermitted, PermissionDenied, ReadOnlyFileSystem + OperationNotPermitted, ParseError, PermissionDenied, ReadOnlyFileSystem from portage.localization import _ from portage.proxy.objectproxy import ObjectProxy from portage.cache.mappings import UserDict @@ -573,6 +573,7 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): writemsg(("!!! " + _("Please use dos2unix to convert line endings " + \ "in config file: '%s'") + "\n") % mycfg, noiselevel=-1) + lex = None try: if tolerant: shlex_class = _tolerant_shlex @@ -596,43 +597,38 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): break; equ=lex.get_token() if (equ==''): - #unexpected end of file - #lex.error_leader(self.filename,lex.lineno) + msg = lex.error_leader() + _("Unexpected EOF") if not tolerant: - writemsg(_("!!! Unexpected end of config file: variable %s\n") % key, - noiselevel=-1) - raise Exception(_("ParseError: Unexpected EOF: %s: on/before line %s") % (mycfg, lex.lineno)) + raise ParseError(msg) else: + writemsg("%s\n" % msg, noiselevel=-1) return mykeys elif (equ!='='): - #invalid token - #lex.error_leader(self.filename,lex.lineno) + msg = lex.error_leader() + \ + _("Invalid token '%s' (not '=')") % (equ,) if not tolerant: - raise Exception(_("ParseError: Invalid token " - "'%s' (not '='): %s: line %s") % \ - (equ, mycfg, lex.lineno)) + raise Exception(msg) else: + writemsg("%s\n" % msg, noiselevel=-1) return mykeys val=lex.get_token() if val is None: - #unexpected end of file - #lex.error_leader(self.filename,lex.lineno) + msg = lex.error_leader() + \ + _("Unexpected end of config file: variable '%s'") % (key,) if not tolerant: - writemsg(_("!!! Unexpected end of config file: variable %s\n") % key, - noiselevel=-1) - raise portage.exception.CorruptionError(_("ParseError: Unexpected EOF: %s: line %s") % (mycfg, lex.lineno)) + raise ParseError(msg) else: + writemsg("%s\n" % msg, noiselevel=-1) return mykeys key = _unicode_decode(key) val = _unicode_decode(val) if _invalid_var_name_re.search(key) is not None: + msg = lex.error_leader() + \ + _("Invalid variable name '%s'") % (key,) if not tolerant: - raise Exception(_( - "ParseError: Invalid variable name '%s': line %s") % \ - (key, lex.lineno - 1)) - writemsg(_("!!! Invalid variable name '%s': line %s in %s\n") \ - % (key, lex.lineno - 1, mycfg), noiselevel=-1) + raise ParseError(msg) + writemsg("%s\n" % msg, noiselevel=-1) continue if expand: @@ -644,7 +640,12 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): except SystemExit as e: raise except Exception as e: - raise portage.exception.ParseError(str(e)+" in "+mycfg) + if isinstance(e, ParseError) or lex is None: + raise + msg = _unicode_decode("%s%s") % (lex.error_leader(), e) + writemsg("%s\n" % msg, noiselevel=-1) + raise + return mykeys _varexpand_word_chars = frozenset(string.ascii_letters + string.digits + "_") -- cgit v1.2.3-1-g7c22