gpk@bell-labs.com: [Python-bugs-list] netrc module has bad error handling (PR#265)

Eric S. Raymond esr@thyrsus.com
Mon, 3 Apr 2000 19:23:28 -0400


Guido van Rossum <guido@python.org>:
> Eric,
> 
> Perhaps you can answer these two bug reports for your netrc.py module?

Gladly.
 
> --Guido van Rossum (home page: http://www.python.org/~guido/)
> 
> > Subject: [Python-bugs-list] netrc.py rejects hostnames (PR#243)
> > From: lannert@uni-duesseldorf.de
> > To: python-bugs-list@python.org
> > Cc: bugs-py@python.org
> > Date: Tue, 21 Mar 2000 19:07:53 -0500 (EST)
> > Status: O
> > X-Status: 
> > X-Keywords: 
> > X-UID: 276
> > 
> > Hi,
> > 
> > I tried to use netrc.py but it chokes on my ~/.netrc: hostnames of the
> > form spam.uni-duesseldorf.de are rejected because of the '-' character.
> > 
> > The following patch against the CVS version made this module work (for
> > me!):
> > 
> > --- netrc.py~   Wed Mar 22 01:01:33 2000
> > +++ netrc.py    Wed Mar 22 01:02:01 2000
> > @@ -15,7 +15,7 @@
> >          self.hosts = {}
> >          self.macros = {}
> >          lexer = shlex.shlex(fp)
> > -        lexer.wordchars = lexer.wordchars + '.'
> > +        lexer.wordchars = lexer.wordchars + '.-'
> >          while 1:
> >              # Look for a machine, default, or macdef top-level keyword
> >              toplevel = tt = lexer.get_token()
> > 
> > 
> > AFAIK, '-' is a legitimate hostname character; I don't think that this
> > extended acceptance of the lexer could break anything else.

I agree.  I believe this patch is correct; thanks.

> > BTW, the following lines:
> > 
> > 31:                lexer.whitepace = ' \t'
> > 35:                        lexer.whitepace = ' \t\r\n'
> > 
> > look a bit strange; shouldn't it be "whitespace" as defined in shlex?
> > (But then, how did this ever work? :)

This code is correct, though perhaps not as explicit as it should be.

It is a workaround for the fact that macdefs actually have different
lexical rules than the rest of the .netrc format.  If you notice that
the assignment on line 35 is actually restoring the shlex default I
think you'll grok what's happening.  

It's kind of ugly, but that's not my fault :-).  Go pound on whoever 
designed the .netrc format.

> ------- Forwarded Message
> 
> Date:    Sun, 02 Apr 2000 23:39:40 -0400
> From:    gpk@bell-labs.com
> To:      python-bugs-list@python.org
> cc:      bugs-py@python.org
> Subject: [Python-bugs-list] netrc module has bad error handling (PR#265)
> 
> Full_Name: Greg Kochanski
> Version: 1.5.2
> OS: Solaris 2.6
> Submission from: h-135-104-50-27.research.bell-labs.com (135.104.50.27)
> 
> 
> The constructor for netrc.netrc messes up royally
> if the file named file doesn't exist.
> 
> In that case, it attempts to return None
> from the constructor as an error indication,
> rather than throwing an exception.
> Consequently,  self.hosts is not initialized,
> and future attempts to use the class object will
> fail.   You can't usefully return a value from a
> constructor.
> 
> So, the code at the top of netrc.netrc.__init__() should be
> 
> 	fp = open(file)
> 
> rather than
> 
> 	try:
> 		fp = open(file)
> 	except:
> 		return None
> 

You are correct, sir.  I was a rather new Pythoneer when I wrote this;
this is a novice error.

> BTW, Python ought to catch attempts to return values
> from constructors.

Probably so.

Both these patches should be put in the masters by someone with
commit authority.

Guido, this reminds me that I have an updated and improved shlex that
adds the capability to optionally support a file-inclusion construct
at the lexical level, with proper stacking and unstacking of input sources.  
All the user has to do is say

	lexer.include = "include"

or

	lexer.include = "source"

or whatever to declare the inclusion keyword.  The token after it is 
treated as a filename and becomes the input source.

What's the current submission process?
-- 
		<a href="http://www.tuxedo.org/~esr">Eric S. Raymond</a>

If gun laws in fact worked, the sponsors of this type of legislation
should have no difficulty drawing upon long lists of examples of
criminal acts reduced by such legislation. That they cannot do so
after a century and a half of trying -- that they must sweep under the
rug the southern attempts at gun control in the 1870-1910 period, the
northeastern attempts in the 1920-1939 period, the attempts at both
Federal and State levels in 1965-1976 -- establishes the repeated,
complete and inevitable failure of gun laws to control serious crime.
        -- Senator Orrin Hatch, in a 1982 Senate Report