Skip to content

Commit

Permalink
gh-35761: gap: switch more code to offical libgap APIs
Browse files Browse the repository at this point in the history
    

    
URL: #35761
Reported by: Max Horn
Reviewer(s): David Coudert, Frédéric Chapoton, Max Horn
  • Loading branch information
Release Manager committed Jun 29, 2023
2 parents 630d1ff + fb0908e commit 615fbb8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
30 changes: 15 additions & 15 deletions src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ cdef char *capture_stdout(Obj func, Obj obj):

CALL_WITH_STREAM = GAP_ValueGlobalVariable("CALL_WITH_STREAM")
CALL_3ARGS(CALL_WITH_STREAM, stream, func, l)
return CSTR_STRING(s)
return GAP_CSTR_STRING(s)
finally:
GAP_Leave()

Expand Down Expand Up @@ -247,7 +247,7 @@ cdef Obj make_gap_string(sage_string) except NULL:
try:
GAP_Enter()
b = str_to_bytes(sage_string)
result = MakeStringWithLen(b, len(b))
result = GAP_MakeStringWithLen(b, len(b))
return result
finally:
GAP_Leave()
Expand Down Expand Up @@ -937,7 +937,7 @@ cdef class GapElement(RingElement):
sig_on()
try:
GAP_Enter()
return EQ(self.value, c_other.value)
return GAP_EQ(self.value, c_other.value)
finally:
GAP_Leave()
sig_off()
Expand All @@ -959,7 +959,7 @@ cdef class GapElement(RingElement):
sig_on()
try:
GAP_Enter()
return LT(self.value, c_other.value)
return GAP_LT(self.value, c_other.value)
finally:
GAP_Leave()
sig_off()
Expand Down Expand Up @@ -987,7 +987,7 @@ cdef class GapElement(RingElement):
try:
sig_GAP_Enter()
sig_on()
result = SUM(self.value, (<GapElement>right).value)
result = GAP_SUM(self.value, (<GapElement>right).value)
sig_off()
finally:
GAP_Leave()
Expand Down Expand Up @@ -1015,7 +1015,7 @@ cdef class GapElement(RingElement):
try:
sig_GAP_Enter()
sig_on()
result = DIFF(self.value, (<GapElement>right).value)
result = GAP_DIFF(self.value, (<GapElement>right).value)
sig_off()
finally:
GAP_Leave()
Expand Down Expand Up @@ -1045,7 +1045,7 @@ cdef class GapElement(RingElement):
try:
sig_GAP_Enter()
sig_on()
result = PROD(self.value, (<GapElement>right).value)
result = GAP_PROD(self.value, (<GapElement>right).value)
sig_off()
finally:
GAP_Leave()
Expand Down Expand Up @@ -1079,7 +1079,7 @@ cdef class GapElement(RingElement):
try:
sig_GAP_Enter()
sig_on()
result = QUO(self.value, (<GapElement>right).value)
result = GAP_QUO(self.value, (<GapElement>right).value)
sig_off()
finally:
GAP_Leave()
Expand All @@ -1106,7 +1106,7 @@ cdef class GapElement(RingElement):
try:
sig_GAP_Enter()
sig_on()
result = MOD(self.value, (<GapElement>right).value)
result = GAP_MOD(self.value, (<GapElement>right).value)
sig_off()
finally:
GAP_Leave()
Expand Down Expand Up @@ -1155,7 +1155,7 @@ cdef class GapElement(RingElement):
try:
sig_GAP_Enter()
sig_on()
result = POW(self.value, (<GapElement>other).value)
result = GAP_POW(self.value, (<GapElement>other).value)
sig_off()
finally:
GAP_Leave()
Expand Down Expand Up @@ -2334,7 +2334,7 @@ cdef class GapElement_String(GapElement):
sage: type(_)
<class 'str'>
"""
s = char_to_str(CSTR_STRING(self.value))
s = char_to_str(GAP_CSTR_STRING(self.value))
return s

sage = __str__
Expand Down Expand Up @@ -2477,8 +2477,8 @@ cdef class GapElement_Function(GapElement):
GAPError: Error, no method found!
Error, no 1st choice method found for `SumOp' on 2 arguments
sage: for i in range(0,100):
....: rnd = [ randint(-10,10) for i in range(0,randint(0,7)) ]
sage: for i in range(100):
....: rnd = [ randint(-10,10) for i in range(randint(0,7)) ]
....: # compute the sum in GAP
....: _ = libgap.Sum(rnd)
....: try:
Expand Down Expand Up @@ -2884,7 +2884,7 @@ cdef class GapElement_List(GapElement):
else:
celt= self.parent()(elt)

ASS_LIST(obj, j+1, celt.value)
GAP_AssList(obj, j+1, celt.value)

def sage(self, **kwds):
r"""
Expand Down Expand Up @@ -3288,7 +3288,7 @@ cdef class GapElement_RecordIterator():
raise StopIteration
# note the abs: negative values mean the rec keys are not sorted
key_index = abs(GET_RNAM_PREC(self.rec.value, i))
key = char_to_str(CSTR_STRING(NAME_RNAM(key_index)))
key = char_to_str(GAP_CSTR_STRING(NAME_RNAM(key_index)))
cdef Obj result = GET_ELM_PREC(self.rec.value,i)
val = make_any_gap_element(self.rec.parent(), result)
self.i += 1
Expand Down
33 changes: 17 additions & 16 deletions src/sage/libs/gap/gap_includes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ cdef extern from "gap/system.h" nogil:
ctypedef void* Obj


