(root)/
Python-3.11.7/
Lib/
test/
test_asyncio/
test_context.py
       1  import asyncio
       2  import decimal
       3  import unittest
       4  
       5  
       6  def tearDownModule():
       7      asyncio.set_event_loop_policy(None)
       8  
       9  
      10  @unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context")
      11  class ESC[4;38;5;81mDecimalContextTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      12  
      13      def test_asyncio_task_decimal_context(self):
      14          async def fractions(t, precision, x, y):
      15              with decimal.localcontext() as ctx:
      16                  ctx.prec = precision
      17                  a = decimal.Decimal(x) / decimal.Decimal(y)
      18                  await asyncio.sleep(t)
      19                  b = decimal.Decimal(x) / decimal.Decimal(y ** 2)
      20                  return a, b
      21  
      22          async def main():
      23              r1, r2 = await asyncio.gather(
      24                  fractions(0.1, 3, 1, 3), fractions(0.2, 6, 1, 3))
      25  
      26              return r1, r2
      27  
      28          r1, r2 = asyncio.run(main())
      29  
      30          self.assertEqual(str(r1[0]), '0.333')
      31          self.assertEqual(str(r1[1]), '0.111')
      32  
      33          self.assertEqual(str(r2[0]), '0.333333')
      34          self.assertEqual(str(r2[1]), '0.111111')
      35  
      36  
      37  if __name__ == '__main__':
      38      unittest.main()