netrc.__repr__() says it dumps the data "in the format of a .netrc file", but it writes every value as is with no quoting. So the output only parses back if every value is a single non empty word.
The simplest case is an entry with no login, which is pretty normal for token based logins:
The empty login gets written as login followed by nothing, so the parser takes password as the login and then chokes on hunter2.
Same thing with other values, i tried a few as the password:
"pass word" -> NetrcParseError: bad follower token 'word'
"pass\tword" -> NetrcParseError
"pass\nword" -> NetrcParseError
" pass" -> reads back as 'pass'
"pass\word" -> reads back as 'password'
The last two are the worst ones since nothing fails, you just get a different password back.
The reader in the same file already supports quoted values and backslash escapes (_netrclex.get_token), the writer just never uses them. Looks like this goes back to GH-2491 (bpo-30806, #74989). Before that __repr__ used repr() for the values and that fix replaced it with plain f-strings, with the commit message saying "netrc file format doesn't support quotes and escapes". But _netrclex does support both, so the Python quotes got removed and nothing replaced them.
That fix also added a round trip check to the tests:
but it was dropped in GH-26330 (15409c7) when test_default was rewritten, and nothing in test_netrc.py calls __repr__ now.
A fix would be to quote a value when its empty or has whitespace, " or \ in it, and escape \ and " inside the quotes the way _netrclex reads them. One thing to keep in mind is #99080 (backslashes outside quotes get dropped by the parser), if that changes how backslashes are read the escaping here would have to match.
I have a patch with a round trip test for all the cases above, happy to open a PR.
Activity