cdef extern from "gap/ariths.h" nogil:
Obj SUM(Obj, Obj)
Obj DIFF(Obj, Obj)
Obj PROD(Obj, Obj)
Obj QUO(Obj, Obj)
Obj POW(Obj, Obj)
Obj MOD(Obj, Obj)
bint EQ(Obj opL, Obj opR)
bint LT(Obj opL, Obj opR)


cdef extern from "gap/calls.h" nogil:
bint IS_FUNC(Obj)
Obj CALL_0ARGS(Obj f) # 0 arguments
Expand Down Expand Up @@ -71,6 +60,16 @@ cdef extern from "gap/libgap-api.h" nogil:
void GAP_MarkBag(Obj bag)
void GAP_CollectBags(UInt full)

Obj GAP_SUM(Obj, Obj)
Obj GAP_DIFF(Obj, Obj)
Obj GAP_PROD(Obj, Obj)
Obj GAP_QUO(Obj, Obj)
Obj GAP_POW(Obj, Obj)
Obj GAP_MOD(Obj, Obj)
bint GAP_EQ(Obj opL, Obj opR)
bint GAP_LT(Obj opL, Obj opR)
bint GAP_IN(Obj opL, Obj opR)

cdef Obj GAP_True
cdef Obj GAP_False

Expand All @@ -89,11 +88,16 @@ cdef extern from "gap/libgap-api.h" nogil:
bint GAP_IsRecord(Obj obj)
Obj GAP_NewPrecord(Int capacity)

bint GAP_IsString(Obj obj)
UInt GAP_LenString(Obj string)
char* GAP_CSTR_STRING(Obj list)
Obj GAP_MakeStringWithLen(const char* buf, UInt len)

Int GAP_ValueOfChar(Obj obj)


cdef extern from "gap/lists.h" nogil:
Obj ELM_LIST(Obj lst, int pos)
Obj ELM0_LIST(Obj lst, int pos)
void ASS_LIST(Obj lst, int pos, Obj elt)


cdef extern from "gap/listfunc.h" nogil:
Expand All @@ -117,7 +121,6 @@ cdef extern from "gap/objects.h" nogil:
T_BOOL
T_CHAR
T_FUNCTION
T_PLIST
T_COMOBJ
T_POSOBJ

Expand Down Expand Up @@ -147,8 +150,6 @@ cdef extern from "gap/records.h" nogil:


cdef extern from "gap/stringobj.h" nogil:
char* CSTR_STRING(Obj list)
bint IS_STRING(Obj obj)
bint IsStringConv(Obj obj)
Obj NEW_STRING(Int)
Obj MakeStringWithLen(const char* buf, size_t len)
8 changes: 4 additions & 4 deletions src/sage/libs/gap/util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ cdef Obj gap_eval(str gap_string) except? NULL:
raise GAPError("can only evaluate a single statement")

# Get the result of the first statement
result = ELM0_LIST(result, 1) # 1-indexed!
result = GAP_ElmList(result, 1) # 1-indexed!

if ELM0_LIST(result, 1) != GAP_True:
if GAP_ElmList(result, 1) != GAP_True:
# An otherwise unhandled error occurred in GAP (such as a
# syntax error). Try running the error handler manually
# to capture the error output, if any.
Expand All @@ -388,7 +388,7 @@ cdef Obj gap_eval(str gap_string) except? NULL:
# 0 is returned without setting a Python exception, so we should treat
# this like returning None)

return ELM0_LIST(result, 2)
return GAP_ElmList(result, 2)
finally:
GAP_Leave()
sig_off()
Expand Down Expand Up @@ -416,7 +416,7 @@ cdef str extract_libgap_errout():

# Grab a pointer to the C string underlying the GAP string libgap_errout
# then copy it to a Python str (char_to_str contains an implicit strcpy)
msg = CSTR_STRING(r)
msg = GAP_CSTR_STRING(r)
if msg != NULL:
msg_py = char_to_str(msg)
msg_py = msg_py.replace('For debugging hints type ?Recovery from '
Expand Down

0 comments on commit 615fbb8

Please sign in to comment.