Skip to content

gh-143201: Remove unused truncated variable from warnings - #157043

Open
AZero13 wants to merge 1 commit into
python:mainfrom
AZero13:trunc
Open

gh-143201: Remove unused truncated variable from warnings#157043
AZero13 wants to merge 1 commit into
python:mainfrom
AZero13:trunc

Conversation

@AZero13

@AZero13 AZero13 commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

It is not used and hasn't been, so we can remove it.

@AZero13 AZero13 changed the title (NFC) Remove unused truncated variable. (NFC) Remove unused truncated variable from warnings Sep 6, 2026
Comment thread Python/_warnings.c
@picnixz picnixz changed the title (NFC) Remove unused truncated variable from warnings gh-143201: Remove unused truncated variable from warnings Sep 6, 2026
It is not used and hasn't been, so we can remove it.

Remove i, len, and data too.

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on just removing.

cc: @vstinner who last worked on this code as part of bpo-19424 (a4c704b). Looks like that was a translation of older code.

@cmaloney

cmaloney commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

@AZero13 please do not force push to CPython PR branches: https://devguide.python.org/getting-started/pull-request-lifecycle/#don-t-force-push

@vstinner

vstinner commented Sep 7, 2026

Copy link
Copy Markdown
Member

The change is not correct: the function must skip the indentation.

I introduced a bug in a4c704b (in 2013, 13 years ago!): PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW) should be PyFile_WriteObject(truncated, f_stderr, Py_PRINT_RAW) instead.

Example:

import sys
import warnings

# Force the usage of the C implementation
del warnings._showwarnmsg

def current_lineno():
    return sys._getframe(1).f_lineno

def func():
              # looooong indentation
              lineno = current_lineno() + 1
              warnings.warn_explicit('text', Warning,
                                     __file__, lineno,
                                     module_globals=globals())

func()

The output is:

$ ./python x.py 
/home/vstinner/python/main/x.py:13: Warning: text
              warnings.warn_explicit('text', Warning,

The long indentation is not replaced with 2 spaces.

If I comment del warnings._showwarnmsg line, the indentation is replaced with 2 spaces as expected:

$ ./python x.py 
/home/vstinner/python/main/x.py:13: Warning: text
  warnings.warn_explicit('text', Warning,

Well, IMO the first problem here is that the C implementation (C show_warning() function) is not covered by tests.

@cmaloney

cmaloney commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

@AZero13 could you update this PR for vstinner's requested changes? He's a lot more authoratative on this code than I am. Sorry for leading you back and forth.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants