Skip to content

Commit

Permalink
Small class wrapping edits/bug fixes.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.dsource.org/projects/pyd/trunk@18 1df65b71-e716-0410-9316-ac55df2b1602
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Jul 1, 2006
1 parent ec0f7a7 commit fce5f5e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 57 deletions.
3 changes: 3 additions & 0 deletions build_testembed.bat
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions pyd/ctor_wrap.d
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}

4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 17 additions & 9 deletions testdll.d
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand All @@ -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);
}
Expand All @@ -27,22 +34,23 @@ 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);
}
}

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);
}
Expand Down
41 changes: 0 additions & 41 deletions testdll.map

This file was deleted.

51 changes: 51 additions & 0 deletions testembed.d
Original file line number Diff line number Diff line change
@@ -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;
}


0 comments on commit fce5f5e

Please sign in to comment.