diff -r b49e366556ba Lib/test/test_tracemalloc.py --- a/Lib/test/test_tracemalloc.py Mon Jun 02 14:43:24 2014 +0200 +++ b/Lib/test/test_tracemalloc.py Mon Jun 02 16:44:21 2014 +0300 @@ -3,6 +3,7 @@ import sys import tracemalloc import unittest +import io from unittest.mock import patch from test.script_helper import assert_python_ok, assert_python_failure from test import support @@ -286,6 +287,13 @@ exitcode = os.WEXITSTATUS(status) self.assertEqual(exitcode, 0) + def test_floating_point_error(self): + # This should not crash. The changeset 5b0fda8f5718 introduced + # a regression, which leads to a Floating point exception. + with io.BytesIO(): + pass + with io.StringIO(): + pass class TestSnapshot(unittest.TestCase): maxDiff = 4000 diff -r b49e366556ba Modules/_tracemalloc.c --- a/Modules/_tracemalloc.c Mon Jun 02 14:43:24 2014 +0200 +++ b/Modules/_tracemalloc.c Mon Jun 02 16:44:21 2014 +0300 @@ -477,8 +477,9 @@ { PyMemAllocator *alloc = (PyMemAllocator *)ctx; void *ptr; - - assert(nelem <= PY_SIZE_MAX / elsize); + + if (elsize != 0) + assert(nelem <= PY_SIZE_MAX / elsize); if (use_calloc) ptr = alloc->calloc(alloc->ctx, nelem, elsize);