331
+ _set_printoptions (precision , threshold , edgeitems , linewidth , suppress ,
332
+ nanstr , infstr , formatter , sign , floatmode , legacy )
304
333
305
334
306
335
@set_module ('numpy' )
@@ -330,7 +359,7 @@ def get_printoptions():
330
359
set_printoptions, printoptions
331
360
332
361
"""
333
- opts = _format_options .copy ()
362
+ opts = _format_options .get (). copy ()
334
363
opts ['legacy' ] = {
335
364
113 : '1.13' , 121 : '1.21' , 125 : '1.25' , sys .maxsize : False ,
336
365
}[opts ['legacy' ]]
@@ -339,7 +368,7 @@ def get_printoptions():
339
368
340
369
def _get_legacy_print_mode ():
341
370
"""Return the legacy print mode as an int."""
342
- return _format_options ['legacy' ]
371
+ return _format_options . get () ['legacy' ]
343
372
344
373
345
374
@set_module ('numpy' )
@@ -369,12 +398,12 @@ def printoptions(*args, **kwargs):
369
398
set_printoptions, get_printoptions
370
399
371
400
"""
372
- opts = np .get_printoptions ()
373
401
try :
374
- np . set_printoptions (* args , ** kwargs )
375
- yield np . get_printoptions ()
402
+ token = _set_printoptions (* args , ** kwargs )
403
+ yield get_printoptions ()
376
404
finally :
377
- np .set_printoptions (** opts )
405
+ _format_options .reset (token )
406
+ _set_legacy_print_mode (_format_options .get ())
378
407
379
408
380
409
def _leading_trailing (a , edgeitems , index = ()):
@@ -732,7 +761,7 @@ def array2string(a, max_line_width=None, precision=None,
732
761
overrides = _make_options_dict (precision , threshold , edgeitems ,
733
762
max_line_width , suppress_small , None , None ,
734
763
sign , formatter , floatmode , legacy )
735
- options = _format_options .copy ()
764
+ options = _format_options .get (). copy ()
736
765
options .update (overrides )
737
766
738
767
if options ['legacy' ] <= 113 :
@@ -1037,8 +1066,8 @@ def fillFormat(self, data):
1037
1066
# if there are non-finite values, may need to increase pad_left
1038
1067
if data .size != finite_vals .size :
1039
1068
neginf = self .sign != '-' or any (data [isinf (data )] < 0 )
1040
- nanlen = len (_format_options ['nanstr' ])
1041
- inflen = len (_format_options ['infstr' ]) + neginf
1069
+ nanlen = len (_format_options . get () ['nanstr' ])
1070
+ inflen = len (_format_options . get () ['infstr' ]) + neginf
1042
1071
offset = self .pad_right + 1 # +1 for decimal pt
1043
1072
self .pad_left = max (
1044
1073
self .pad_left , nanlen - offset , inflen - offset
@@ -1049,10 +1078,10 @@ def __call__(self, x):
1049
1078
with errstate (invalid = 'ignore' ):
1050
1079
if np .isnan (x ):
1051
1080
sign = '+' if self .sign == '+' else ''
1052
- ret = sign + _format_options ['nanstr' ]
1081
+ ret = sign + _format_options . get () ['nanstr' ]
1053
1082
else : # isinf
1054
1083
sign = '-' if x < 0 else '+' if self .sign == '+' else ''
1055
- ret = sign + _format_options ['infstr' ]
1084
+ ret = sign + _format_options . get () ['infstr' ]
1056
1085
return ' ' * (
1057
1086
self .pad_left + self .pad_right + 1 - len (ret )
1058
1087
) + ret
@@ -1443,10 +1472,10 @@ def _void_scalar_to_string(x, is_repr=True):
1443
1472
scalartypes.c.src code, and is placed here because it uses the elementwise
1444
1473
formatters defined above.
1445
1474
"""
1446
- options = _format_options .copy ()
1475
+ options = _format_options .get (). copy ()
1447
1476
1448
1477
if options ["legacy" ] <= 125 :
1449
- return StructuredVoidFormat .from_data (array (x ), ** _format_options )(x )
1478
+ return StructuredVoidFormat .from_data (array (x ), ** _format_options . get () )(x )
1450
1479
1451
1480
if options .get ('formatter' ) is None :
1452
1481
options ['formatter' ] = {}
@@ -1490,7 +1519,7 @@ def dtype_is_implied(dtype):
1490
1519
array([1, 2, 3], dtype=int8)
1491
1520
"""
1492
1521
dtype = np .dtype (dtype )
1493
- if _format_options ['legacy' ] <= 113 and dtype .type == np .bool :
1522
+ if _format_options . get () ['legacy' ] <= 113 and dtype .type == np .bool :
1494
1523
return False
1495
1524
1496
1525
# not just void types can be structured, and names are not part of the repr
@@ -1541,7 +1570,7 @@ def _array_repr_implementation(
1541
1570
array2string = array2string ):
1542
1571
"""Internal version of array_repr() that allows overriding array2string."""
1543
1572
if max_line_width is None :
1544
- max_line_width = _format_options ['linewidth' ]
1573
+ max_line_width = _format_options . get () ['linewidth' ]
1545
1574
1546
1575
if type (arr ) is not ndarray :
1547
1576
class_name = type (arr ).__name__
@@ -1553,7 +1582,7 @@ def _array_repr_implementation(
1553
1582
prefix = class_name + "("
1554
1583
suffix = ")" if skipdtype else ","
1555
1584
1556
- if (_format_options ['legacy' ] <= 113 and
1585
+ if (_format_options . get () ['legacy' ] <= 113 and
1557
1586
arr .shape == () and not arr .dtype .names ):
1558
1587
lst = repr (arr .item ())
1559
1588
elif arr .size > 0 or arr .shape == (0 ,):
@@ -1574,7 +1603,7 @@ def _array_repr_implementation(
1574
1603
# Note: This line gives the correct result even when rfind returns -1.
1575
1604
last_line_len = len (arr_str ) - (arr_str .rfind ('\n ' ) + 1 )
1576
1605
spacer = " "
1577
- if _format_options ['legacy' ] <= 113 :
1606
+ if _format_options . get () ['legacy' ] <= 113 :
1578
1607
if issubclass (arr .dtype .type , flexible ):
1579
1608
spacer = '\n ' + ' ' * len (class_name + "(" )
1580
1609
elif last_line_len + len (dtype_str ) + 1 > max_line_width :
@@ -1648,7 +1677,7 @@ def _array_str_implementation(
1648
1677
a , max_line_width = None , precision = None , suppress_small = None ,
1649
1678
array2string = array2string ):
1650
1679
"""Internal version of array_str() that allows overriding array2string."""
1651
- if (_format_options ['legacy' ] <= 113 and
1680
+ if (_format_options . get () ['legacy' ] <= 113 and
1652
1681
a .shape == () and not a .dtype .names ):
1653
1682
return str (a .item ())
1654
1683
0 commit comments