Skip to content

netrc emits syntax errors for comments after blank lines #104306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
LukeShu opened this issue May 8, 2023 · 1 comment
Open

netrc emits syntax errors for comments after blank lines #104306

LukeShu opened this issue May 8, 2023 · 1 comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@LukeShu
Copy link

LukeShu commented May 8, 2023

Bug report

netrc.netrc() emits a syntax error if it encounters a comment after a blank line. Prior to the 3.11, it only did this if the comment was the first non-whitespace thing in the file, but now with 3.11 it does it for all comments.

For example:

import netrc

filename = 'netrctest'

with open(filename, 'w') as fl:
    print("""
# HTTP
machine www.example.com login myuser password mypass

# FTP
machine ftp.example.com login myuser password mypass
""", file=fl)

print(netrc.netrc(filename))

produces

$ python netrc-test.py 
Traceback (most recent call last):
  File "/home/lukeshu/netrc-test.py", line 14, in <module>
    print(netrc.netrc(filename))
          ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/netrc.py", line 31, in __init__
    self._parse(file, fp, default_netrc)
  File "/usr/lib/python3.11/netrc.py", line 66, in _parse
    raise NetrcParseError(
netrc.NetrcParseError: bad toplevel token 'HTTP' (netrctest, line 3)

This issue was made worse by #26330. Prior to that change (prior to 3.11), it only triggered on the if the comment was the first thing in the file. For example, if we remove the leading newline:

import netrc

filename = 'netrctest'

with open(filename, 'w') as fl:
    print("""# HTTP
machine www.example.com login myuser password mypass

# FTP
machine ftp.example.com login myuser password mypass
""", file=fl)

print(netrc.netrc(filename))

Before we got:

$ python netrc-test.py 
machine www.example.com
        login myuser
        password mypass
machine ftp.example.com
        login myuser
        password mypass

but now in 3.11 we get:

$ python netrc-test.py 
Traceback (most recent call last):
  File "/home/lukeshu/netrc-test.py", line 13, in <module>
    print(netrc.netrc(filename))
          ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/netrc.py", line 75, in __init__
    self._parse(file, fp, default_netrc)
  File "/usr/lib/python3.11/netrc.py", line 140, in _parse
    raise NetrcParseError("bad follower token %r" % tt,
netrc.NetrcParseError: bad follower token 'FTP' (netrctest, line 4)

Your environment

  • CPython versions tested on: 3.11.3
  • Operating system and architecture: Parabola GNU/Linux-libre (like Arch Linux) on x86_64.

Linked PRs

@LukeShu LukeShu added the type-bug An unexpected behavior, bug, or error label May 8, 2023
@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label May 9, 2023
sleiderr added a commit to sleiderr/cpython that referenced this issue May 15, 2023
sleiderr added a commit to sleiderr/cpython that referenced this issue May 16, 2023
sleiderr added a commit to sleiderr/cpython that referenced this issue May 29, 2023
@ssbarnea
Copy link
Contributor

ssbarnea commented Jan 4, 2025

I recently found out that ~/.netrc files do not officially support comments and even worse, if you add # prefix to a line, netrc will still load that line and attempt to use that entry!

In other cased it might produce a syntax error but the current behavior is clearly messed badly.

Curl itself seems to treat lines prefixed with # as commented, but not python. Python will somehow load them incorrectly. I tried with a github.com entry and while the entry is correct by default and used by github/python to successfully download, once I add # prefix, curl continues to work (using non authenticated calls), python will fail, likely because is sending an incorrect request to github.

I suspect, it might be fixed by #104511

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants