[py-svn] r9756 - in py/dist/py: . c-extension/extractdict magic magic/testing

arigo at codespeak.net arigo at codespeak.net
Sun Mar 13 11:24:44 CET 2005


Author: arigo
Date: Sun Mar 13 11:24:44 2005
New Revision: 9756

Added:
   py/dist/py/c-extension/extractdict/
   py/dist/py/c-extension/extractdict/extractdict.c   (contents, props changed)
   py/dist/py/magic/extractdict.py   (contents, props changed)
   py/dist/py/magic/testing/test_extractdict.py   (contents, props changed)
Modified:
   py/dist/py/__init__.py
Log:
py.magic.extractdict(x) -> returns the C-level dictionary of 'x'.

Useful to hack built-in types :-)



Modified: py/dist/py/__init__.py
==============================================================================
--- py/dist/py/__init__.py	(original)
+++ py/dist/py/__init__.py	Sun Mar 13 11:24:44 2005
@@ -39,6 +39,7 @@
     'magic.autopath'         : ('./magic/autopath.py', 'autopath'),
     'magic.View'             : ('./magic/viewtype.py', 'View'),
     'magic.AssertionError'   : ('./magic/assertion.py', 'AssertionError'),
+    'magic.extractdict'      : ('./magic/extractdict.py', 'extractdict'),
 
     'code.compile'           : ('./code/source.py', 'compile_'),
     'code.Source'            : ('./code/source.py', 'Source'),

Added: py/dist/py/c-extension/extractdict/extractdict.c
==============================================================================
--- (empty file)
+++ py/dist/py/c-extension/extractdict/extractdict.c	Sun Mar 13 11:24:44 2005
@@ -0,0 +1,35 @@
+#include <Python.h>
+
+
+static PyObject* extractdict(PyObject* self, PyObject* arg)
+{
+	PyObject *dict;
+	PyObject **p = _PyObject_GetDictPtr(arg);
+
+	if (p == NULL) {
+		PyErr_Format(PyExc_TypeError,
+                             "'%.100s' object has no __dict__",
+			     arg->ob_type->tp_name);
+		return NULL;
+	}
+	dict = *p;
+	if (dict == NULL) {
+		dict = PyDict_New();
+		if (dict == NULL)
+			return NULL;
+		*p = dict;
+	}
+	Py_INCREF(dict);
+	return dict;
+}
+
+
+static PyMethodDef ExtractDictMethods[] = {
+	{"extractdict", extractdict, METH_O, NULL},
+	{NULL,     NULL}        /* Sentinel */
+};
+
+void initextractdict(void)
+{
+	Py_InitModule("extractdict", ExtractDictMethods);
+}

Added: py/dist/py/magic/extractdict.py
==============================================================================
--- (empty file)
+++ py/dist/py/magic/extractdict.py	Sun Mar 13 11:24:44 2005
@@ -0,0 +1,5 @@
+
+import py
+gdir = py.path.local(py.__file__).dirpath() 
+path = gdir.join('c-extension', 'extractdict', 'extractdict.c')
+extractdict = path.getpymodule().extractdict

Added: py/dist/py/magic/testing/test_extractdict.py
==============================================================================
--- (empty file)
+++ py/dist/py/magic/testing/test_extractdict.py	Sun Mar 13 11:24:44 2005
@@ -0,0 +1,27 @@
+import py
+
+def test_extractdict():
+    from py.magic import extractdict
+    d = extractdict(str)
+    assert 'join' in d
+    saved = d['join']
+    try:
+        del d['join']
+        assert not hasattr('', 'join')
+    finally:
+        d['join'] = saved
+
+def test_typeerror():
+    from py.magic import extractdict
+    py.test.raises(TypeError, extractdict, 5)
+
+class X(object):
+    pass
+
+def test_emptydict():
+    from py.magic import extractdict
+    x = X()
+    d1 = extractdict(x)
+    assert d1 is x.__dict__
+    d2 = extractdict(x)
+    assert d2 is d1



More information about the pytest-commit mailing list