From fce5f5e7b06ce2cc34f08267428e6d6ce052d7eb Mon Sep 17 00:00:00 2001 From: KirkMcDonald Date: Sat, 1 Jul 2006 01:32:18 +0000 Subject: [PATCH] Small class wrapping edits/bug fixes. git-svn-id: http://svn.dsource.org/projects/pyd/trunk@18 1df65b71-e716-0410-9316-ac55df2b1602 --- build_testembed.bat | 3 +++ pyd/class_wrap.d | 10 ++++----- pyd/ctor_wrap.d | 23 ++++++++++++++++++++ test.py | 4 ++-- testdll.d | 26 +++++++++++++++-------- testdll.map | 41 ------------------------------------ testembed.d | 51 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 101 insertions(+), 57 deletions(-) create mode 100644 build_testembed.bat delete mode 100644 testdll.map create mode 100644 testembed.d diff --git a/build_testembed.bat b/build_testembed.bat new file mode 100644 index 0000000..d015d65 --- /dev/null +++ b/build_testembed.bat @@ -0,0 +1,3 @@ +C:\dmd\dmd\bin\dmd.exe -odbuild\testembed -ofbuild\testembed\testembed.exe testembed.d C:\Python24\Lib\site-packages\celerid\infrastructure\python\headers\python.d C:\Python24\lib\site-packages\celerid\infrastructure\python\libs\2.4\python24_digitalmars.lib + +pause diff --git a/pyd/class_wrap.d b/pyd/class_wrap.d index 4fc5166..429d147 100644 --- a/pyd/class_wrap.d +++ b/pyd/class_wrap.d @@ -199,16 +199,16 @@ wrapped_class!(name, T) wrap_class(char[] name, T) () { } void finalize_class(char[] name, T) () { - if (PyType_Ready(&wrapped_class_type!(name, T)) < 0) { - // XXX: This will probably crash the interpreter, as it isn't normally - // caught and translated. - throw new Exception("Couldn't ready wrapped type!"); - } // If a ctor wasn't supplied, try the default. if (wrapped_class_type!(name, T).tp_init is null) { wrapped_class_type!(name, T).tp_init = &wrapped_init!(T).init; } + if (PyType_Ready(&wrapped_class_type!(name, T)) < 0) { + // XXX: This will probably crash the interpreter, as it isn't normally + // caught and translated. + throw new Exception("Couldn't ready wrapped type!"); + } Py_INCREF(cast(PyObject*)&wrapped_class_type!(name, T)); PyModule_AddObject(DPy_Module_p, name, cast(PyObject*)&wrapped_class_type!(name, T)); is_wrapped!(T) = true; diff --git a/pyd/ctor_wrap.d b/pyd/ctor_wrap.d index d84c973..0460c81 100644 --- a/pyd/ctor_wrap.d +++ b/pyd/ctor_wrap.d @@ -1,3 +1,24 @@ +/* +Copyright (c) 2006 Kirk McDonald + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ module pyd.ctor_wrap; private import python; @@ -240,6 +261,8 @@ template wrapped_ctors(T, alias C1, alias C2, alias C3, alias C4, alias C5, alia if (len == C10.ARGS) return wrapped_ctor!(T, C10)(self, args, kwds); } + PyErr_SetString(PyExc_TypeError, "Unsupported number of constructor arguments."); + return -1; } } diff --git a/test.py b/test.py index 2d58739..4c16dc4 100644 --- a/test.py +++ b/test.py @@ -10,11 +10,11 @@ sys.path.append(os.path.abspath(libDir)) import testdll -print testdll.bar(12) +testdll.foo() print -testdll.foo() +print testdll.bar(12) print diff --git a/testdll.d b/testdll.d index 8821773..8fbe077 100644 --- a/testdll.d +++ b/testdll.d @@ -4,6 +4,20 @@ import python; import pyd.pyd; import std.stdio; +// d_type testing +void foo() { + PyObject* s = PyString_FromString("blargh"); + PyObject* i = PyInt_FromLong(20); + + int a = d_type!(int)(i); + char[] b = d_type!(char[])(s); + + writefln("%s\n%s", a, b); + + Py_DECREF(s); + Py_DECREF(i); +} + char[] bar(int i) { if (i > 10) { return "It's greater than 10!"; @@ -12,13 +26,6 @@ char[] bar(int i) { } } -void foo() { - writefln("typeof(Py_None) == %s", typeid(typeof(Py_None))); - writefln("typeof(Py_None()) == %s", typeid(typeof(Py_None()))); - DPyObject o = new DPyObject(); - writefln("Py_None.repr() == %s", o); -} - void baz(int i=10, char[] s="moo") { writefln("i = %s\ns = %s", i, s); } @@ -27,6 +34,7 @@ class Foo { int m_i; this() { } this(int i) { m_i = i; } + this(int i, int j) { m_i = i + j; } void foo() { writefln("Foo.foo(): i = %s", m_i); } @@ -34,15 +42,15 @@ class Foo { extern (C) export void inittestdll() { - def!("bar", bar); def!("foo", foo); + def!("bar", bar); // Minimum argument count. def!("baz", baz, 0); module_init("testdll"); auto Foo_ = wrap_class!("Foo", Foo)(); - Foo_.init!(ctor!(int)); + Foo_.init!(ctor!(int), ctor!(int, int)); Foo_.def!("foo", Foo.foo); finalize_class!("Foo", Foo); } diff --git a/testdll.map b/testdll.map deleted file mode 100644 index a69c84a..0000000 --- a/testdll.map +++ /dev/null @@ -1,41 +0,0 @@ - - Start Length Name Class - 0003:00000000 000133F0H _TEXT CODE 32-bit - 0003:000133F0 00000378H ICODE ICODE 32-bit - 0004:00000000 00000004H .CRT$XIA DATA 32-bit - 0004:00000010 00000004H .CRT$XIZ DATA 32-bit - 0004:00000020 00000004H .CRT$XCA DATA 32-bit - 0004:00000030 00000004H .CRT$XCZ DATA 32-bit - 0004:00000040 00000004H .CRT$XPA DATA 32-bit - 0004:00000050 00000004H .CRT$XPZ DATA 32-bit - 0004:00000060 00000004H .CRT$XTA DATA 32-bit - 0004:00000070 00000004H .CRT$XTZ DATA 32-bit - 0004:00000074 00000000H IMP__DATA IMP__DATA 32-bit - 0004:00000080 000082E0H _DATA DATA 32-bit - 0004:00008360 00000000H FMB DATA 32-bit - 0004:00008360 00000084H FM DATA 32-bit - 0004:000083E4 00000000H FME DATA 32-bit - 0004:000083E4 00000000H XIB DATA 32-bit - 0004:000083E4 00000018H XI DATA 32-bit - 0004:000083FC 00000000H XIE DATA 32-bit - 0004:000083FC 00000000H XCB DATA 32-bit - 0004:000083FC 00000010H XC DATA 32-bit - 0004:0000840C 00000000H XCE DATA 32-bit - 0004:0000840C 00000000H XIFCB DATA 32-bit - 0004:0000840C 00000004H XIFU DATA 32-bit - 0004:00008410 00000000H XIFL DATA 32-bit - 0004:00008410 00000004H XIFM DATA 32-bit - 0004:00008414 00000000H XIFCE DATA 32-bit - 0004:00008420 00000000H CONST CONST 32-bit - 0004:00008420 00000000H EEND ENDBSS 32-bit - 0004:00008420 000032F8H _BSS BSS 32-bit - 0004:0000B718 00000000H XOB BSS 32-bit - 0004:0000B718 00000004H XO BSS 32-bit - 0004:0000B71C 00000000H XOE BSS 32-bit - 0004:0000B71C 00000000H XOFB BSS 32-bit - 0004:0000B71C 00000108H XOF BSS 32-bit - 0004:0000B824 00000000H XOFE BSS 32-bit - 0004:0000B830 0000042AH c_common BSS 32-bit - 0004:0000BC60 00000000H STACK STACK 32-bit - -Program entry point at 0000E534 diff --git a/testembed.d b/testembed.d new file mode 100644 index 0000000..b8d7ce0 --- /dev/null +++ b/testembed.d @@ -0,0 +1,51 @@ +import python; +import std.string; + +int main() { + Py_Initialize(); + scope(exit) Py_Finalize(); + + PyObject* sys = PyImport_ImportModule("sys"); + scope(exit) Py_DECREF(sys); + + PyObject* path = PyObject_GetAttrString(sys, "path"); + scope(exit) Py_DECREF(path); + + PyObject* dir = PyString_FromString("C:\\Projects\\Pyd\\build\\lib.win32-2.4"); + PyList_Append(path, dir); + Py_DECREF(dir); + + PyObject* testdll = PyImport_ImportModule("testdll"); + scope(exit) Py_DECREF(testdll); + + PyObject* Foo = PyObject_GetAttrString(testdll, "Foo"); + scope(exit) Py_DECREF(Foo); + + PyObject* t = PyObject_Type(Foo); + scope(exit) Py_DECREF(t); + PyObject* s = PyObject_Repr(t); + scope(exit) Py_DECREF(s); + char[] str = .toString(PyString_AsString(s)); + writefln("type(Foo) is %s", str); + + PyObject* i = PyInt_FromLong(12); + scope(exit) Py_DECREF(i); + PyObject* st = PyString_FromString("moo"); + scope(exit) Py_DECREF(st); + + PyObject* a = PyObject_CallFunctionObjArgs(Foo, i, null); + scope(exit) Py_DECREF(a); + + PyObject* b = PyObject_CallFunctionObjArgs(Foo, st, null); + scope(exit) Py_DECREF(b); + + PyObject* foo = PyObject_GetAttrString(a, "foo"); + scope(exit) Py_DECREF(foo); + + PyObject* temp = PyObject_CallObject(foo, null); + Py_DECREF(temp); + + return 0; +} + +