diff --git a/Lib/multiprocessing/heap.py b/Lib/multiprocessing/heap.py index 6217dfe12689b3..f1d74d245101c0 100644 --- a/Lib/multiprocessing/heap.py +++ b/Lib/multiprocessing/heap.py @@ -20,6 +20,12 @@ __all__ = ['BufferWrapper'] +# +# Module constants +# + +_MAXSIZE = sys.maxsize + # # Inheritable class which wraps an mmap, and from which blocks can be allocated # @@ -108,6 +114,17 @@ def rebuild_arena(size, dupfd): reduction.register(Arena, reduce_arena) +# +# Validation helpers +# + +def _validate_size(size): + """Validate requested size; raise ValueError if < 0, OverflowError if >= _MAXSIZE.""" + if size < 0: + raise ValueError("Size {0:n} out of range".format(size)) + if _MAXSIZE <= size: + raise OverflowError("Size {0:n} too large".format(size)) + # # Class allowing allocation of chunks of memory from arenas # @@ -295,10 +312,7 @@ def free(self, block): def malloc(self, size): # return a block of right size (possibly rounded up) - 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)) + _validate_size(size) if os.getpid() != self._lastpid: self.__init__() # reinitialize after fork with self._lock: @@ -324,10 +338,7 @@ class BufferWrapper(object): _heap = Heap() def __init__(self, size): - 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)) + _validate_size(size) block = BufferWrapper._heap.malloc(size) self._state = (block, size) util.Finalize(self, BufferWrapper._heap.free, args=(block,))