Skip to content

Commit

Permalink
Merge pull request #270 from atilaneves/fix-268
Browse files Browse the repository at this point in the history
pynih supports #268
  • Loading branch information
atilaneves authored Oct 28, 2020
2 parents b1b2d4b + 5b84ed8 commit 3e8bc99
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/issues/source/issues.d
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,8 @@ struct MethodWithScopeSafeDelegate {
return cb(i, 33.3, Struct());
}
}


struct Issue268 {
int i;
}
8 changes: 8 additions & 0 deletions examples/phobos/std_datetime/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ enum str = wrapDlang!(

// pragma(msg, str);
mixin(str);


pragma(mangle, "_D6python4type__T13PythonCompareTS3std8typecons__T10RebindableTyCQBf8datetime8timezone8TimeZoneZQBuZ7_py_cmpUNbPSQEh3raw7_objectQriZQv")
private void hack0() { assert(0); }


pragma(mangle, "_D6python4type__T13PythonCompareTS3std8datetime8timezone13PosixTimeZone6TTInfoZ7_py_cmpUNbPSQDm3raw7_objectQriZQv")
private void hack1() { assert(0); }
4 changes: 4 additions & 0 deletions examples/phobos/std_experimental/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ pragma(mangle, "_D6python4conv11python_to_d__T2toTxS3std12experimental9allocator
extern(C) void hack3() {

}


pragma(mangle, "_D6python4type__T13PythonCompareTS3std12experimental9allocator15building_blocks14allocator_list__T13AllocatorListTSQDdQDcQCr8showcase14mmapRegionListFmZ7FactoryTSQEyQExQEmQEf14null_allocator13NullAllocatorZQEe4NodeZ7_py_cmpUNbPSQIs3raw7_objectQriZQv")
private void hack4() { assert(0); }
4 changes: 4 additions & 0 deletions examples/phobos/std_file/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ void hack() {
import std.file: dirEntries, SpanMode;
auto id = typeid(dirEntries("path", "pattern", SpanMode.depth));
}


pragma(mangle, "_D6python4type__T13PythonCompareTS3std8typecons__T10RebindableTyCQBf8datetime8timezone8TimeZoneZQBuZ7_py_cmpUNbPSQEh3raw7_objectQriZQv")
private void hack1() { assert(0); }
4 changes: 4 additions & 0 deletions examples/phobos/std_net/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ enum str = wrapDlang!(

// pragma(msg, str);
mixin(str);


pragma(mangle, "_D6python4type__T13PythonCompareTS3std8typecons__T10RebindableTyCQBf8datetime8timezone8TimeZoneZQBuZ7_py_cmpUNbPSQEh3raw7_objectQriZQv")
private void hack0() { assert(0); }
4 changes: 4 additions & 0 deletions examples/phobos/std_zip/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ enum str = wrapDlang!(

// pragma(msg, str);
mixin(str);


pragma(mangle, "_D6python4type__T13PythonCompareTS3std8typecons__T10RebindableTyCQBf8datetime8timezone8TimeZoneZQBuZ7_py_cmpUNbPSQEh3raw7_objectQriZQv")
private void hack0() { assert(0); }
15 changes: 15 additions & 0 deletions pynih/source/python/conv/d_to_python.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ import std.datetime: Date, DateTime;
import core.time: Duration;


PyObject* toPython(in bool val) @trusted @nogc {
import python.raw: pyIncRef, _Py_TrueStruct, _Py_FalseStruct;

auto pyTrue = cast(PyObject*) &_Py_TrueStruct;
auto pyFalse = cast(PyObject*) &_Py_FalseStruct;

static PyObject* incAndRet(PyObject* obj) {
pyIncRef(obj);
return obj;
}

return val ? incAndRet(pyTrue) : incAndRet(pyFalse);
}


PyObject* toPython(T)(T value) @trusted if(isIntegral!T && !is(T == enum)) {
import python.raw: PyLong_FromLong;
return PyLong_FromLong(value);
Expand Down
53 changes: 51 additions & 2 deletions pynih/source/python/type.d
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ struct PythonType(T) {
)
{
_pyType.tp_richcompare = &PythonOpCmp!T._py_cmp;
}
}
} else
_pyType.tp_richcompare = &PythonCompare!T._py_cmp;
} else
_pyType.tp_richcompare = &PythonCompare!T._py_cmp;


static if(hasMember!(T, "opSlice")) {
static if(AlwaysTry || __traits(compiles, &PythonIterViaList!T._py_iter))
Expand Down Expand Up @@ -1462,6 +1465,52 @@ private template PythonIndexAssign(T) {
}
}


private template PythonCompare(T) {

static extern(C) PyObject* _py_cmp(PyObject* self, PyObject* other, int op) nothrow {

PyObject* impl() {
import python.conv.python_to_d: to;
import python.conv.d_to_python: toPython;
import python.raw: pyIncRef, _Py_NotImplementedStruct, Py_EQ, Py_LT, Py_LE, Py_NE, Py_GT, Py_GE;

auto pyNotImplemented = cast(PyObject*) &_Py_NotImplementedStruct;

static if(is(typeof(T.init < T.init) == bool))
if(op == Py_LT)
return (self.to!T < other.to!T).toPython;

static if(is(typeof(T.init <= T.init) == bool))
if(op == Py_LE)
return (self.to!T <= other.to!T).toPython;

static if(is(typeof(T.init == T.init) == bool))
if(op == Py_EQ)
return (self.to!T == other.to!T).toPython;

static if(is(typeof(T.init != T.init) == bool))
if(op == Py_NE)
return (self.to!T != other.to!T).toPython;

static if(is(typeof(T.init > T.init) == bool))
if(op == Py_GT)
return (self.to!T > other.to!T).toPython;

static if(is(typeof(T.init >= T.init) == bool))
if(op == Py_GE)
return (self.to!T >= other.to!T).toPython;


pyIncRef(pyNotImplemented);
return pyNotImplemented;
}

return noThrowable!impl;
}
}


private bool isInstanceOf(T)(PyObject* obj) {
import python.raw: PyObject_IsInstance;
return cast(bool) PyObject_IsInstance(obj, cast(PyObject*) PythonType!T.pyType);
Expand Down
11 changes: 11 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,14 @@ def test_method_delegate_safe_scope():
from issues import MethodWithScopeSafeDelegate
m = MethodWithScopeSafeDelegate()
assert m.fun(3, lambda i, d, s: i * 2) == 6


def test_issue_268():
import pytest
from issues import Issue268
assert Issue268(1) != Issue268(2)
if is_pynih:
assert Issue268(42) == Issue268(42)
else:
with pytest.raises(AssertionError):
assert Issue268(42) == Issue268(42)

0 comments on commit 3e8bc99

Please sign in to comment.