Skip to content

Commit

Permalink
Support for one-at-a-time compilation. (Preparing for GDC support.)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.dsource.org/projects/pyd/trunk@51 1df65b71-e716-0410-9316-ac55df2b1602
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Dec 4, 2006
1 parent b4a7dfd commit 982576a
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 75 deletions.
6 changes: 3 additions & 3 deletions dcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def compile(self, sources,
# Compiling one-by-one exhibits a strange bug in the D front-end, while
# compiling all at once works. This flags allows me to test each form
# easily. Supporting the one-by-one form is synonymous with GDC support.
ONE_BY_ONE = False
ONE_BY_ONE = True
if ONE_BY_ONE:
for source in sources:
outOpts = outputOpts[:]
Expand Down Expand Up @@ -515,9 +515,9 @@ def _initialize(self):
# _debugOpt
self._debugOpt = '-fdebug=%s'
# _defaultOptimizeOpts
self._defaultOptimizeOpts = ['-fdebug', '-funittest']
self._defaultOptimizeOpts = ['-fdebug']
# _debugOptimizeOpts
self._debugOptimizeOpts = self._defaultOptimizeOpts + ['-g']
self._debugOptimizeOpts = self._defaultOptimizeOpts + ['-g', '-funittest']
# _releaseOptimizeOpts
self._releaseOptimizeOpts = ['-fversion=Optimized', '-frelease', '-O3', '-finline-functions']

Expand Down
2 changes: 1 addition & 1 deletion html_doc/install.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h3>Requirements</h3>

<p>Pyd requires Python 2.4 or newer and the latest version of DMD.</p>

<p>At the moment, Pyd is only supported on Windows using the <a href="http://digitalmars.com/d/index.html">DMD</a> compiler. GDC support has been written, but Pyd cannot yet be compiled with GDC (due to <a href="http://d.puremagic.com/issues/show_bug.cgi?id=311">this bug</a>). Once GDC support is available, Pyd should work on Linux. Support for Derek Parnell's <code>bud</code> is also planned.</p>
<p>At the moment, Pyd is only supported on Windows using the <a href="http://digitalmars.com/d/index.html">DMD</a> compiler. Linux support is contingent on GDC catching up to at least DMD 0.176. Support for Derek Parnell's <code>bud</code> is also planned.</p>

<p>Because Pyd is still in development, it is only available via Subversion. The repository is located <a href="http://svn.dsource.org/projects/pyd/trunk">here</a>.</p>

Expand Down
38 changes: 18 additions & 20 deletions infrastructure/pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@ SOFTWARE.
*/
module pyd.class_wrap;

private {
import python;

import pyd.ctor_wrap;
import pyd.def;
import pyd.exception;
import pyd.func_wrap;
version(Pyd_with_StackThreads) {
import pyd.iteration;
}
import pyd.make_object;
import pyd.op_wrap;
import python;

import pyd.ctor_wrap;
import pyd.def;
import pyd.exception;
import pyd.func_wrap;
version(Pyd_with_StackThreads) {
import pyd.iteration;
}
import pyd.make_object;
import pyd.op_wrap;

import meta.Default;
import meta.Nameof;
import meta.Default;
import meta.Nameof;

import std.string;
import std.traits;
}
import std.string;
import std.traits;

bool[TypeInfo] wrapped_classes;

Expand Down Expand Up @@ -363,14 +361,14 @@ template wrapped_class(T, char[] classname = symbolnameof!(T)) {
* Finalize the wrapping of the class. It is neccessary to call this after all
* calls to the wrapped_class member functions.
*/
void finalize_class(CLS) (CLS cls) {
void finalize_class(CLS) (CLS cls, char[] modulename="") {
alias typeof(cls.t) T;
alias wrapped_class_type!(T) type;
const char[] name = CLS._name;
pragma(msg, "finalize_class: " ~ name);

assert(Pyd_Module_p !is null, "Must initialize module before wrapping classes.");
char[] module_name = toString(PyModule_GetName(Pyd_Module_p));
char[] module_name = toString(PyModule_GetName(Pyd_Module_p(modulename)));
// Fill in missing values
type.ob_type = PyType_Type_p();
//type.tp_new = &(wrapped_methods!(T).wrapped_new);
Expand Down Expand Up @@ -425,7 +423,7 @@ void finalize_class(CLS) (CLS cls) {
throw new Exception("Couldn't ready wrapped type!");
}
Py_INCREF(cast(PyObject*)&type);
PyModule_AddObject(Pyd_Module_p, name, cast(PyObject*)&type);
PyModule_AddObject(Pyd_Module_p(modulename), name, cast(PyObject*)&type);
is_wrapped!(T) = true;
wrapped_classes[typeid(T)] = true;
}
Expand Down
49 changes: 31 additions & 18 deletions infrastructure/pyd/def.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ private import pyd.func_wrap;
private import meta.Default;
private import meta.Nameof;

private
PyMethodDef module_global_methods[] = [
private PyMethodDef module_global_methods[] = [
{ null, null, 0, null }
];

private
PyObject* m_module;
private PyMethodDef[][char[]] module_methods;
private PyObject*[char[]] pyd_modules;

PyObject* Pyd_Module_p() {
return m_module;
PyObject* Pyd_Module_p(char[] modulename="") {
return pyd_modules[modulename];
}

/**
Expand Down Expand Up @@ -72,26 +71,40 @@ PyObject* Pyd_Module_p() {
*>>> print testdll.foo(20)
*It's greater than 10!)
*/
template def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) {
pragma(msg, "def: " ~ name);
void def() {
PyMethodDef empty;
alias module_global_methods list;
void def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) () {
def!("", fn, name, fn_t, MIN_ARGS)();
}

list[length-1].ml_name = name ~ \0;
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 = "";
list ~= empty;
void def(char[] modulename, alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) () {
pragma(msg, "def: " ~ name);
PyMethodDef empty;
if (!(modulename in module_methods)) {
module_methods[modulename] = (PyMethodDef[]).init;
module_methods[modulename] ~= empty;
}
PyMethodDef[]* list = &module_methods[modulename];

(*list)[length-1].ml_name = name ~ \0;
(*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 = "";
(*list) ~= empty;
}

/**
* Module initialization function. Should be called after the last call to def.
*/
PyObject* module_init(char[] name) {
//_loadPythonSupport();
m_module = Py_InitModule(name ~ \0, module_global_methods);
return m_module;
pyd_modules[""] = Py_InitModule(name ~ \0, module_methods[""].ptr);
return pyd_modules[""];
}

/**
* Module initialization function. Should be called after the last call to def.
*/
PyObject* add_module(char[] name) {
pyd_modules[name] = Py_InitModule(name ~ \0, module_methods[name].ptr);
return pyd_modules[name];
}

2 changes: 1 addition & 1 deletion infrastructure/pyd/dg_convert.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SOFTWARE.
*/
module pyd.dg_convert;

private import std.traits;
import std.traits;

template fn_to_dgT(Fn) {
alias ParameterTypeTuple!(Fn) T;
Expand Down
16 changes: 7 additions & 9 deletions infrastructure/pyd/iteration.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ SOFTWARE.
*/
module pyd.iteration;

private {
import python;
import python;

import pyd.class_wrap;
import pyd.dg_convert;
import pyd.exception;
import pyd.make_object;
import pyd.class_wrap;
import pyd.dg_convert;
import pyd.exception;
import pyd.make_object;

import std.traits;
import std.traits;

import st.stackcontext;
}
import st.stackcontext;

// This exception is for yielding a PyObject* from within a StackContext.
class PydYield : Exception {
Expand Down
17 changes: 11 additions & 6 deletions infrastructure/pyd/make_object.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ SOFTWARE.
*/
module pyd.make_object;

private import python;
import python;

private import std.string;
import std.string;

private import pyd.pydobject;
private import pyd.class_wrap;
private import pyd.func_wrap;
private import pyd.exception;
import pyd.pydobject;
import pyd.class_wrap;
import pyd.func_wrap;
import pyd.exception;

private template isArray(T) {
const bool isArray = is(typeof(T.init[0])[] == T);
Expand Down Expand Up @@ -226,6 +226,9 @@ T d_type(T) (PyObject* o) {
Py_INCREF(Py_None);
return Py_None;
} else static if (is(T == class)) {
static if (is(T == Object)) {
pragma(msg, "d_type: T is Object");
}
// We can only convert to a class if it has been wrapped, and of course
// we can only convert the object if it is the wrapped type.
if (is_wrapped!(T) && PyObject_TypeCheck(o, &wrapped_class_type!(T))) {
Expand Down Expand Up @@ -289,6 +292,8 @@ T d_type(T) (PyObject* o) {
}
}

alias d_type!(Object) d_type_Object;

private
void could_not_convert(T) (PyObject* o) {
// Pull out the name of the type of this Python object, and the
Expand Down
22 changes: 12 additions & 10 deletions infrastructure/pyd/op_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ SOFTWARE.
*/
module pyd.op_wrap;

private import python;
import python;

private import pyd.class_wrap;
private import pyd.dg_convert;
private import pyd.func_wrap;
private import pyd.exception;
private import pyd.make_object;
import pyd.class_wrap;
import pyd.dg_convert;
import pyd.func_wrap;
import pyd.exception;
import pyd.make_object;

private import meta.Nameof;
import meta.Nameof;

private import std.traits;
import std.traits;

version(Python_2_5_Or_Later) {
alias Py_ssize_t index_t;
Expand Down Expand Up @@ -122,10 +122,11 @@ template opfunc_binary_wrap(T, alias opfn) {
alias wrapped_class_object!(T) wrap_object;
alias ParameterTypeTuple!(opfn) Info;
alias ReturnType!(opfn) Ret;
alias dg_wrapper!(T, typeof(&opfn)) get_dg;
extern(C)
PyObject* func(PyObject* self, PyObject* o) {
return exception_catcher(delegate PyObject*() {
auto dg = dg_wrapper((cast(wrap_object*)self).d_obj, &opfn);
auto dg = get_dg((cast(wrap_object*)self).d_obj, &opfn);
pragma(msg, prettytypeof!(typeof(dg)));
pragma(msg, symbolnameof!(opfn));
static if (is(Ret == void)) {
Expand Down Expand Up @@ -192,6 +193,7 @@ template opindex_mapping_pyfunc(T) {
});
}
} else {
alias method_wrap!(T, T.opIndex, typeof(&T.opIndex)) opindex_methodT;
extern(C)
PyObject* func(PyObject* self, PyObject* key) {
int args;
Expand All @@ -204,7 +206,7 @@ template opindex_mapping_pyfunc(T) {
setWrongArgsError(args, ARGS, ARGS);
return null;
}
return method_wrap!(T, T.opIndex, typeof(&T.opIndex)).func(self, key);
return opindex_methodT.func(self, key);
}
}
}
Expand Down
22 changes: 15 additions & 7 deletions infrastructure/pyd/pyd.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ SOFTWARE.
*/
module pyd.pyd;

public import pyd.def;
public import pyd.class_wrap;
//public import pyd.ctor_wrap;
public import pyd.pydobject;
public import pyd.exception;
public import pyd.func_wrap;
public import pyd.make_object;
public {
import pyd.class_wrap;
import pyd.def;
import pyd.exception;
import pyd.func_wrap;
import pyd.make_object;
import pyd.pydobject;

// Importing these is only needed as a workaround to bug #311
import pyd.ctor_wrap;
import pyd.dg_convert;
import pyd.exception;
import pyd.func_wrap;
import pyd.iteration;
}

0 comments on commit 982576a

Please sign in to comment.