MCPcopy Index your code
hub / github.com/python/cpython / test_context_subclassing

Method test_context_subclassing

Lib/test/test_decimal.py:4309–4414  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4307class ContextSubclassing:
4308
4309 def test_context_subclassing(self):
4310 decimal = self.decimal
4311 Decimal = decimal.Decimal
4312 Context = decimal.Context
4313 Clamped = decimal.Clamped
4314 DivisionByZero = decimal.DivisionByZero
4315 Inexact = decimal.Inexact
4316 Overflow = decimal.Overflow
4317 Rounded = decimal.Rounded
4318 Subnormal = decimal.Subnormal
4319 Underflow = decimal.Underflow
4320 InvalidOperation = decimal.InvalidOperation
4321
4322 class MyContext(Context):
4323 def __init__(self, prec=None, rounding=None, Emin=None, Emax=None,
4324 capitals=None, clamp=None, flags=None,
4325 traps=None):
4326 Context.__init__(self)
4327 if prec is not None:
4328 self.prec = prec
4329 if rounding is not None:
4330 self.rounding = rounding
4331 if Emin is not None:
4332 self.Emin = Emin
4333 if Emax is not None:
4334 self.Emax = Emax
4335 if capitals is not None:
4336 self.capitals = capitals
4337 if clamp is not None:
4338 self.clamp = clamp
4339 if flags is not None:
4340 if isinstance(flags, list):
4341 flags = {v:(v in flags) for v in OrderedSignals[decimal] + flags}
4342 self.flags = flags
4343 if traps is not None:
4344 if isinstance(traps, list):
4345 traps = {v:(v in traps) for v in OrderedSignals[decimal] + traps}
4346 self.traps = traps
4347
4348 c = Context()
4349 d = MyContext()
4350 for attr in ('prec', 'rounding', 'Emin', 'Emax', 'capitals', 'clamp',
4351 'flags', 'traps'):
4352 self.assertEqual(getattr(c, attr), getattr(d, attr))
4353
4354 # prec
4355 self.assertRaises(ValueError, MyContext, **{'prec':-1})
4356 c = MyContext(prec=1)
4357 self.assertEqual(c.prec, 1)
4358 self.assertRaises(InvalidOperation, c.quantize, Decimal('9e2'), 0)
4359
4360 # rounding
4361 self.assertRaises(TypeError, MyContext, **{'rounding':'XYZ'})
4362 c = MyContext(rounding=ROUND_DOWN, prec=1)
4363 self.assertEqual(c.rounding, ROUND_DOWN)
4364 self.assertEqual(c.plus(Decimal('9.9')), 9)
4365
4366 # Emin

Callers

nothing calls this directly

Calls 14

DecimalClass · 0.85
strFunction · 0.85
assertTrueMethod · 0.80
create_decimalMethod · 0.80
to_sci_stringMethod · 0.80
clear_flagsMethod · 0.80
assertFalseMethod · 0.80
ContextClass · 0.70
MyContextClass · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45
plusMethod · 0.45

Tested by

no test coverage detected