From: <wme...@us...> - 2009-08-31 08:54:10
|
Revision: 6732 http://jython.svn.sourceforge.net/jython/?rev=6732&view=rev Author: wmeissner Date: 2009-08-31 07:40:56 +0000 (Mon, 31 Aug 2009) Log Message: ----------- Start implementing POINTER() Modified Paths: -------------- branches/ctypes-jffi/Lib/ctypes/__init__.py branches/ctypes-jffi/src/org/python/modules/jffi/Pointer.java Modified: branches/ctypes-jffi/Lib/ctypes/__init__.py =================================================================== --- branches/ctypes-jffi/Lib/ctypes/__init__.py 2009-08-30 23:31:13 UTC (rev 6731) +++ branches/ctypes-jffi/Lib/ctypes/__init__.py 2009-08-31 07:40:56 UTC (rev 6732) @@ -36,10 +36,27 @@ def pointer(cdata): return cdata.pointer() -def POINTER(type): -# return jffi.Type.Pointer(type) - return c_void_p +_pointer_type_cache = {} +def POINTER(ctype): + # If a pointer class for the C type has been created, re-use it + if _pointer_type_cache.has_key(ctype): + return _pointer_type_cache[ctype] + # Create a new class for this particular C type + dict = { '_jffi_type': jffi.Type.Pointer(ctype) } + # Look back up the stack frame to find out the module this new type is declared in + import inspect + mod = inspect.getmodule(inspect.stack()[1][0]) + if mod is None: + name = "__main__" + else: + name = mod.__name__ + dict["__module__"] = name + + ptype = type("LP_%s" % (ctype.__name__,), (jffi.Pointer,), dict) + _pointer_type_cache[ctype] = ptype + return ptype + class c_byte(_ScalarCData): _jffi_type = jffi.Type.BYTE Modified: branches/ctypes-jffi/src/org/python/modules/jffi/Pointer.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/Pointer.java 2009-08-30 23:31:13 UTC (rev 6731) +++ branches/ctypes-jffi/src/org/python/modules/jffi/Pointer.java 2009-08-31 07:40:56 UTC (rev 6732) @@ -1,9 +1,13 @@ package org.python.modules.jffi; +import org.python.core.Py; +import org.python.core.PyNewWrapper; import org.python.core.PyObject; import org.python.core.PyType; import org.python.expose.ExposedGet; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedSet; import org.python.expose.ExposedType; @ExposedType(name = "jffi.Pointer", base = PyObject.class) @@ -40,9 +44,35 @@ this.componentMemoryOp = componentMemoryOp; } + @ExposedNew + public static PyObject Pointer_new(PyNewWrapper new_, boolean init, PyType subtype, + PyObject[] args, String[] keywords) { + + PyObject jffi_type = subtype.__getattr__("_jffi_type"); + + if (!(jffi_type instanceof CType.Pointer)) { + throw Py.TypeError("invalid _jffi_type for " + subtype.getName()); + } + + CType.Pointer type = (CType.Pointer) jffi_type; + + if (args.length == 0) { + return new Pointer(subtype, NullMemory.INSTANCE, type.componentMemoryOp); + } + DirectMemory contents = AllocatedNativeMemory.allocate(type.componentType.size(), false); + type.componentMemoryOp.put(contents, 0, args[0]); + + return new Pointer(subtype, contents, type.componentMemoryOp); + } + @ExposedGet(name="contents") public PyObject contents() { return componentMemoryOp.get(memory, 0); } + @ExposedSet(name="contents") + public void contents(PyObject value) { + componentMemoryOp.put(memory, 0, value); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |