Skip to content

Commit

Permalink
Simplify GapFunction.__call__ using the public API GAP_CallFuncList
Browse files Browse the repository at this point in the history
Refs #2.
  • Loading branch information
embray committed Jan 12, 2021
1 parent 20edea8 commit 35c04c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 53 deletions.
54 changes: 9 additions & 45 deletions gappy/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ cdef char *capture_stdout(Obj func, Obj obj):
"""
cdef Obj s, stream, output_text_string
cdef UInt res
cdef Obj args[2]
# The only way to get a string representation of an object that is truly
# consistent with how it would be represented at the GAP REPL is to call
# ViewObj on it. Unfortunately, ViewObj *prints* to the output stream,
Expand All @@ -172,13 +173,16 @@ cdef char *capture_stdout(Obj func, Obj obj):
GAP_Enter()
s = NEW_STRING(0)
output_text_string = GAP_ValueGlobalVariable("OutputTextString")
stream = CALL_2ARGS(output_text_string, s, GAP_True)
args[0] = s
args[1] = GAP_True
stream = GAP_CallFuncArray(output_text_string, 2, args)

if not OpenOutputStream(stream):
raise GAPError("failed to open output capture stream for "
"representing GAP object")

CALL_1ARGS(func, obj)
args[0] = obj
GAP_CallFuncArray(func, 1, args)
CloseOutput()
return CSTR_STRING(s)
finally:
Expand Down Expand Up @@ -2182,55 +2186,15 @@ cdef class GapFunction(GapObj):
b'hello from the shell\n'
"""
cdef Obj result = NULL
cdef Obj arg_list
cdef int i, n = len(args)
cdef Obj arglist

libgap = self.parent()

if n > 0:
a = [x if isinstance(x, GapObj) else libgap(x) for x in args]

try:
sig_GAP_Enter()
sig_on()
if n == 0:
result = CALL_0ARGS(self.value)
elif n == 1:
result = CALL_1ARGS(self.value,
(<GapObj>a[0]).value)
elif n == 2:
result = CALL_2ARGS(self.value,
(<GapObj>a[0]).value,
(<GapObj>a[1]).value)
elif n == 3:
result = CALL_3ARGS(self.value,
(<GapObj>a[0]).value,
(<GapObj>a[1]).value,
(<GapObj>a[2]).value)
elif n == 4:
result = CALL_4ARGS(self.value,
(<GapObj>a[0]).value,
(<GapObj>a[1]).value,
(<GapObj>a[2]).value,
(<GapObj>a[3]).value)
elif n == 5:
result = CALL_5ARGS(self.value,
(<GapObj>a[0]).value,
(<GapObj>a[1]).value,
(<GapObj>a[2]).value,
(<GapObj>a[3]).value,
(<GapObj>a[4]).value)
elif n == 6:
result = CALL_6ARGS(self.value,
(<GapObj>a[0]).value,
(<GapObj>a[1]).value,
(<GapObj>a[2]).value,
(<GapObj>a[3]).value,
(<GapObj>a[4]).value,
(<GapObj>a[5]).value)
elif n >= 7:
arg_list = make_gap_list(libgap, args)
result = CALL_XARGS(self.value, arg_list)
arglist = make_gap_list(libgap, args)
result = GAP_CallFuncList(self.value, arglist)
sig_off()
finally:
GAP_Leave()
Expand Down
12 changes: 4 additions & 8 deletions gappy/gap_includes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ cdef extern from "gap/bool.h" nogil:

cdef extern from "gap/calls.h" nogil:
bint IS_FUNC(Obj)
Obj CALL_0ARGS(Obj f) # 0 arguments
Obj CALL_1ARGS(Obj f, Obj a1) # 1 argument
Obj CALL_2ARGS(Obj f, Obj a1, Obj a2)
Obj CALL_3ARGS(Obj f, Obj a1, Obj a2, Obj a3)
Obj CALL_4ARGS(Obj f, Obj a1, Obj a2, Obj a3, Obj a4)
Obj CALL_5ARGS(Obj f, Obj a1, Obj a2, Obj a3, Obj a4, Obj a5)
Obj CALL_6ARGS(Obj f, Obj a1, Obj a2, Obj a3, Obj a4, Obj a5, Obj a6)
Obj CALL_XARGS(Obj f, Obj args) # more than 6 arguments


cdef extern from "gap/gasman.h" nogil:
Expand Down Expand Up @@ -89,6 +81,10 @@ cdef extern from "gap/libgap-api.h" nogil:
cdef void GAP_Leave()
cdef int GAP_Error_Setjmp() except 0

# Calls
Obj GAP_CallFuncArray(Obj, UInt, Obj *)
Obj GAP_CallFuncList(Obj, Obj)

# Ints
cdef int GAP_IsInt(Obj)
cdef int GAP_IsSmallInt(Obj)
Expand Down

0 comments on commit 35c04c0

Please sign in to comment.