This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.get_source_segment behaviour with missing location info doesn't match docstring
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, benjamin.peterson, brett.cannon, hauntsaninja, iritkatriel, pablogsal, yselivanov
Priority: normal Keywords: 3.9regression

Created on 2020-05-17 17:35 by iritkatriel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20155 closed iritkatriel, 2020-05-17 17:35
PR 20157 merged iritkatriel, 2020-05-17 18:05
Messages (3)
msg369139 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-05-17 17:35
The doctoring says "If some location information (lineno, end_lineno, col_offset, or end_col_offset) is missing, return None."

However:

>>> s = "12"
>>> node = ast.parse(s).body[0]
>>> ast.get_source_segment(s, node)
'12'
>>> del node.lineno
>>> ast.get_source_segment(s, node)
>>> node = ast.parse(s).body[0]
>>> del node.end_lineno
>>> ast.get_source_segment(s, node)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/iritkatriel/src/cpython/Lib/ast.py", line 336, in get_source_segment
    end_lineno = node.end_lineno - 1
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

In other parts of ast.py there are hasattr checks for lineno/col_offset alongside checks for None for end_lineno/end_col_offset. So this difference in behaviour of the end_ fields seems intended.
msg369155 - (view) Author: Shantanu (hauntsaninja) * Date: 2020-05-17 23:16
The code works on 3.8 for me, but has regressed on 3.9 master. Looks like this is caused by https://bugs.python.org/issue36287 (https://github.com/python/cpython/pull/18843)
msg369282 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-05-18 18:14
New changeset e6578a226d8a8a13d1062d154fad0fef28ee2416 by Irit Katriel in branch 'master':
bpo-40662: Fixed ast.get_source_segment for ast nodes that have incomplete location information (GH-20157)
https://github.com/python/cpython/commit/e6578a226d8a8a13d1062d154fad0fef28ee2416
History
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 84839
2020-05-18 18:17:15pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-05-18 18:14:19pablogsalsetmessages: + msg369282
2020-05-18 17:46:42pablogsalsetkeywords: + 3.9regression, - patch
2020-05-17 23:16:49hauntsaninjasetnosy: + hauntsaninja
messages: + msg369155
2020-05-17 18:05:48iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request19460
2020-05-17 17:55:08iritkatrielsettitle: ast.get_source_segment behaviour with missing location info doesn't match doctoring -> ast.get_source_segment behaviour with missing location info doesn't match docstring
2020-05-17 17:39:43BTaskayasetnosy: + BTaskaya
2020-05-17 17:35:18iritkatrielcreate