From 816978d4f17eaf015a8b5f5d6fcabd534eff6019 Mon Sep 17 00:00:00 2001 From: Tom Bird Date: Thu, 21 Jul 2016 11:35:22 +0100 Subject: [PATCH 1/2] BUG: Issue #7852. raising TypeError from set_printoptions for incorrect first arg type --- numpy/core/arrayprint.py | 5 +++++ numpy/core/tests/test_arrayprint.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 282fbd1cfb7f..2a97c68032fd 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -59,6 +59,9 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, ---------- precision : int, optional Number of digits of precision for floating point output (default 8). + We type check this argument, as it is the first argument and we wish to + catch the common mistake of passing an unpacked dict (which will later + cause print() to fail with an opaque error). threshold : int, optional Total number of array elements which trigger summarization rather than full repr (default 1000). @@ -161,6 +164,8 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, if edgeitems is not None: _summaryEdgeItems = edgeitems if precision is not None: + if not isinstance(precision, int): + raise TypeError("set_printoptions requires the argument 'precision' to be of type int") _float_output_precision = precision if suppress is not None: _float_output_suppress_small = not not suppress diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 5759a098496c..754cceb3e8ff 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -7,7 +7,7 @@ import numpy as np from numpy.compat import sixu from numpy.testing import ( - TestCase, run_module_suite, assert_, assert_equal + TestCase, run_module_suite, assert_, assert_equal, assert_raises ) class TestArrayRepr(object): @@ -157,6 +157,10 @@ def test_formatter_reset(self): np.set_printoptions(formatter={'float_kind':None}) assert_equal(repr(x), "array([ 0., 1., 2.])") + def test_type_check(self): + d = {} + assert_raises(TypeError, np.set_printoptions, d) + def test_unicode_object_array(): import sys if sys.version_info[0] >= 3: From 743d8468697044dc2f338d4650576b0227a0fd64 Mon Sep 17 00:00:00 2001 From: Tom Bird Date: Thu, 28 Jul 2016 08:42:12 +0100 Subject: [PATCH 2/2] BUG: #7852. Add type checking for args of set_printoptions --- numpy/core/arrayprint.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 2a97c68032fd..ba5fb607ce7b 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -23,6 +23,7 @@ datetime_data) from .fromnumeric import ravel from .numeric import asarray +import operator if sys.version_info[0] >= 3: _MAXINT = sys.maxsize @@ -59,9 +60,6 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, ---------- precision : int, optional Number of digits of precision for floating point output (default 8). - We type check this argument, as it is the first argument and we wish to - catch the common mistake of passing an unpacked dict (which will later - cause print() to fail with an opaque error). threshold : int, optional Total number of array elements which trigger summarization rather than full repr (default 1000). @@ -158,21 +156,19 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, global _formatter if linewidth is not None: - _line_width = linewidth + _line_width = operator.index(linewidth) if threshold is not None: - _summaryThreshold = threshold + _summaryThreshold = operator.index(threshold) if edgeitems is not None: - _summaryEdgeItems = edgeitems + _summaryEdgeItems = operator.index(edgeitems) if precision is not None: - if not isinstance(precision, int): - raise TypeError("set_printoptions requires the argument 'precision' to be of type int") - _float_output_precision = precision + _float_output_precision = operator.index(precision) if suppress is not None: _float_output_suppress_small = not not suppress if nanstr is not None: - _nan_str = nanstr + _nan_str = str(nanstr) if infstr is not None: - _inf_str = infstr + _inf_str = str(infstr) _formatter = formatter def get_printoptions():