[pypy-commit] pypy decimal-libmpdec: Start decimal.context, so far only with a skeleton of the "flags" attribute.

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


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: decimal-libmpdec
Changeset: r71447:a858f7de905a
Date: 2014-05-01 22:42 +0200
http://bitbucket.org/pypy/pypy/changeset/a858f7de905a/

Log:	Start decimal.context, so far only with a skeleton of the "flags"
	attribute.

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
@@ -6,5 +6,6 @@
     
     interpleveldefs = {
         'Decimal': 'interp_decimal.W_Decimal',
+        'getcontext': 'interp_context.getcontext',
         'IEEE_CONTEXT_MAX_BITS': 'space.wrap(interp_decimal.IEEE_CONTEXT_MAX_BITS)',
         }
diff --git a/pypy/module/_decimal/interp_context.py b/pypy/module/_decimal/interp_context.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_decimal/interp_context.py
@@ -0,0 +1,61 @@
+from rpython.rlib import rmpdec
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import (TypeDef, interp_attrproperty_w)
+from pypy.interpreter.executioncontext import ExecutionContext
+
+
+# The SignalDict is a MutableMapping that provides access to the
+# mpd_context_t flags, which reside in the context object.
+# When a new context is created, context.traps and context.flags are
+# initialized to new SignalDicts.
+# Once a SignalDict is tied to a context, it cannot be deleted.
+class W_SignalDictMixin(W_Root):
+    pass
+
+def descr_new_signaldict(space, w_subtype):
+    w_result = space.allocate_instance(W_SignalDictMixin, w_subtype)
+    W_SignalDictMixin.__init__(w_result)
+    return w_result
+
+W_SignalDictMixin.typedef = TypeDef(
+    'SignalDictMixin',
+    __new__ = interp2app(descr_new_signaldict),
+    )
+
+
+class State:
+    def __init__(self, space):
+        w_import = space.builtin.get('__import__')
+        w_collections = space.call_function(w_import,
+                                            space.wrap('collections'))
+        w_MutableMapping = space.getattr(w_collections,
+                                         space.wrap('MutableMapping'))
+        self.W_SignalDict = space.call_function(
+            space.w_type, space.wrap("SignalDict"),
+            space.newtuple([space.gettypeobject(W_SignalDictMixin.typedef),
+                            w_MutableMapping]),
+            space.newdict())
+
+def state_get(space):
+    return space.fromcache(State)
+
+
+class W_Context(W_Root):
+    def __init__(self, space):
+        self.w_flags = space.call_function(state_get(space).W_SignalDict)
+
+W_Context.typedef = TypeDef(
+    'Context',
+    flags=interp_attrproperty_w('w_flags', W_Context),
+    )
+
+
+ExecutionContext.decimal_context = None
+
+def getcontext(space):
+    ec = space.getexecutioncontext()
+    if not ec.decimal_context:
+        # Set up a new thread local context
+        ec.decimal_context = W_Context(space)
+    return ec.decimal_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
@@ -8,3 +8,11 @@
     def test_type(self):
         import _decimal
         assert isinstance(_decimal.Decimal, type)
+
+    def test_contextflags(self):
+        import _decimal
+        from collections.abc import MutableMapping
+        flags = _decimal.getcontext().flags
+        assert type(flags).__name__ == 'SignalDict'
+        bases = type(flags).__bases__
+        assert bases[1] is MutableMapping


More information about the pypy-commit mailing list