Skip to content

Commit

Permalink
Merge pull request #171 from atilaneves/fix-enum
Browse files Browse the repository at this point in the history
pynih no longer has #170
  • Loading branch information
atilaneves committed Dec 1, 2019
2 parents 1bae55e + 5f07f7c commit 09721b0
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 10 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ export PYD_D_VERSION_13 ?= Python_3_8_Or_Later
all: test
test: test_python test_cs
test_python: test_python_pyd test_python_pynih
test_python_pyd: test_simple_pyd test_pyd_pyd test_issues_pyd test_numpy_pyd
test_python_pynih: test_simple_pynih test_pyd_pynih test_issues_pynih
test_python_pyd: test_simple_pyd test_pyd_pyd test_issues_pyd test_phobos_pyd test_numpy_pyd
test_python_pynih: test_simple_pynih test_pyd_pynih test_issues_pynih test_phobos_pynih
test_python_phobos: test_phobos_pynih test_phobos_pyd
test_cs: test_simple_cs

clean:
git clean -xffd

test_simple_pyd: tests/test_simple.py examples/simple/lib/pyd/simple.so
PYTHONPATH=$(PWD)/examples/simple/lib/pyd pytest -s -vv $<
PYTHONPATH=$(PWD)/examples/simple/lib/pyd PYD=1 pytest -s -vv $<

examples/simple/lib/pyd/simple.so: examples/simple/lib/pyd/libsimple.so
@cp $^ $@
Expand All @@ -47,7 +48,7 @@ test_simple_pynih_only: tests/test_simple_pynih_only.py examples/simple/lib/pyni
PYTHONPATH=$(PWD)/examples/simple/lib/pynih pytest -s -vv $<

test_simple_pynih: tests/test_simple.py examples/simple/lib/pynih/simple.so
PYTHONPATH=$(PWD)/examples/simple/lib/pynih pytest -s -vv $<
PYTHONPATH=$(PWD)/examples/simple/lib/pynih PYNIH=1 pytest -s -vv $<

test_simple_cs: examples/simple/lib/csharp/libsimple.x64.so examples/simple/Simple.cs
@cd tests/test_simple_cs && \
Expand Down Expand Up @@ -136,7 +137,7 @@ examples/numpy/lib/pynih/libnumpy.so: pynih/source/python/raw.d


test_phobos_pyd: tests/test_phobos.py examples/phobos/lib/pyd/phobos.so
PYTHONPATH=$(PWD)/examples/phobos/lib/pyd pytest -s -vv $<
PYTHONPATH=$(PWD)/examples/phobos/lib/pyd PYD=1 pytest -s -vv $<

examples/phobos/lib/pyd/phobos.so: examples/phobos/lib/pyd/libphobos.so
@cp $^ $@
Expand All @@ -148,7 +149,7 @@ examples/phobos/dub.selections.json:
@cd examples/phobos && dub upgrade -q

test_phobos_pynih: tests/test_phobos.py examples/phobos/lib/pynih/phobos.so
PYTHONPATH=$(PWD)/examples/phobos/lib/pynih pytest -s -vv $<
PYTHONPATH=$(PWD)/examples/phobos/lib/pynih PYNIH=1 pytest -s -vv $<

