comparison roundup/scripts/roundup_mailgw.py @ 8522:82fc69e6d9d7

refactor/bug: handle case where netrc returns None. pyrefly flagged this. Python 3.14 (and 2.7) says authenticators() returns None if there is no matching host. Also the original exceptions IOError and TypeError appear to not be valid in this context. Rewrite to scope FileNotFoundError if .netrc is missing to just the one method call. Handle rest of flow with if/else not exception jump.
author John Rouillard <rouilj@ieee.org>
date Thu, 19 Feb 2026 22:15:12 -0500
parents 24e867f846dd
children dbe30d5032b8
comparison
equal deleted inserted replaced
8521:24e867f846dd 8522:82fc69e6d9d7
213 if source == 'mailbox': 213 if source == 'mailbox':
214 return handler.do_mailbox(specification) 214 return handler.do_mailbox(specification)
215 215
216 # the source will be a network server, so obtain the credentials to 216 # the source will be a network server, so obtain the credentials to
217 # use in connecting to the server 217 # use in connecting to the server
218 authenticator = None
218 try: 219 try:
219 # attempt to obtain credentials from a ~/.netrc file 220 # attempt to obtain credentials from a ~/.netrc file
221 # returns None if host not found
220 authenticator = netrc.netrc().authenticators(specification) 222 authenticator = netrc.netrc().authenticators(specification)
223 except FileNotFoundError:
224 # FileNotFoundError if no ~/.netrc file
225 pass
226
227 if authenticator:
221 username = authenticator[0] 228 username = authenticator[0]
222 password = authenticator[2] 229 password = authenticator[2]
223 server = specification 230 server = specification
224 # IOError if no ~/.netrc file, TypeError if the hostname 231 else:
225 # not found in the ~/.netrc file:
226 except (IOError, TypeError):
227 match = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', 232 match = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
228 specification) 233 specification)
229 if match: 234 if match:
230 username = match.group('user') 235 username = match.group('user')
231 password = match.group('pass') 236 password = match.group('pass')

Roundup Issue Tracker: http://roundup-tracker.org/