Skip to content

Commit

Permalink
Merge pull request symmetryinvestments#266 from atilaneves/better-man…
Browse files Browse the repository at this point in the history
…ual-bindings

Better pynih manual bindings
  • Loading branch information
atilaneves committed Sep 21, 2020
2 parents 1ef8a37 + 5466b39 commit e96e35c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
38 changes: 38 additions & 0 deletions pynih/source/python/boilerplate.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ import python.raw: isPython2, isPython3;
import std.traits: isFunction;


string createModuleRecipe(Module module_, alias cfunctions, alias aggregates = Aggregates!())
()
{
import std.format: format;

return q{
extern(C) export auto PyInit_%s() nothrow {
import python.cooked: createModule;
import python.boilerplate: commonInit, Module, CFunctions, Aggregates;

commonInit;

return createModule!(
Module("%s"),
%s,
%s,
);
}

}.format(
module_.name,
module_.name,
cfunctions.stringof,
aggregates.stringof,
);
}


void commonInit() nothrow {
import python.raw: pyDateTimeImport;
import core.runtime: rt_init;

try
rt_init;
catch(Exception _)
assert(0);
}

/// For a nicer API
struct Module {
string name;
Expand Down
2 changes: 1 addition & 1 deletion pynih/source/python/cooked.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import python.boilerplate: Module, CFunctions, Aggregates;
Creates a Python3 module from the given C functions.
Each function has the same name in Python.
*/
auto createModule(Module module_, alias cfunctions, alias aggregates)()
auto createModule(Module module_, alias cfunctions, alias aggregates = Aggregates!())()
if(isPython3 &&
is(cfunctions == CFunctions!F, F...) &&
is(aggregates == Aggregates!T, T...))
Expand Down
9 changes: 4 additions & 5 deletions pynih/source/python/type.d
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ struct PythonType(T) {
static bool failedToReady;

static PyObject* pyObject() {
initialise;
return failedToReady ? null : cast(PyObject*) &_pyType;
return cast(PyObject*) pyType;
}

static PyTypeObject* pyType() nothrow {
Expand All @@ -74,7 +73,7 @@ struct PythonType(T) {

if(_pyType != _pyType.init) return;

// This allows tp_name to do its usual Python job and allos us to
// This allows tp_name to do its usual Python job and allows us to
// construct a D class from its runtime Python type.
_pyType.tp_name = fullyQualifiedName!(Unqual!T).ptr;
_pyType.tp_flags = TypeFlags.Default;
Expand Down Expand Up @@ -542,7 +541,7 @@ private string dlangAssignOpToPythonSlot(string op) {
}


private auto pythonArgsToDArgs(bool isVariadic, P...)(PyObject* args, PyObject* kwargs)
auto pythonArgsToDArgs(bool isVariadic, P...)(PyObject* args, PyObject* kwargs)
if(allSatisfy!(isParameter, P))
{
import python.raw: PyTuple_Size, PyTuple_GetItem, PyTuple_GetSlice, pyUnicodeDecodeUTF8, PyDict_GetItem;
Expand Down Expand Up @@ -669,7 +668,7 @@ struct PythonFunction(alias F) {
}


private auto noThrowable(alias F, A...)(auto ref A args) {
auto noThrowable(alias F, A...)(auto ref A args) {
import python.raw: PyErr_SetString, PyExc_RuntimeError;
import std.string: toStringz;
import std.traits: ReturnType;
Expand Down

0 comments on commit e96e35c

Please sign in to comment.