Skip to content

tokenize unconditionally emits NL after comment lines & blank lines #61263

Closed as not planned

Description

@takluyver
mannequin
BPO 17061
Nosy @meadori, @takluyver, @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2013-01-28.11:14:28.570>
labels = ['type-feature']
title = 'tokenize unconditionally emits NL after comment lines & blank lines'
updated_at = <Date 2015-10-05.02:10:31.899>
user = 'https://github.com/takluyver'

bugs.python.org fields:

activity = <Date 2015-10-05.02:10:31.899>
actor = 'martin.panter'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2013-01-28.11:14:28.570>
creator = 'takluyver'
dependencies = []
files = []
hgrepos = []
issue_num = 17061
keywords = []
message_count = 4.0
messages = ['180846', '181241', '182034', '252297']
nosy_count = 3.0
nosy_names = ['meador.inge', 'takluyver', 'martin.panter']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue17061'
versions = ['Python 3.6']

Activity

  1. takluyver commented on Jan 28, 2013

    takluyvermannequin
    MannequinAuthor

    The docs describe the NL token as "Token value used to indicate a non-terminating newline. The NEWLINE token indicates the end of a logical line of Python code; NL tokens are generated when a logical line of code is continued over multiple physical lines."

    However, after a comment or a blank line, tokenize emits NL, even when it's not inside a multi-line statement. For example:

    In [15]: for tok in tokenize.generate_tokens(StringIO('#comment\n').readline): print(tok)
    TokenInfo(type=54 (COMMENT), string='#comment', start=(1, 0), end=(1, 8), line='#comment\n')
    TokenInfo(type=55 (NL), string='\n', start=(1, 8), end=(1, 9), line='#comment\n')
    TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')

    This makes it difficult to use tokenize to detect multi-line statements, as we want to do in IPython.

    In my tests so far, changing two instances of NL to NEWLINE in this block (lines 530 & 533) makes it behave as I expect:
    http://hg.python.org/cpython/file/a375c3d88c7e/Lib/tokenize.py#l524

  2. meadori commented on Feb 3, 2013

    @meadori
    Member

    The current behavior seems consistent with the lexical definition for
    blank lines [1]:

    """
    A logical line that contains only spaces, tabs, formfeeds and possibly a
    comment, is ignored (i.e., no NEWLINE token is generated).
    """

    NL and COMMENT are used for items that the CPython tokenizer
    ignores (and are not really tokens). Also, the test suite explicitly
    tests for this case.

    Perhaps the tokenize documentation should be updated
    to say something like:

    """
    NL tokens are generated when a logical line of code is continued over
    multiple physical lines and for blank lines.
    """

    [1] http://docs.python.org/3.4/reference/lexical_analysis.html#blank-lines

  3. added
    type-bug
    on Feb 3, 2013
  4. takluyver commented on Feb 13, 2013

    takluyvermannequin
    MannequinAuthor

    Hmm, that's interesting.

    For our purposes, a blank line or a comment line shouldn't result in a continuation prompt. This is consistent with what the plain Python shell does.

    As part of this, we're tokenizing the code, and if the final \n results in a NL token (instead of NEWLINE), we wait to build a 'Python line'. (Likewise if the final \n doesn't appear before EOFError, indicating that a string continues to the next line). Since tokenize doesn't expose parenlev (parentheses level), my modification to tokenize makes this work as we need.

    Maybe another way forward would be to make parenlev accessible in some way, so that we can use that rather than using NL == parenlev > 0?

  5. vadmium commented on Oct 5, 2015

    @vadmium
    Member

    The plain Python shell does respond to lines with only a comment and/or horizontal space with a continuation prompt. It only treats completely blank lines without any horizontal space specially:

    >>>     
    ... # Indented blank line above; completely blank line below:
    ... 
    >>> 

    Meador: The documentation already says what you proposed: “NL tokens are generated when a logical line of code is continued over multiple physical lines” <https://docs.python.org/dev/library/tokenize.html#tokenize.NL\>.

    Thomas: It sounds like you actually want to differentiate newlines inside bracketed expressions from newlines outside of statements. I think this would require a new feature.

    Also, I noticed that an escaped continued newline doesn’t seem to generate any token at all. Not sure if this is a bug or intended, but it does seem inconsistent with the other uses of the NL token.

    $ ./python -btWall -m tokenize
    1 + \
    1,0-1,1:            NUMBER         '1'            
    1,2-1,3:            OP             '+'            
    1
    2,0-2,1:            NUMBER         '1'            
    2,1-2,2:            NEWLINE        '\n'           
    3,0-3,0:            ENDMARKER      ''
  6. added
    type-feature
    and removed
    type-bug
    on Oct 5, 2015
  7. transferred this issue fromon Apr 10, 2022
  8. lysnikolaou commented on Oct 11, 2023

    @lysnikolaou
    Member

    I am going to close this, since I believe that the case presented here behaves correctly. Also, things have changed a lot since this was opened, both on the CPython and the IPython side.

    Feel free to reopen if you feel differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions