Commits

Amaury Forgeot d'Arc  committed 494911d

Add enough of context constructor,
so that test_decimal can be imported and run tests.

  • Participants
  • Parent commits 2ca4490
  • Branches decimal-libmpdec

Comments (0)

Files changed (5)

File pypy/module/_decimal/__init__.py

 from pypy.interpreter.mixedmodule import MixedModule
+from rpython.rlib import rmpdec
 
 class Module(MixedModule):
     appleveldefs = {
     
     interpleveldefs = {
         'Decimal': 'interp_decimal.W_Decimal',
+        'Context': 'interp_context.W_Context',
         'getcontext': 'interp_context.getcontext',
+        'setcontext': 'interp_context.setcontext',
 
         'IEEE_CONTEXT_MAX_BITS': 'space.wrap(interp_decimal.IEEE_CONTEXT_MAX_BITS)',
         }
+    for name in rmpdec.ROUND_CONSTANTS:
+        interpleveldefs[name] = 'space.wrap(%r)' % (
+            getattr(rmpdec, 'MPD_' + name),)
     for name in ('DecimalException', 'Clamped', 'Rounded', 'Inexact',
                  'Subnormal', 'Underflow', 'Overflow', 'DivisionByZero',
                  'InvalidOperation', 'FloatOperation'):

File pypy/module/_decimal/interp_context.py

     def __init__(self, space):
         self.w_flags = space.call_function(state_get(space).W_SignalDict)
 
+    def copy_w(self, space):
+        w_copy = W_Context(space)
+        # XXX incomplete
+        return w_copy
+
+def descr_new_context(space, w_subtype, __args__):
+    w_result = space.allocate_instance(W_Context, w_subtype)
+    W_Context.__init__(w_result, space)
+    return w_result
+
 W_Context.typedef = TypeDef(
     'Context',
+    copy=interp2app(W_Context.copy_w),
     flags=interp_attrproperty_w('w_flags', W_Context),
+    __new__ = interp2app(descr_new_context),
     )
 
 
         # Set up a new thread local context
         ec.decimal_context = W_Context(space)
     return ec.decimal_context
+
+def setcontext(space, w_context):
+    ec = space.getexecutioncontext()
+    ec.decimal_context = w_context

File pypy/module/_decimal/test/test_module.py

         import _decimal
         assert isinstance(_decimal.Decimal, type)
 
+    def test_context(self):
+        import _decimal
+        context = _decimal.Context(
+            prec=9, rounding=_decimal.ROUND_HALF_EVEN,
+            traps=dict.fromkeys(_decimal.getcontext().flags.keys(), 0))
+        _decimal.setcontext(context)
+        assert _decimal.getcontext() is context
+
     def test_contextflags(self):
         import _decimal
         from collections.abc import MutableMapping

File rpython/rlib/rmpdec.py

     libraries=['m'],
     )
 
+
+ROUND_CONSTANTS = (
+    'ROUND_UP', 'ROUND_DOWN', 'ROUND_CEILING', 'ROUND_FLOOR',
+    'ROUND_HALF_UP', 'ROUND_HALF_DOWN', 'ROUND_HALF_EVEN',
+    'ROUND_05UP', 'ROUND_TRUNC')
+
 class CConfig:
     _compilation_info_ = eci
 
     MPD_IEEE_CONTEXT_MAX_BITS = platform.ConstantInteger(
         'MPD_IEEE_CONTEXT_MAX_BITS')
 
+    for name in ROUND_CONSTANTS:
+        name = 'MPD_' + name
+        locals()[name] = platform.ConstantInteger(name)
+
+
 globals().update(platform.configure(CConfig))

File rpython/rlib/test/test_rmpdec.py

 from rpython.rlib import rmpdec
+
+class TestMpdec:
+    def test_constants(self):
+        assert 'ROUND_HALF_EVEN' in rmpdec.ROUND_CONSTANTS
+        assert isinstance(rmpdec.MPD_ROUND_HALF_EVEN, int)