@@ -3070,6 +3070,41 @@ def test_copy(self):
30703070 self .assertEqual (k1 , k2 )
30713071 self .assertEqual (c .flags , d .flags )
30723072
3073+ def test_replace (self ):
3074+ Context = self .decimal .Context
3075+ Inexact = self .decimal .Inexact
3076+ Overflow = self .decimal .Overflow
3077+ ROUND_UP = self .decimal .ROUND_UP
3078+
3079+ c = Context (prec = 10 , Emin = - 99 , capitals = 0 )
3080+ c .flags [Inexact ] = True
3081+ d = copy .replace (c , prec = 20 , rounding = ROUND_UP )
3082+ self .assertEqual (d .prec , 20 )
3083+ self .assertEqual (d .rounding , ROUND_UP )
3084+ # Not replaced attributes are inherited from the original context.
3085+ self .assertEqual (d .Emin , - 99 )
3086+ self .assertEqual (d .capitals , 0 )
3087+ self .assertEqual (d .Emax , c .Emax )
3088+ self .assertEqual (d .clamp , c .clamp )
3089+ self .assertTrue (d .flags [Inexact ])
3090+ self .assertEqual (d .traps , c .traps )
3091+ # The copy is deep and the original context is left unchanged.
3092+ self .assertIsNot (d .flags , c .flags )
3093+ self .assertIsNot (d .traps , c .traps )
3094+ self .assertEqual (c .prec , 10 )
3095+ self .assertEqual (c .rounding , Context ().rounding )
3096+
3097+ # As in the constructor, flags and traps can be given as a list.
3098+ d = copy .replace (c , flags = [Overflow ])
3099+ self .assertTrue (d .flags [Overflow ])
3100+ self .assertFalse (d .flags [Inexact ])
3101+
3102+ self .assertRaises (TypeError , copy .replace , c , prek = 1 )
3103+ self .assertRaises (TypeError , copy .replace , c , prec = 'spam' )
3104+ # Unlike in the constructor, None is not a valid value.
3105+ self .assertRaises (TypeError , copy .replace , c , prec = None )
3106+ self .assertRaises (TypeError , copy .replace , c , flags = None )
3107+
30733108 def test__clamp (self ):
30743109 # In Python 3.2, the private attribute `_clamp` was made
30753110 # public (issue 8540), with the old `_clamp` becoming a
@@ -3763,6 +3798,13 @@ def test_localcontext_kwargs(self):
37633798 self .assertRaises (TypeError , self .decimal .localcontext , Emin = "" )
37643799 self .assertRaises (TypeError , self .decimal .localcontext , Emax = "" )
37653800
3801+ # None is not a valid value for any of these attributes.
3802+ for name in ('prec' , 'rounding' , 'Emin' , 'Emax' , 'capitals' , 'clamp' ,
3803+ 'flags' , 'traps' ):
3804+ with self .subTest (name = name ):
3805+ self .assertRaises (TypeError , self .decimal .localcontext ,
3806+ ** {name : None })
3807+
37663808 def test_local_context_kwargs_does_not_overwrite_existing_argument (self ):
37673809 ctx = self .decimal .getcontext ()
37683810 orig_prec = ctx .prec
0 commit comments