examples/phobos/lib/pynih/phobos.so: examples/phobos/lib/pynih/libphobos.so
@cp $^ $@
Expand Down
2 changes: 1 addition & 1 deletion csharp/source/autowrap/csharp/csharp.d
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public string generateCSharp(Modules...)(LibraryName libraryName, RootNamespace

static foreach(Agg; AllAggregates!Modules)
{
static if(verifySupported!Agg && !isDateTimeType!Agg)
static if(verifySupported!Agg && !isDateTimeType!Agg && !is(Agg == enum))
{
generateRangeDef!Agg(libraryName.value);
generateConstructors!Agg(libraryName.value);
Expand Down
2 changes: 1 addition & 1 deletion csharp/source/autowrap/csharp/dlang.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string wrapDLang(Modules...)() if(allSatisfy!(isModule, Modules)) {

static foreach(Agg; AllAggregates!Modules)
{
static if(verifySupported!Agg && !isDateTimeType!Agg)
static if(verifySupported!Agg && !isDateTimeType!Agg && !is(Agg == enum))
{
ret ~= generateSliceMethods!Agg(imports);
ret ~= generateConstructors!Agg(imports);
Expand Down
7 changes: 7 additions & 0 deletions examples/simple/source/wrap_all.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ struct String {
string otherStringAsParam(OtherString s) {
return s.s ~ "quux";
}


enum MyEnum {
foo,
bar,
baz,
}
1 change: 1 addition & 0 deletions pynih/source/python/raw.dpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module python.raw;
#include "Python.h"
#include "datetime.h"
#include "structmember.h"
#include "enumobject.h"


enum isPython3 = is(PyModuleDef);
Expand Down
26 changes: 26 additions & 0 deletions pynih/source/python/type.d
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ struct PythonType(T) {
} else static if(isCallable!T) {
_pyType.tp_basicsize = PythonCallable!T.sizeof;
_pyType.tp_call = &PythonCallable!T._py_call;
} else static if(is(T == enum)) {
import python.raw: PyEnum_Type;
_pyType.tp_basicsize = 0;
_pyType.tp_base = &PyEnum_Type;
try
_pyType.tp_dict = classDict;
catch(Exception e) {
import core.stdc.stdio;
enum msg = "Could not create class dict for " ~ T.stringof ~ "\n";
printf(msg);
}
} else
static assert(false, "Don't know what to do for type " ~ T.stringof);

Expand All @@ -205,6 +216,21 @@ struct PythonType(T) {
}
}

static if(is(T == enum)) {
private static PyObject* classDict() {
import python.conv.d_to_python: toPython;
import std.traits: EnumMembers, OriginalType;

OriginalType!T[string] dict;

static foreach(i; 0 .. EnumMembers!T.length) {
dict[__traits(identifier, EnumMembers!T[i])] = EnumMembers!T[i];
}

return dict.toPython;
}
}

static if(isUserAggregate!T)
private static auto getsetDefs() {
import python.raw: PyGetSetDef;
Expand Down
2 changes: 1 addition & 1 deletion reflection/source/autowrap/reflection.d
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ template isUserAggregate(A...) if(A.length == 1) {
!is(Unqual!T == TimeOfDay) &&
!is(Unqual!T == Duration) &&
!isInstanceOf!(Tuple, T) &&
(is(T == struct) || is(T == class));
(is(T == struct) || is(T == class) || is(T == enum));
}


Expand Down
19 changes: 18 additions & 1 deletion tests/test_phobos.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
import os
import pytest

is_pyd = os.environ.get('PYD')
is_pynih = os.environ.get('PYNIH')


def test_protocol():
from phobos import Protocol
if is_pyd:
with pytest.raises(ImportError):
from phobos import Protocol
else:
from phobos import Protocol, ProtocolType

protocol = Protocol()
assert protocol.getProtocolByType(ProtocolType.TCP)

assert protocol.name == "tcp"
assert protocol.aliases == ["TCP"]
19 changes: 19 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from datetime import date
import pytest
import os

is_pyd = os.environ.get('PYD')
is_pynih = os.environ.get('PYNIH')


def test_adder():
Expand Down Expand Up @@ -261,3 +265,18 @@ def test_property_getter_setter():
assert obj.i == 42
obj.i = 33 # shouldn't throw
assert obj.i == 33


def test_enum():
import pytest
if is_pyd: # FIXME
with pytest.raises(ImportError):
from simple import MyEnum
else:
from simple import MyEnum
assert MyEnum.foo == 0
assert MyEnum.bar == 1
assert MyEnum.baz == 2

with pytest.raises(AttributeError): # no quux
assert MyEnum.quux == 42
12 changes: 12 additions & 0 deletions tests/ut/pynih/python/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,15 @@ unittest {
import core.time: seconds;
backAndForth(1.seconds);
}


@("enum")
unittest {
static enum Enum {
foo,
bar,
baz,
}

backAndForth(Enum.bar);
}

0 comments on commit 09721b0

Please sign in to comment.