From ad1e457d8163399064a12b5f8ee342d25ad095dd Mon Sep 17 00:00:00 2001 From: KirkMcDonald Date: Mon, 13 Aug 2007 21:12:37 +0000 Subject: [PATCH] * Un-broke Init. (Oops.) * Reverted some symbol-length-shortening code, which caused the above problem. git-svn-id: http://svn.dsource.org/projects/pyd/trunk@122 1df65b71-e716-0410-9316-ac55df2b1602 --- infrastructure/pyd/class_wrap.d | 42 +++++++++++++++++++++++++-------- infrastructure/pyd/ctor_wrap.d | 9 ++++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/infrastructure/pyd/class_wrap.d b/infrastructure/pyd/class_wrap.d index 8376583..6df4864 100644 --- a/infrastructure/pyd/class_wrap.d +++ b/infrastructure/pyd/class_wrap.d @@ -155,6 +155,8 @@ template wrapped_prop_list(T) { // STANDARD METHODS // ////////////////////// +//import std.stdio; + /// Various wrapped methods template wrapped_methods(T) { alias wrapped_class_object!(T) wrap_object; @@ -177,7 +179,8 @@ template wrapped_methods(T) { extern(C) void wrapped_dealloc(PyObject* self) { exception_catcher(delegate void() { - WrapPyObject_SetObj(self, cast(T)null); + //writefln("wrapped_dealloc: T is %s", typeid(T)); + WrapPyObject_SetObj!(T)(self, cast(T)null); self.ob_type.tp_free(self); }); } @@ -488,11 +491,12 @@ the same number of arguments. struct Init(C ...) { alias C ctors; static const bool needs_shim = true; - template call(T) { - mixin wrapped_ctors!(param.ctors) Ctors; + template call(T, shim) { + //mixin wrapped_ctors!(param.ctors) Ctors; static void call() { wrapped_class_type!(T).tp_init = - &Ctors.init_func; + //&Ctors.init_func; + &wrapped_ctors!(shim, C).init_func; } } template shim_impl(uint i, uint c=0) { @@ -571,11 +575,12 @@ template _wrap_class(T, Params...) { mixin _wrap_class!(T, symbolnameof!(T), Params); } +/ +//import std.stdio; template _wrap_class(_T, string name, Params...) { static if (is(_T == class)) { pragma(msg, "wrap_class: " ~ name); - mixin pyd.make_wrapper.make_wrapper!(_T, Params); - alias wrapper shim_class; + alias pyd.make_wrapper.make_wrapper!(_T, Params).wrapper shim_class; + //alias W.wrapper shim_class; alias _T T; // } else static if (is(_T == interface)) { // pragma(msg, "wrap_interface: " ~ name); @@ -589,17 +594,20 @@ template _wrap_class(_T, string name, Params...) { void wrap_class(string docstring="", string modulename="") { pragma(msg, "shim.mangleof: " ~ shim_class.mangleof); alias wrapped_class_type!(T) type; + //writefln("entering wrap_class for %s", typeid(T)); //pragma(msg, "wrap_class, T is " ~ prettytypeof!(T)); //Params params; + //writefln("before params: tp_init is %s", type.tp_init); foreach (param; Params) { static if (param.needs_shim) { - mixin param.call!(T) PCall; - PCall.call(); + //mixin param.call!(T) PCall; + param.call!(T, shim_class)(); } else { param.call!(T)(); } } + //writefln("after params: tp_init is %s", type.tp_init); assert(Pyd_Module_p(modulename) !is null, "Must initialize module before wrapping classes."); string module_name = toString(python.PyModule_GetName(Pyd_Module_p(modulename))); @@ -680,6 +688,7 @@ void wrap_class(string docstring="", string modulename="") { } } } + //writefln("after default check: tp_init is %s", type.tp_init); ////////////////// // Finalization // @@ -687,6 +696,7 @@ void wrap_class(string docstring="", string modulename="") { if (PyType_Ready(&type) < 0) { throw new Exception("Couldn't ready wrapped type!"); } + //writefln("after Ready: tp_init is %s", type.tp_init); python.Py_INCREF(cast(PyObject*)&type); python.PyModule_AddObject(Pyd_Module_p(modulename), (name~\0).ptr, cast(PyObject*)&type); @@ -696,6 +706,7 @@ void wrap_class(string docstring="", string modulename="") { wrapped_classes[T.classinfo] = &type; wrapped_classes[shim_class.classinfo] = &type; } + //writefln("leaving wrap_class for %s", typeid(T)); } } //////////////// @@ -783,8 +794,19 @@ T WrapPyObject_AsObject(T) (PyObject* _self) { alias wrapped_class_object!(T) wrapped_object; alias wrapped_class_type!(T) type; wrapped_object* self = cast(wrapped_object*)_self; - if (!is_wrapped!(T) || self is null || (is(T : Object) && cast(T)cast(Object)(self.d_obj) is null)) { - throw new Exception("Error extracting D object from Python object..."); + if (!is_wrapped!(T)) { + throw new Exception("Error extracting D object: Type " ~ objToStr(typeid(T)) ~ " is not wrapped."); + } + if (self is null) { + throw new Exception("Error extracting D object: 'self' was null!"); + } + static if (is(T == class)) { + if (cast(Object)(self.d_obj) is null) { + throw new Exception("Error extracting D object: Reference was not castable to Object!"); + } + if (cast(T)cast(Object)(self.d_obj) is null) { + throw new Exception("Error extracting D object: Object was not castable to type "~objToStr(typeid(T))~"."); + } } return self.d_obj; } diff --git a/infrastructure/pyd/ctor_wrap.d b/infrastructure/pyd/ctor_wrap.d index b94478a..0cea383 100644 --- a/infrastructure/pyd/ctor_wrap.d +++ b/infrastructure/pyd/ctor_wrap.d @@ -64,10 +64,11 @@ template wrapped_struct_init(T) { } } +//import std.stdio; // This template accepts a tuple of function pointer types, which each describe // a ctor of T, and uses them to wrap a Python tp_init function. -template wrapped_ctors(/*T,*/ C ...) { - alias shim_class T; +template wrapped_ctors(T, C ...) { + //alias shim_class T; alias wrapped_class_object!(T) wrap_object; extern(C) @@ -75,6 +76,7 @@ template wrapped_ctors(/*T,*/ C ...) { int len = PyObject_Length(args); return exception_catcher({ + //writefln("in init_func: len=%s, T=%s, C.length=%s", len, typeid(T), C.length); // Default ctor static if (is(typeof(new T))) { if (len == 0) { @@ -86,6 +88,7 @@ template wrapped_ctors(/*T,*/ C ...) { C c; foreach(i, arg; c) { alias ParameterTypeTuple!(typeof(arg)) Ctor; + //writefln(" init_func: i=%s, Ctor.length=%s, Ctor=%s", i, Ctor.length, typeid(typeof(arg))); if (Ctor.length == len) { auto fn = &call_ctor!(T, ParameterTypeTuple!(typeof(arg))); if (fn is null) { @@ -93,7 +96,7 @@ template wrapped_ctors(/*T,*/ C ...) { return -1; } alias typeof(fn) dg_t; - mixin applyPyTupleToDelegate!(dg_t); + //mixin applyPyTupleToDelegate!(dg_t) A; T t = applyPyTupleToDelegate(fn, args); if (t is null) { PyErr_SetString(PyExc_RuntimeError, "Class ctor redirect didn't return a class instance!");