[pypy-commit] pypy decimal-libmpdec: Add enough of context constructor,

amauryfa noreply at buildbot.pypy.org
Sun May 11 00:27:37 CEST 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: decimal-libmpdec
Changeset: r71449:494911ddea62
Date: 2014-05-02 01:04 +0200
http://bitbucket.org/pypy/pypy/changeset/494911ddea62/

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

diff --git a/pypy/module/_decimal/__init__.py b/pypy/module/_decimal/__init__.py
--- a/pypy/module/_decimal/__init__.py
+++ b/pypy/module/_decimal/__init__.py
@@ -1,4 +1,5 @@
 from pypy.interpreter.mixedmodule import MixedModule
+from rpython.rlib import rmpdec
 
 class Module(MixedModule):
     appleveldefs = {
@@ -6,10 +7,15 @@
     
     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'):
diff --git a/pypy/module/_decimal/interp_context.py b/pypy/module/_decimal/interp_context.py
--- a/pypy/module/_decimal/interp_context.py
+++ b/pypy/module/_decimal/interp_context.py
@@ -45,9 +45,21 @@
     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),
     )
 
 
@@ -59,3 +71,7 @@
         # 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
diff --git a/pypy/module/_decimal/test/test_module.py b/pypy/module/_decimal/test/test_module.py
--- a/pypy/module/_decimal/test/test_module.py
+++ b/pypy/module/_decimal/test/test_module.py
@@ -9,6 +9,14 @@
         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
diff --git a/rpython/rlib/rmpdec.py b/rpython/rlib/rmpdec.py
--- a/rpython/rlib/rmpdec.py
+++ b/rpython/rlib/rmpdec.py
@@ -34,10 +34,21 @@
     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))
diff --git a/rpython/rlib/test/test_rmpdec.py b/rpython/rlib/test/test_rmpdec.py
--- a/rpython/rlib/test/test_rmpdec.py
+++ b/rpython/rlib/test/test_rmpdec.py
@@ -1,1 +1,6 @@
 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)


More information about the pypy-commit mailing list