From 80013f443ef6429a313b4fce291002999057541c 2019-03-14 17:29:21 From: Matan Gover Date: 2019-03-14 17:29:21 Subject: [PATCH] Fix Audio from data without numpy on Python 3. --- diff --git a/IPython/lib/display.py b/IPython/lib/display.py index a163b6a..a58094d 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -159,16 +159,12 @@ class Audio(DisplayObject): @staticmethod def _validate_and_normalize_without_numpy(data): - # check that it is a "1D" list - idata = iter(data) # fails if not an iterable try: - iter(idata.next()) + maxabsvalue = float(max([abs(x) for x in data])) + except TypeError: raise TypeError('Only lists of mono audio are ' 'supported if numpy is not installed') - except TypeError: - # this means it's not a nested list, which is what we want - pass - maxabsvalue = float(max([abs(x) for x in data])) + scaled = [int(x/maxabsvalue*32767) for x in data] nchan = 1 return scaled, nchan diff --git a/IPython/lib/tests/test_display.py b/IPython/lib/tests/test_display.py index 9a466a9..fa52ff4 100644 --- a/IPython/lib/tests/test_display.py +++ b/IPython/lib/tests/test_display.py @@ -19,6 +19,7 @@ try: import pathlib except ImportError: pass +from unittest import mock # Third-party imports import nose.tools as nt @@ -186,6 +187,17 @@ def test_audio_from_file(): def test_audio_from_numpy_array(): display.Audio(get_test_tone(), rate=44100) +def test_audio_from_list_without_numpy(): + # Simulate numpy not installed. + with mock.patch('numpy.array', side_effect=ImportError): + display.Audio(list(get_test_tone()), rate=44100) + +def test_audio_from_list_without_numpy_raises_for_nested_list(): + # Simulate numpy not installed. + with mock.patch('numpy.array', side_effect=ImportError): + stereo_signal = [list(get_test_tone())] * 2 + nt.assert_raises(TypeError, lambda: display.Audio(stereo_signal, rate=44100)) + def test_audio_from_numpy_array_without_rate_raises(): nt.assert_raises(ValueError, display.Audio, get_test_tone())