Skip to content

Refactor: Centralize size validation in multiprocessing.heap #137928

@acabelloj

Description

@acabelloj

Feature or enhancement

Proposal:

Currently, the multiprocessing.heap module validates requested memory sizes in two different places:

  • Heap.malloc()
  • BufferWrapper.__init__()

Both duplicate the same logic:

if size < 0:
    raise ValueError("Size {0:n} out of range".format(size))
if sys.maxsize <= size:
    raise OverflowError("Size {0:n} too large".format(size))

Proposed Change

Move this logic into a private static method (Heap._validate_size).

Benefits

  • Removes duplication and ensures consistency.
  • Improves maintainability (future changes affect only one place).
  • Binds sys.maxsize at definition time (_maxsize=sys.maxsize) to avoid repeated global lookups.

Impact

  • No behavior changes. This change does not alter functionality or the external API of multiprocessing. It only removes duplication and ensures future modifications to size validation remain consistent.
  • Existing multiprocessing tests already cover these code paths.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions