Skip to content

window.insnstr() leaks its bytes argument when setting attr fails #156953

Description

@fedonman

Bug description:

window.insnstr(str, n, attr) sets the window rendition before the write, and when that fails it never releases the bytes object it converted its argument to, so every failing call leaks one reference. addstr(), addnstr() and insstr() got that release in GH-145609 (gh-145376); insnstr() was missed.

A window detached by screen.close() makes the rendition call fail:

import curses, sys

screen = curses.newterm()
win = screen.stdscr
screen.close()
data = b'x' * 40
before = sys.getrefcount(data)
for _ in range(100):
    try:
        win.insnstr(data, 8, curses.A_BOLD)
    except curses.error:
        pass
print('insnstr leaked', sys.getrefcount(data) - before, 'references', file=sys.stderr)
insnstr leaked 100 references

Expected: insnstr leaked 0 references.

Only main is affected: 3.15 has the same omission, but nothing there detaches a window, so the rendition call cannot fail.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

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

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions