[pypy-commit] pypy py3.5: Implement _PyNamespace_New()

rlamy pypy.commits at gmail.com
Wed Jun 28 12:23:07 EDT 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r91658:97ca3ac43c30
Date: 2017-06-28 17:22 +0100
http://bitbucket.org/pypy/pypy/changeset/97ca3ac43c30/

Log:	Implement _PyNamespace_New()

diff --git a/pypy/module/cpyext/__init__.py b/pypy/module/cpyext/__init__.py
--- a/pypy/module/cpyext/__init__.py
+++ b/pypy/module/cpyext/__init__.py
@@ -75,6 +75,7 @@
 import pypy.module.cpyext.methodobject
 import pypy.module.cpyext.dictproxyobject
 import pypy.module.cpyext.genobject
+import pypy.module.cpyext.namespaceobject
 
 # now that all rffi_platform.Struct types are registered, configure them
 api.configure_types()
diff --git a/pypy/module/cpyext/namespaceobject.py b/pypy/module/cpyext/namespaceobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/namespaceobject.py
@@ -0,0 +1,8 @@
+from pypy.module.cpyext.api import cts
+
+ at cts.decl("PyObject * _PyNamespace_New(PyObject *kwds)")
+def _PyNamespace_new(space, w_kwds):
+    return space.appexec([w_kwds], """(kwds):
+        from _structseq import SimpleNamespace
+        return SimpleNamespace(**kwds)
+        """)
diff --git a/pypy/module/cpyext/test/test_namespaceobject.py b/pypy/module/cpyext/test/test_namespaceobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/test/test_namespaceobject.py
@@ -0,0 +1,11 @@
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+
+class AppTestNamespace(AppTestCpythonExtensionBase):
+    def test_simple(self):
+        from types import SimpleNamespace
+        module = self.import_extension('ns', [
+            ("new", "METH_O",
+             """
+                return _PyNamespace_New(args);
+             """)])
+        assert module.new({'a': 1, 'b': 2}) == SimpleNamespace(a=1, b=2)


More information about the pypy-commit mailing list