Skip to content

Commit

Permalink
Updated infrastructure code to compile with DMD 2.052 (at least on x8…
Browse files Browse the repository at this point in the history
…6 arch), while keeping backwards compatibility with D1.
  • Loading branch information
dansanduleac committed Jun 1, 2011
1 parent 8f1ac92 commit 32d6e39
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 240 deletions.
170 changes: 85 additions & 85 deletions infrastructure/meta/Demangle.d

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions infrastructure/meta/Nameof.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private {
// We will get the .mangleof for a pointer to this function pointer.
template rawmanglednameof(alias A)
{
const char [] rawmanglednameof =
const string rawmanglednameof =
typeof(&outer!(A)).mangleof;
}

Expand Down Expand Up @@ -90,10 +90,10 @@ template manglednameof(alias A)
{
static if (rawmanglednameof!(A).length - pointerstartlength <= 100 + 1) {
// the length of the template argument requires 2 characters
const char [] manglednameof =
const string manglednameof =
rawmanglednameof!(A)[ pointerstartlength + 2 .. $ - pointerendlength];
} else
const char [] manglednameof =
const string manglednameof =
rawmanglednameof!(A)[ pointerstartlength + 3 .. $ - pointerendlength];
}

Expand All @@ -104,7 +104,7 @@ template manglednameof(alias A)
*/
template prettynameof(alias A)
{
const char [] prettynameof = prettyTemplateArg!(manglednameof!(A), MangledNameType.PrettyName);
const string prettynameof = prettyTemplateArg!(manglednameof!(A), MangledNameType.PrettyName);
}

/** Convert any D type to a human-readable string literal
Expand All @@ -113,7 +113,7 @@ template prettynameof(alias A)
*/
template prettytypeof(A)
{
const char [] prettytypeof = demangleType!(A.mangleof, MangledNameType.PrettyName);
const string prettytypeof = demangleType!(A.mangleof, MangledNameType.PrettyName);
}

/**
Expand All @@ -125,7 +125,7 @@ template prettytypeof(A)
*/
template qualifiednameof(alias A)
{
const char [] qualifiednameof = prettyTemplateArg!(manglednameof!(A), MangledNameType.QualifiedName);
const string qualifiednameof = prettyTemplateArg!(manglednameof!(A), MangledNameType.QualifiedName);
}

/**
Expand All @@ -135,7 +135,7 @@ template qualifiednameof(alias A)
*/
template symbolnameof(alias A)
{
const char [] symbolnameof = prettyTemplateArg!(manglednameof!(A), MangledNameType.SymbolName);
const string symbolnameof = prettyTemplateArg!(manglednameof!(A), MangledNameType.SymbolName);
}

//----------------------------------------------
Expand Down Expand Up @@ -173,7 +173,7 @@ enum SomeEnum { ABC = 2 }
SomeEnum SomeInt;

// remove the ".d" from the end
const char [] THISFILE = "meta.Nameof";
const string THISFILE = "meta.Nameof";

static assert( prettytypeof!(real) == "real");
static assert( prettytypeof!(OuterClass.SomeClass) == "class " ~ THISFILE ~".OuterClass.SomeClass");
Expand Down
46 changes: 23 additions & 23 deletions infrastructure/pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import pyd.lib_abstract :
ReturnType,
minArgs,
objToStr,
ToString
toStringNow
;

//import meta.Default;
Expand Down Expand Up @@ -314,20 +314,20 @@ template _Def(alias fn, /*string _realname,*/ string name, fn_t/+, uint MIN_ARGS
pragma(msg, "class.def: " ~ name);
static PyMethodDef empty = { null, null, 0, null };
alias wrapped_method_list!(T) list;
list[length-1].ml_name = (name ~ \0).ptr;
list[length-1].ml_name = (name ~ "\0").ptr;
list[length-1].ml_meth = &method_wrap!(T, fn, fn_t).func;
list[length-1].ml_flags = METH_VARARGS;
list[length-1].ml_doc = (docstring~\0).ptr;
list[length-1].ml_doc = (docstring~"\0").ptr;
list ~= empty;
// It's possible that appending the empty item invalidated the
// pointer in the type struct, so we renew it here.
wrapped_class_type!(T).tp_methods = list.ptr;
}
template shim(uint i) {
const char[] shim =
" alias Params["~ToString!(i)~"] __pyd_p"~ToString!(i)~";\n"
" ReturnType!(__pyd_p"~ToString!(i)~".func_t) "~realname~"(ParameterTypeTuple!(__pyd_p"~ToString!(i)~".func_t) t) {\n"
" return __pyd_get_overload!(\""~realname~"\", __pyd_p"~ToString!(i)~".func_t).func(\""~name~"\", t);\n"
" alias Params["~toStringNow!(i)~"] __pyd_p"~toStringNow!(i)~";\n"
" ReturnType!(__pyd_p"~toStringNow!(i)~".func_t) "~realname~"(ParameterTypeTuple!(__pyd_p"~toStringNow!(i)~".func_t) t) {\n"
" return __pyd_get_overload!(\""~realname~"\", __pyd_p"~toStringNow!(i)~".func_t).func(\""~name~"\", t);\n"
" }\n";
}
}
Expand Down Expand Up @@ -373,10 +373,10 @@ template _StaticDef(alias fn,/+ string _realname,+/ string name, fn_t, uint MIN_
pragma(msg, "class.static_def: " ~ name);
static PyMethodDef empty = { null, null, 0, null };
alias wrapped_method_list!(T) list;
list[length-1].ml_name = (name ~ \0).ptr;
list[length-1].ml_name = (name ~ "\0").ptr;
list[length-1].ml_meth = &function_wrap!(fn, MIN_ARGS, fn_t).func;
list[length-1].ml_flags = METH_VARARGS | METH_STATIC;
list[length-1].ml_doc = (docstring~\0).ptr;
list[length-1].ml_doc = (docstring~"\0").ptr;
list ~= empty;
wrapped_class_type!(T).tp_methods = list;
}
Expand Down Expand Up @@ -427,14 +427,14 @@ template _Property(alias fn, string _realname, string name, bool RO, string docs
static void call(T) () {
pragma(msg, "class.prop: " ~ name);
static PyGetSetDef empty = { null, null, null, null, null };
wrapped_prop_list!(T)[length-1].name = (name ~ \0).ptr;
wrapped_prop_list!(T)[length-1].name = (name ~ "\0").ptr;
wrapped_prop_list!(T)[length-1].get =
&wrapped_get!(T, fn).func;
static if (!RO) {
wrapped_prop_list!(T)[length-1].set =
&wrapped_set!(T, fn).func;
}
wrapped_prop_list!(T)[length-1].doc = (docstring~\0).ptr;
wrapped_prop_list!(T)[length-1].doc = (docstring~"\0").ptr;
wrapped_prop_list!(T)[length-1].closure = null;
wrapped_prop_list!(T) ~= empty;
// It's possible that appending the empty item invalidated the
Expand All @@ -447,16 +447,16 @@ template _Property(alias fn, string _realname, string name, bool RO, string docs
const char[] shim_setter = "";
} else {
const char[] shim_setter =
" ReturnType!(__pyd_p"~ToString!(i)~".set_t) "~_realname~"(ParameterTypeTuple!(__pyd_p"~ToString!(i)~".set_t) t) {\n"
" return __pyd_get_overload!(\""~_realname~"\", __pyd_p"~ToString!(i)~".set_t).func(\""~name~"\", t);\n"
" ReturnType!(__pyd_p"~toStringNow!(i)~".set_t) "~_realname~"(ParameterTypeTuple!(__pyd_p"~toStringNow!(i)~".set_t) t) {\n"
" return __pyd_get_overload!(\""~_realname~"\", __pyd_p"~toStringNow!(i)~".set_t).func(\""~name~"\", t);\n"
" }\n";
}
}
template shim(uint i) {
const char[] shim =
" alias Params["~ToString!(i)~"] __pyd_p"~ToString!(i)~";\n"
" ReturnType!(__pyd_p"~ToString!(i)~".get_t) "~_realname~"() {\n"
" return __pyd_get_overload!(\""~_realname~"\", __pyd_p"~ToString!(i)~".get_t).func(\""~name~"\");\n"
" alias Params["~toStringNow!(i)~"] __pyd_p"~toStringNow!(i)~";\n"
" ReturnType!(__pyd_p"~toStringNow!(i)~".get_t) "~_realname~"() {\n"
" return __pyd_get_overload!(\""~_realname~"\", __pyd_p"~toStringNow!(i)~".get_t).func(\""~name~"\");\n"
" }\n" ~
shim_setter!(i);
}
Expand Down Expand Up @@ -502,7 +502,7 @@ struct Init(C ...) {
template shim_impl(uint i, uint c=0) {
static if (c < ctors.length) {
const char[] shim_impl =
" this(ParameterTypeTuple!(__pyd_c"~ToString!(i)~"["~ToString!(c)~"]) t) {\n"
" this(ParameterTypeTuple!(__pyd_c"~toStringNow!(i)~"["~toStringNow!(c)~"]) t) {\n"
" super(t);\n"
" }\n" ~ shim_impl!(i, c+1);
} else {
Expand All @@ -514,8 +514,8 @@ struct Init(C ...) {
}
template shim(uint i) {
const char[] shim =
" alias Params["~ToString!(i)~"] __pyd_p"~ToString!(i)~";\n"
" alias __pyd_p"~ToString!(i)~".ctors __pyd_c"~ToString!(i)~";\n"~
" alias Params["~toStringNow!(i)~"] __pyd_p"~toStringNow!(i)~";\n"
" alias __pyd_p"~toStringNow!(i)~".ctors __pyd_c"~toStringNow!(i)~";\n"~
shim_impl!(i);
}
}
Expand Down Expand Up @@ -554,10 +554,10 @@ struct AltIter(alias fn, string name = symbolnameof!(fn), iter_t = ParameterType
static PyMethodDef empty = { null, null, 0, null };
alias wrapped_method_list!(T) list;
PydStackContext_Ready();
list[length-1].ml_name = name ~ \0;
list[length-1].ml_name = name ~ "\0";
list[length-1].ml_meth = cast(PyCFunction)&wrapped_iter!(T, fn, int function(iter_t)).iter;
list[length-1].ml_flags = METH_VARARGS;
list[length-1].ml_doc = "";//(docstring ~ \0).ptr;
list[length-1].ml_doc = "";//(docstring ~ "\0").ptr;
list ~= empty;
// It's possible that appending the empty item invalidated the
// pointer in the type struct, so we renew it here.
Expand Down Expand Up @@ -617,11 +617,11 @@ void wrap_class(string docstring="", string modulename="") {
//////////////////
type.ob_type = python.PyType_Type_p();
type.tp_basicsize = (wrapped_class_object!(T)).sizeof;
type.tp_doc = (docstring ~ \0).ptr;
type.tp_doc = (docstring ~ "\0").ptr;
type.tp_flags = python.Py_TPFLAGS_DEFAULT | python.Py_TPFLAGS_BASETYPE;
//type.tp_repr = &wrapped_repr!(T).repr;
type.tp_methods = wrapped_method_list!(T).ptr;
type.tp_name = (module_name ~ "." ~ name ~ \0).ptr;
type.tp_name = (module_name ~ "." ~ name ~ "\0").ptr;

/////////////////
// Inheritance //
Expand Down Expand Up @@ -698,7 +698,7 @@ void wrap_class(string docstring="", string modulename="") {
}
//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);
python.PyModule_AddObject(Pyd_Module_p(modulename), (name~"\0").ptr, cast(PyObject*)&type);

is_wrapped!(T) = true;
static if (is(T == class)) {
Expand Down
14 changes: 8 additions & 6 deletions infrastructure/pyd/def.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ PyObject* Pyd_Module_p(string modulename="") {
*>>> print testdll.foo(20)
*It's greater than 10!)
*/

void def(alias fn, string name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) (string docstring="") {
def!("", fn, name, fn_t, MIN_ARGS)(docstring);
}
Expand All @@ -94,10 +95,11 @@ void def(string modulename, alias fn, string name = symbolnameof!(fn), fn_t=type
ready_module_methods(modulename);
PyMethodDef[]* list = &module_methods[modulename];

(*list)[length-1].ml_name = (name ~ \0).ptr;
(*list)[length-1].ml_meth = &function_wrap!(fn, MIN_ARGS, fn_t).func;
(*list)[length-1].ml_flags = METH_VARARGS;
(*list)[length-1].ml_doc = (docstring ~ \0).ptr;
// was: length-1
(*list)[$-1].ml_name = (name ~ "\0").ptr;
(*list)[$-1].ml_meth = &function_wrap!(fn, MIN_ARGS, fn_t).func;
(*list)[$-1].ml_flags = METH_VARARGS;
(*list)[$-1].ml_doc = (docstring ~ "\0").ptr;
(*list) ~= empty;
}

Expand All @@ -110,7 +112,7 @@ PyObject* module_init(string docstring="") {
//_loadPythonSupport();
string name = pyd_module_name;
ready_module_methods("");
pyd_modules[""] = Py_InitModule3((name ~ \0).ptr, module_methods[""].ptr, (docstring ~ \0).ptr);
pyd_modules[""] = Py_InitModule3((name ~ "\0").ptr, module_methods[""].ptr, (docstring ~ "\0").ptr);
return pyd_modules[""];
}

Expand All @@ -119,7 +121,7 @@ PyObject* module_init(string docstring="") {
*/
PyObject* add_module(string name, string docstring="") {
ready_module_methods(name);
pyd_modules[name] = Py_InitModule3((name ~ \0).ptr, module_methods[name].ptr, (docstring ~ \0).ptr);
pyd_modules[name] = Py_InitModule3((name ~ "\0").ptr, module_methods[name].ptr, (docstring ~ "\0").ptr);
return pyd_modules[name];
}

18 changes: 14 additions & 4 deletions infrastructure/pyd/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import pyd.lib_abstract :
;
//import meta.Nameof;
//import std.string;
version (D_Version2) {
import std.conv : to;
}

/**
* This function first checks if a Python exception is set, and then (if one
Expand Down Expand Up @@ -69,6 +72,9 @@ T error_code(T) () {
*})
*/
T exception_catcher(T) (T delegate() dg) {
version (D_Version2) {} else {
alias Object Throwable;
}
try {
return dg();
}
Expand All @@ -81,12 +87,12 @@ T exception_catcher(T) (T delegate() dg) {
// A D exception was raised and should be translated into a meaningful
// Python exception.
catch (Exception e) {
PyErr_SetString(PyExc_RuntimeError, ("D Exception: " ~ e.classinfo.name ~ ": " ~ e.msg ~ \0).ptr);
PyErr_SetString(PyExc_RuntimeError, ("D Exception: " ~ e.classinfo.name ~ ": " ~ e.msg ~ "\0").ptr);
return error_code!(T)();
}
// Some other D object was thrown. Deal with it.
catch (Object o) {
PyErr_SetString(PyExc_RuntimeError, ("thrown D Object: " ~ o.classinfo.name ~ ": " ~ objToStr(o) ~ \0).ptr);
catch (Throwable o) {
PyErr_SetString(PyExc_RuntimeError, ("thrown D Object: " ~ o.classinfo.name ~ ": " ~ objToStr(o) ~ "\0").ptr);
return error_code!(T)();
}
}
Expand All @@ -103,7 +109,11 @@ protected:
PyObject* m_type, m_value, m_trace;
public:
this(PyObject* type, PyObject* value, PyObject* traceback) {
super(.toString(PyString_AsString(value)));
version (D_Version2) {
super(.to!(string)(PyString_AsString(value)));
} else {
super(.toString(PyString_AsString(value)));
}
m_type = type;
m_value = value;
m_trace = traceback;
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/pyd/func_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void setWrongArgsError(int gotArgs, uint minArgs, uint maxArgs, string funcName=
}

char[] argStr(int args) {
char[] temp = toString(args) ~ " argument";
char[] temp = cast(char[])(toString(args) ~ " argument");
if (args > 1) {
temp ~= "s";
}
Expand All @@ -88,7 +88,7 @@ void setWrongArgsError(int gotArgs, uint minArgs, uint maxArgs, string funcName=
}
str ~= " (" ~ toString(gotArgs) ~ " given)";

PyErr_SetString(PyExc_TypeError, (str ~ \0).ptr);
PyErr_SetString(PyExc_TypeError, (str ~ "\0").ptr);
}

// Calls callable alias fn with PyTuple args.
Expand Down
28 changes: 23 additions & 5 deletions infrastructure/pyd/lib_abstract.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,40 @@ version (Tango) {
string objToStr(Object o) {
return o.toString();
}
/*
version (D_Version2) {
// D1 issues?
template symbolnameof(alias symbol) {
static if (is(typeof(symbol) == function)) {
const char[] symbolnameof = (&symbol).stringof[2 .. $];
string symbolnameof = (&symbol).stringof[2 .. $];
} else {
const char[] symbolnameof = symbol.stringof;
string symbolnameof = symbol.stringof;
}
}
} else {
public import meta.Nameof : symbolnameof;
}
}*/

public import meta.Nameof : symbolnameof;
public import meta.Nameof : /*symbolnameof,*/ prettytypeof, prettynameof;

public import std.string : toString;
version (D_Version2) {
public import std.conv : to;
// provide a "wrapper" using the old toString
auto toString(T)(T obj) {
return to!(string)(obj);
}
} else {
// deprecated in D2
public import std.string : toString;
}
public import std.traits : ParameterTypeTuple, ReturnType;
public import meta.Default : minArgs;
public import std.metastrings : ToString;
version (D_Version2) {
public import std.metastrings : toStringNow;
} else {
// deprecated in D2
public import std.metastrings : ToString;
alias ToString toStringNow;
}
}
2 changes: 1 addition & 1 deletion infrastructure/pyd/make_object.d
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ PyObject* _py(T) (T t) {
} else static if (is(T : cdouble)) {
return PyComplex_FromDoubles(t.re, t.im);
} else static if (is(T : string)) {
return PyString_FromString((t ~ \0).ptr);
return PyString_FromString((t ~ "\0").ptr);
} else static if (is(T : wchar[])) {
return PyUnicode_FromWideChar(t, t.length);
// Converts any array (static or dynamic) to a Python list
Expand Down
Loading

0 comments on commit 32d6e39

Please sign in to comment.