Skip to content

Newline string breaks doctest in four different contexts #104303

Closed as not planned
@pdq-self

Description

@pdq-self

Crash report

Four separate small code samples break doctest because a newline is misinterpreted.
In all four code samples, the failure follows the newline string, "\n", whether it appears
as a string literal or is assigned to a variable.

CONTEXT 1

  • A newline character in a literal string: "\n",
  • in an __init__(),
  • in a class,
  • that is in a docstring,
  • that is inside an outermost class

CAUSES doctest.testmod() to

  1. raise a ValueError exception,
  2. diagnose that line 10 of the docstring has inconsistent leading whitespace: '",' -- when there is no inconsistent leading whitespace in the file --
  3. display a Traceback stack,
  4. return to the shell prompt with status code 1.

TRACEBACK, context 1

Runable code sample 1 contains a traceback.

Error messages, context 1

  • ValueError: line 10 of the docstring for main.F has inconsistent leading whitespace: '",'
  • returned to shell prompt with status code 1.

CONTEXT 2

  • Assigning a newline character in a literal string to a variable
  • in an __init__(),
  • in a class,
  • that is in a docstring,
  • that is inside an outermost class

CAUSES doctest.testmod() to

  1. raise a ValueError exception,
  2. -- NOTE THE LINE NUMBER CHANGE FROM CONTEXT 1--
  3. diagnose that line 5 of the docstring has inconsistent leading whitespace: '",' -- when there is no inconsistent leading whitespace in the file --
  4. display a Traceback stack,
  5. return to the shell prompt with status code 1.

TRACEBACK, context 2

Runable code sample 2 contains a traceback.

Error messages, context 2

  • ValueError: line 5 of the docstring for main.F has inconsistent leading whitespace: '"'
  • returned to shell prompt with status code 1.

CONTEXT 3

  • A newline character in a literal string ("\n"),
  • in a function,
  • that is in a docstring

CAUSES doctest.testmod() to

  1. incorrectly interpret string literal "\n" as a line break,
  2. try a test that was not requested,
  3. incorrectly expect the test to produce the bottom of function in_it(),
  4. raise a SyntaxError exception,
  5. incorrectly detect that line 9 of the docstring has an unterminated string literal -- although there are no unterminated string literals in the file --
  6. display a Traceback stack,
  7. return to the shell prompt with status code 0, as if nothing bad happened.

TRACEBACK, context 3

Runable code sample 3 contains both the try/expecting pair and the traceback.

Error messages, context 3

  • SyntaxError: unterminated string literal (detected at line 9)
  • returned to shell prompt with status code 0.

CONTEXT 4

  • Assigning a newline character in a literal string to a variable
  • in a function,
  • that is in a docstring

CAUSES doctest.testmod() to

  1. incorrectly interpret string literal "\n" as a line break,
  2. try a test that was not requested,
  3. incorrectly expect the test to produce the bottom of function in_it(),
  4. raise a SyntaxError exception,
  5. -- NOTE THE LINE NUMBER CHANGE FROM CONTEXT 3--
  6. incorrectly detect that line 4 of the docstring has an unterminated string literal -- although there are no unterminated string literals in the file --
  7. display a Traceback stack,
  8. return to the shell prompt with status code 0, as if nothing bad happened.

TRACEBACK, context 4

Runable code sample 4 contains both the try/expecting pair and the traceback.

Error messages, context 4

  • SyntaxError: unterminated string literal (detected at line 4)
  • returned to shell prompt with status code 0.

My environment

  • CPython versions tested on: Python 3.11.2 (main, Mar 24 2023, 00:28:48) [Clang 14.0.0 (clang-1400.0.29.202)]
  • Operating system and architecture: macOS 12.6.5, Intel Core i9

codesample1.tgz
codesample2.tgz
codesample3.tgz
codesample4.tgz

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is provided

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions