@@ -21,11 +21,13 @@ export pyobjectBase
2121template getMagic * (obj: PyObject , methodName): untyped =
2222 obj.pyType.magicMethods.methodName
2323
24- template getFun * (obj: PyObject , methodName: untyped , handleExcp= false ): untyped =
25- if obj.pyType.isNil:
26- unreachable (" Py type not set" )
27- let fun = getMagic (obj, methodName)
28- if fun.isNil:
24+
25+ template checkTypeNotNil (obj) =
26+ when not defined (release):
27+ if obj.pyType.isNil:
28+ unreachable (" Py type not set" )
29+
30+ template handleNilFunOfGetFun (obj, methodName, handleExcp) =
2931 let objTypeStr = $ obj.pyType.name
3032 let methodStr = astToStr (methodName)
3133 let msg = " No " & methodStr & " method for " & objTypeStr & " defined"
@@ -34,33 +36,56 @@ template getFun*(obj: PyObject, methodName: untyped, handleExcp=false): untyped
3436 handleException (excp)
3537 else :
3638 return excp
39+
40+ template getFun * (obj: PyObject , methodName: untyped , handleExcp= false ): untyped =
41+ bind checkTypeNotNil, handleNilFunOfGetFun
42+ obj.checkTypeNotNil
43+ let fun = getMagic (obj, methodName)
44+ if fun.isNil:
45+ handleNilFunOfGetFun (obj, methodName, handleExcp)
3746 fun
3847
3948
4049# XXX: `obj` is used twice so it better be a simple identity
4150# if it's a function then the function is called twice!
4251
43- # is there any ways to reduce the repetition? simple template won't work
44- template callMagic * (obj: PyObject , methodName: untyped , handleExcp= false ): PyObject =
45- let fun = obj.getFun (methodName, handleExcp)
46- let res = fun (obj)
52+ template checkExcAndRet [T](res: T, handleExcp): T =
4753 when handleExcp:
4854 if res.isThrownException:
4955 handleException (res)
5056 res
5157 else :
5258 res
5359
60+ # is there any ways to reduce the repetition? simple template won't work
61+ template callMagic * (obj: PyObject , methodName: untyped , handleExcp= false ): PyObject =
62+ let fun = obj.getFun (methodName, handleExcp)
63+ let res = fun (obj)
64+ bind checkExcAndRet
65+ res.checkExcAndRet handleExcp
66+
5467
5568template callMagic * (obj: PyObject , methodName: untyped , arg1: PyObject , handleExcp= false ): PyObject =
5669 let fun = obj.getFun (methodName, handleExcp)
5770 let res = fun (obj, arg1)
58- when handleExcp:
59- if res.isThrownException:
60- handleException (res)
61- res
71+ bind checkExcAndRet
72+ res.checkExcAndRet handleExcp
73+
74+
75+ template callInplaceMagic * (obj: PyObject , methodName1: untyped ,
76+ arg1: PyObject , handleExcp= false ): PyObject =
77+ bind checkTypeNotNil, handleNilFunOfGetFun
78+ bind checkExcAndRet
79+ obj.checkTypeNotNil
80+ var fun = obj.getMagic (methodName1)
81+ if fun.isNil:
82+ pyNotImplemented
6283 else :
63- res
84+ if fun.isNil:
85+ handleNilFunOfGetFun (obj, methodName1, handleExcp)
86+ else :
87+ let res = fun (obj, arg1)
88+ res.checkExcAndRet handleExcp
6489
6590template callMagic * (obj: PyObject , methodName: untyped ,
6691 arg1, arg2: PyObject , handleExcp= false ): PyObject =
0 commit comments