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: tokenize.untokenize does not maintain the order of tabbed indentations and leading spaces
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Blake Hilliard, cryvate, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-11-18 21:13 by Blake Hilliard, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg254857 - (view) Author: Blake Hilliard (Blake Hilliard) Date: 2015-11-18 21:13
Here's a simple test case that shows the bug:

# python
Python 2.7.9 (default, Apr  2 2015, 14:49:18) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import StringIO, tokenize
>>> code = "if False:\n\tx=3\n \ty=3\n"
>>> g = list(tokenize.generate_tokens(StringIO.StringIO(code).readline))
>>> assert tokenize.untokenize(g) == code
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError


This is very similar to: https://bugs.python.org/issue20387

I'm using the patch that was provided to fix that bug, but with the patch this slightly different test case still fails. On the last line of "code" from my example, where the line begins with a single space then a tab, the untokenized string reverses the order, and the tab precedes the space. This generates code that will throw an IndentationError if run.
msg360945 - (view) Author: Henk-Jaap Wagenaar (cryvate) * Date: 2020-01-29 10:05
Python 2.7 is end-of-life, see e.g. https://www.python.org/doc/sunset-python-2/ so this is unlikely to be fixed.

This behaviour continues on Python 3 it seems (I used 3.8.1), *however* on Python 3 this is not valid code as it mixes spaces and tabs, see e.g. https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces

I think therefore this can be closed, though I might well be mistaken.
msg370484 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-31 15:21
Python 2.7 is no longer supported.
History
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69847
2020-05-31 15:21:12serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg370484

resolution: out of date
stage: resolved
2020-01-29 10:05:47cryvatesetnosy: + cryvate
messages: + msg360945
2020-01-29 03:23:05josephgordonsetnosy: - josephgordon
2015-12-22 08:28:29josephgordonsetnosy: + josephgordon
2015-11-18 21:13:27Blake Hilliardcreate