Skip to content

addstr no longer restores the window attributes when the write fails #155974

Description

@fedonman

Bug description:

window.addstr(y, x, s, attr) sets the window rendition to attr for the write and restores the previous one afterwards. Since 3.15 the restore is skipped when the write itself fails, so the window keeps the caller's attr and whatever the application had set with attrset() is gone. addnstr(), insstr() and insnstr() behave the same way.

import curses

def check(stdscr):
    win = curses.newwin(2, 10, 0, 0)
    win.attrset(curses.A_UNDERLINE)
    try:
        win.addstr(100, 0, 'x', curses.A_BOLD)   # outside the window: raises
    except curses.error:
        pass
    win.addstr(0, 0, 'y')                        # no attr argument
    return win.inch(0, 0) & curses.A_ATTRIBUTES

print('want', hex(curses.A_UNDERLINE), 'got', hex(curses.wrapper(check)))
$ python3.14 repro.py
want 0x20000 got 0x20000
$ ./python repro.py
want 0x20000 got 0x200000

Expected: got 0x20000 on both, the rendition the caller set with attrset().

This changed in 30dde1e (gh-133579), which moved the restore below a new early return on the error path.

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions