Skip to content

Commit

Permalink
Fixed #111
Browse files Browse the repository at this point in the history
  • Loading branch information
yglukhov committed Aug 7, 2019
1 parent f6b6654 commit 4a50b8d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions nimpy.nim
Expand Up @@ -105,6 +105,13 @@ const
Py_MLFLAGS_CLASS = (1 shl 4)
Py_MLFLAGS_STATIC = (1 shl 5)

# Rich comparison opcodes
Py_LT = 0
Py_LE = 1
Py_EQ = 2
Py_NE = 3
Py_GT = 4
Py_GE = 5

type
PyModuleDesc = object
Expand Down Expand Up @@ -1272,3 +1279,11 @@ proc dir*(v: PyObject): seq[string] =
proc pyBuiltinsModule*(): PyObject =
initPyLibIfNeeded()
pyImport(if pyLib.pythonVersion == 3: "builtins" else: "__builtin__")

proc `==`*(a, b: PyObject): bool =
if a.isNil and b.isNil:
true
elif (not a.isNil) and (not b.isNil):
pyLib.PyObject_RichCompareBool(a.rawPyObj, b.rawPyObj, Py_EQ) == 1
else:
false
3 changes: 3 additions & 0 deletions nimpy/py_lib.nim
Expand Up @@ -31,6 +31,7 @@ type
PyObject_GetIter*: proc(o: PPyObject): PPyObject {.cdecl.}
PyObject_GetItem*: proc(o, k: PPyObject): PPyObject {.cdecl.}
PyObject_SetItem*: proc(o, k, v: PPyObject): cint {.cdecl.}
PyObject_RichCompareBool*: proc(a, b: PPyObject, op: cint): cint {.cdecl.}
PyObject_GetBuffer*: proc(o: PPyObject, b: var RawPyBuffer, flags: cint): cint {.cdecl.}
PyBuffer_Release*: proc(b: var RawPyBuffer) {.cdecl.}

Expand Down Expand Up @@ -188,6 +189,8 @@ proc loadPyLibFromModule(m: LibHandle): PyLib =
load PyObject_GetIter
load PyObject_GetItem
load PyObject_SetItem
load PyObject_RichCompareBool

maybeLoad PyObject_GetBuffer
maybeLoad PyBuffer_Release

Expand Down
4 changes: 4 additions & 0 deletions tests/tpyfromnim.nim
Expand Up @@ -195,6 +195,10 @@ proc test*() =
doAssert(bb == 2)
doAssert(cc == "Hello")

block: # Comparison
let py = pyBuiltinsModule()
doAssert(py.None == py.None)

when isMainModule:
test()
echo "Test complete!"

0 comments on commit 4a50b8d

Please sign in to comment.