Title: `netrc` emits syntax errors for comments after blank lines · Issue #104306 · python/cpython · GitHub
Open Graph Title: `netrc` emits syntax errors for comments after blank lines · Issue #104306 · python/cpython
X Title: `netrc` emits syntax errors for comments after blank lines · Issue #104306 · python/cpython
Description: 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...
Open Graph Description: 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 n...
X Description: 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 n...
Opengraph URL: https://github.com/python/cpython/issues/104306
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`netrc` emits syntax errors for comments after blank lines","articleBody":"# Bug report\r\n\r\n`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.\r\n\r\nFor example:\r\n\r\n```python\r\nimport netrc\r\n\r\nfilename = 'netrctest'\r\n\r\nwith open(filename, 'w') as fl:\r\n print(\"\"\"\r\n# HTTP\r\nmachine www.example.com login myuser password mypass\r\n\r\n# FTP\r\nmachine ftp.example.com login myuser password mypass\r\n\"\"\", file=fl)\r\n\r\nprint(netrc.netrc(filename))\r\n```\r\nproduces\r\n```console\r\n$ python netrc-test.py \r\nTraceback (most recent call last):\r\n File \"/home/lukeshu/netrc-test.py\", line 14, in \u003cmodule\u003e\r\n print(netrc.netrc(filename))\r\n ^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/netrc.py\", line 31, in __init__\r\n self._parse(file, fp, default_netrc)\r\n File \"/usr/lib/python3.11/netrc.py\", line 66, in _parse\r\n raise NetrcParseError(\r\nnetrc.NetrcParseError: bad toplevel token 'HTTP' (netrctest, line 3)\r\n```\r\n\r\nThis issue was made worse by https://github.com/python/cpython/pull/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:\r\n\r\n```python\r\nimport netrc\r\n\r\nfilename = 'netrctest'\r\n\r\nwith open(filename, 'w') as fl:\r\n print(\"\"\"# HTTP\r\nmachine www.example.com login myuser password mypass\r\n\r\n# FTP\r\nmachine ftp.example.com login myuser password mypass\r\n\"\"\", file=fl)\r\n\r\nprint(netrc.netrc(filename))\r\n```\r\nBefore we got:\r\n```console\r\n$ python netrc-test.py \r\nmachine www.example.com\r\n login myuser\r\n password mypass\r\nmachine ftp.example.com\r\n login myuser\r\n password mypass\r\n\r\n```\r\nbut now in 3.11 we get:\r\n```console\r\n$ python netrc-test.py \r\nTraceback (most recent call last):\r\n File \"/home/lukeshu/netrc-test.py\", line 13, in \u003cmodule\u003e\r\n print(netrc.netrc(filename))\r\n ^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/netrc.py\", line 75, in __init__\r\n self._parse(file, fp, default_netrc)\r\n File \"/usr/lib/python3.11/netrc.py\", line 140, in _parse\r\n raise NetrcParseError(\"bad follower token %r\" % tt,\r\nnetrc.NetrcParseError: bad follower token 'FTP' (netrctest, line 4)\r\n```\r\n\r\n# Your environment\r\n\r\n- CPython versions tested on: 3.11.3\r\n- Operating system and architecture: Parabola GNU/Linux-libre (like Arch Linux) on x86_64.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104511\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/LukeShu","@type":"Person","name":"LukeShu"},"datePublished":"2023-05-08T20:06:05.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/104306/cpython/issues/104306"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c2148407-2531-84f8-8c8a-2f5cee0efe4e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E184:1DECF9:B53834:ED5716:696AFB3F |
| html-safe-nonce | 0c99425663a50957d830515f9fa1494c11957ca1ec74b170ece3638e98e42431 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTg0OjFERUNGOTpCNTM4MzQ6RUQ1NzE2OjY5NkFGQjNGIiwidmlzaXRvcl9pZCI6IjY0MzYwOTM1NDM3NjcwMTQyMDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d705aa05ae6e7b73d26759a83df5f67eb05d5eaf82c501a6fe77ac297a19437f |
| hovercard-subject-tag | issue:1700856057 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/104306/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e11bfafae94c0bfd63a5fd3a1504a825cd85c8fb5a26db22b439ba84bd420c08/python/cpython/issues/104306 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e11bfafae94c0bfd63a5fd3a1504a825cd85c8fb5a26db22b439ba84bd420c08/python/cpython/issues/104306 |
| og:image:alt | 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 n... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LukeShu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width