Skip to content

Commit

Permalink
Function wrapping refactoring, now uses tuples.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.dsource.org/projects/pyd/trunk@28 1df65b71-e716-0410-9316-ac55df2b1602
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Jul 12, 2006
1 parent f493434 commit b6ff9e2
Show file tree
Hide file tree
Showing 13 changed files with 1,096 additions and 563 deletions.
1 change: 1 addition & 0 deletions dcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'object.d',
'op_wrap.d',
'pyd.d',
'tuples.d',
]

_pyVerXDotY = '.'.join(str(v) for v in sys.version_info[:2]) # e.g., '2.4'
Expand Down
68 changes: 42 additions & 26 deletions examples/testdll/testdll.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ module testdll;

import python;
import pyd.pyd;
//import pyd.ftype;
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);
void apply_test(int i, char[] s) {
writefln("%s %s", i, s);
}

Py_DECREF(s);
Py_DECREF(i);
void foo() {
alias tuple!(int, char[]) Tuple;
// alias dg_from_tuple!(void, Tuple) dg;
Tuple t;
// Tuple.TypeNo!(0) i = 20;
// typeof(Tuple.arg1) j = 30;
t.arg1 = 20;
t.arg2 = "Monkey";
// t.arg3 = 5.8;
apply_tuple_to_fn(t, &apply_test);

// writefln(typeid(ArgType!(dg, 1)));
// writefln(typeid(TypeNo!(Tuple, 0)));
// writefln(typeid(Tuple.A1));
// writefln(typeid(dg));
// writefln(i, " ", j);
}

void foo(int i) {
Expand Down Expand Up @@ -89,27 +98,34 @@ Foo spam(Foo f) {

extern (C)
export void inittestdll() {
module_init("testdll");

def!("foo", foo);
def!(foo, "foo");
// Python does not support function overloading. This allows us to wrap
// an overloading function under a different name.
def!("foo2", foo, 1, void function(int));
def!("bar", bar);
// an overloading function under a different name. Note that if the
// overload accepts a different number of minimum arguments, that number
// must be specified.
def!(foo, "foo2", void function(int), 1);
def!(bar, "bar");
// Default argument support - Now implicit!
def!("baz", baz);
def!("spam", spam);
def!("iter_test", iter_test);
def!("func_test", func_test);
def!("dg_test", dg_test);
def!(baz, "baz");
def!(spam, "spam");
def!(iter_test, "iter_test");
def!(func_test, "func_test");
def!(dg_test, "dg_test");

module_init("testdll");

auto t = func_range!(foo, 0)();
alias typeof(t) Tu;
writefln(typeid(TypeNo!(Tu, 1)));
writefln(MIN_ARGS!(bar));

wrapped_class!("Foo", Foo) f;
wrapped_class!(Foo, "Foo") f;
// Constructor wrapping
f.init!(ctor!(int), ctor!(int, int));
f.init!(tuple!(int), tuple!(int, int));
// Member function wrapping
f.def!("foo", Foo.foo);
f.def!(Foo.foo, "foo");
// Property wrapping
f.prop!("i", Foo.i);
f.prop!(Foo.i, "i");
finalize_class(f);
}

15 changes: 10 additions & 5 deletions infrastructure/pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private import pyd.ftype;
private import pyd.func_wrap;
private import pyd.make_object;
private import pyd.op_wrap;
private import pyd.tuples;

private import std.string;

Expand Down Expand Up @@ -234,7 +235,8 @@ template wrapped_prop_list(T) {
* This struct wraps a D class. Its member functions are the primary way of
* wrapping the specific parts of the class.
*/
template wrapped_class(char[] classname, T) {
template wrapped_class(T, char[] classname) {
pragma(msg, "wrapped_class: " ~ classname);
struct wrapped_class {
static const char[] _name = classname;
T t = null;
Expand All @@ -248,7 +250,8 @@ template wrapped_class(char[] classname, T) {
* fn_t = The type of the function. It is only useful to specify this
* if more than one function has the same name as this one.
*/
template def(char[] name, alias fn, uint MIN_ARGS = MIN_ARGS!(fn), fn_t=typeof(&fn)) {
template def(alias fn, char[] name, fn_t=typeof(&fn), uint MIN_ARGS = MIN_ARGS!(fn)) {
pragma(msg, "class.def: " ~ name);
static void def() {
static PyMethodDef empty = { null, null, 0, null };
alias wrapped_method_list!(T) list;
Expand All @@ -271,7 +274,8 @@ template wrapped_class(char[] classname, T) {
* fn = The property to wrap.
* RO = Whether this is a read-only property.
*/
template prop(char[] name, alias fn, bool RO=false) {
template prop(alias fn, char[] name, bool RO=false) {
pragma(msg, "class.prop: " ~ name);
static void prop() {
static PyGetSetDef empty = { null, null, null, null, null };
wrapped_prop_list!(T)[length-1].name = name ~ \0;
Expand Down Expand Up @@ -303,10 +307,10 @@ template wrapped_class(char[] classname, T) {
* This currently does not support having multiple constructors with
* the same number of arguments.
*/
template init(alias C1=undefined, alias C2=undefined, alias C3=undefined, alias C4=undefined, alias C5=undefined, alias C6=undefined, alias C7=undefined, alias C8=undefined, alias C9=undefined, alias C10=undefined) {
template init(C1=Void, C2=Void, C3=Void, C4=Void, C5=Void, C6=Void, C7=Void, C8=Void, C9=Void, C10=Void) {
static void init() {
wrapped_class_type!(T).tp_init =
&wrapped_ctors!(T, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10).init_func;
&wrapped_ctors!(T, tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10)).init_func;
}
}
}
Expand All @@ -320,6 +324,7 @@ void finalize_class(CLS) (CLS cls) {
alias typeof(cls.t) T;
alias wrapped_class_type!(T) type;
const char[] name = CLS._name;
pragma(msg, "finalize_class: " ~ name);

assert(DPy_Module_p !is null, "Must initialize module before wrapping classes.");
char[] module_name = .toString(PyModule_GetName(DPy_Module_p));
Expand Down
Loading

0 comments on commit b6ff9e2

Please sign in to comment.