Skip to content

Commit

Permalink
* Un-broke Init. (Oops.)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Aug 13, 2007
1 parent 6c5b4a7 commit ad1e457
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
42 changes: 32 additions & 10 deletions infrastructure/pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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)));
Expand Down Expand Up @@ -680,13 +688,15 @@ void wrap_class(string docstring="", string modulename="") {
}
}
}
//writefln("after default check: tp_init is %s", type.tp_init);

//////////////////
// Finalization //
//////////////////
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);

Expand All @@ -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));
}
}
////////////////
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions infrastructure/pyd/ctor_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ 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)
static int init_func(PyObject* self, PyObject* args, PyObject* kwds) {
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) {
Expand All @@ -86,14 +88,15 @@ 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) {
PyErr_SetString(PyExc_RuntimeError, "Couldn't get pointer to class ctor redirect.");
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!");
Expand Down

0 comments on commit ad1e457

Please sign in to comment.