mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Always has the lineno and filename of the source text.
This commit is contained in:
parent
a76ba6ed9b
commit
ec6ec90dd2
1 changed files with 22 additions and 5 deletions
27
Lib/netrc.py
27
Lib/netrc.py
|
@ -4,7 +4,20 @@
|
||||||
|
|
||||||
import os, shlex
|
import os, shlex
|
||||||
|
|
||||||
__all__ = ["netrc"]
|
__all__ = ["netrc", "NetrcParseError"]
|
||||||
|
|
||||||
|
|
||||||
|
class NetrcParseError(Exception):
|
||||||
|
"""Exception raised on syntax errors in the .netrc file."""
|
||||||
|
def __init__(self, msg, filename=None, lineno=None):
|
||||||
|
self.filename = file
|
||||||
|
self.lineno = lineno
|
||||||
|
self.msg = msg
|
||||||
|
Exception.__init__(self, msg)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno)
|
||||||
|
|
||||||
|
|
||||||
class netrc:
|
class netrc:
|
||||||
def __init__(self, file=None):
|
def __init__(self, file=None):
|
||||||
|
@ -37,8 +50,8 @@ class netrc:
|
||||||
tt = line
|
tt = line
|
||||||
self.macros[entryname].append(line)
|
self.macros[entryname].append(line)
|
||||||
else:
|
else:
|
||||||
raise SyntaxError, "bad toplevel token %s, file %s, line %d" \
|
raise NetrcParseError(
|
||||||
% (tt, file, lexer.lineno)
|
"bad toplevel token %r" % tt, file, lexer.lineno)
|
||||||
|
|
||||||
# We're looking at start of an entry for a named machine or default.
|
# We're looking at start of an entry for a named machine or default.
|
||||||
if toplevel == 'machine':
|
if toplevel == 'machine':
|
||||||
|
@ -54,7 +67,10 @@ class netrc:
|
||||||
lexer.push_token(tt)
|
lexer.push_token(tt)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise SyntaxError, "malformed %s entry %s terminated by %s" % (toplevel, entryname, repr(tt))
|
raise NetrcParseError(
|
||||||
|
"malformed %s entry %s terminated by %s"
|
||||||
|
% (toplevel, entryname, repr(tt)),
|
||||||
|
file, lexer.lineno)
|
||||||
elif tt == 'login' or tt == 'user':
|
elif tt == 'login' or tt == 'user':
|
||||||
login = lexer.get_token()
|
login = lexer.get_token()
|
||||||
elif tt == 'account':
|
elif tt == 'account':
|
||||||
|
@ -62,7 +78,8 @@ class netrc:
|
||||||
elif tt == 'password':
|
elif tt == 'password':
|
||||||
password = lexer.get_token()
|
password = lexer.get_token()
|
||||||
else:
|
else:
|
||||||
raise SyntaxError, "bad follower token %s, file %s, line %d"%(tt,file,lexer.lineno)
|
raise NetrcParseError("bad follower token %r" % tt,
|
||||||
|
file, lexer.lineno)
|
||||||
|
|
||||||
def authenticators(self, host):
|
def authenticators(self, host):
|
||||||
"""Return a (user, account, password) tuple for given host."""
|
"""Return a (user, account, password) tuple for given host."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue