From 49be111bed6e71ac8250d653cf1207626ced98e0 Mon Sep 17 00:00:00 2001 From: Mauricio Collares Date: Fri, 23 Sep 2022 16:02:39 -0300 Subject: [PATCH] Protect GAP_ValueGlobalVariable calls by GAP_Enter/GAP_Leave and use char_to_str earlier --- src/sage/libs/gap/element.pxd | 6 +++--- src/sage/libs/gap/element.pyx | 31 ++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/sage/libs/gap/element.pxd b/src/sage/libs/gap/element.pxd index a1bf8118d4f..9a28de87ca8 100644 --- a/src/sage/libs/gap/element.pxd +++ b/src/sage/libs/gap/element.pxd @@ -29,9 +29,9 @@ cdef GapElement_Boolean make_GapElement_Boolean(parent, Obj obj) cdef GapElement_Function make_GapElement_Function(parent, Obj obj) cdef GapElement_Permutation make_GapElement_Permutation(parent, Obj obj) -cdef char *capture_stdout(Obj, Obj) -cdef char *gap_element_str(Obj) -cdef char *gap_element_repr(Obj) +cdef str capture_stdout(Obj, Obj) +cdef str gap_element_str(Obj) +cdef str gap_element_repr(Obj) cdef class GapElement(RingElement): diff --git a/src/sage/libs/gap/element.pyx b/src/sage/libs/gap/element.pyx index e2681165a2d..fa2b0d287f4 100644 --- a/src/sage/libs/gap/element.pyx +++ b/src/sage/libs/gap/element.pyx @@ -120,7 +120,7 @@ cdef Obj make_gap_matrix(sage_list, gap_ring) except NULL: return l.value -cdef char *capture_stdout(Obj func, Obj obj): +cdef str capture_stdout(Obj func, Obj obj): """ Call a single-argument GAP function ``func`` with the argument ``obj`` and return the stdout from that function call. @@ -152,24 +152,28 @@ cdef char *capture_stdout(Obj func, Obj obj): CALL_1ARGS(func, obj) CloseOutput(&output) - return CSTR_STRING(s) + return char_to_str(CSTR_STRING(s)) finally: GAP_Leave() -cdef char *gap_element_repr(Obj obj): +cdef str gap_element_repr(Obj obj): """ Implement ``repr()`` of ``GapElement``s using the ``ViewObj()`` function, which is by default closest to what you get when displaying an object in GAP on the command-line (i.e. when evaluating an expression that returns that object. """ - - cdef Obj func = GAP_ValueGlobalVariable("ViewObj") - return capture_stdout(func, obj) + cdef Obj func + try: + GAP_Enter() + func = GAP_ValueGlobalVariable("ViewObj") + return capture_stdout(func, obj) + finally: + GAP_Leave() -cdef char *gap_element_str(Obj obj): +cdef str gap_element_str(Obj obj): """ Implement ``str()`` of ``GapElement``s using the ``Print()`` function. @@ -179,8 +183,13 @@ cdef char *gap_element_str(Obj obj): slightly different approach more closely mirroring Python's str/repr difference (though this does not map perfectly onto GAP). """ - cdef Obj func = GAP_ValueGlobalVariable("Print") - return capture_stdout(func, obj) + cdef Obj func + try: + GAP_Enter() + func = GAP_ValueGlobalVariable("Print") + return capture_stdout(func, obj) + finally: + GAP_Leave() cdef Obj make_gap_record(sage_dict) except NULL: @@ -761,7 +770,7 @@ cdef class GapElement(RingElement): if self.value == NULL: return 'NULL' - s = char_to_str(gap_element_str(self.value)) + s = gap_element_str(self.value) return s.strip() def _repr_(self): @@ -783,7 +792,7 @@ cdef class GapElement(RingElement): if self.value == NULL: return 'NULL' - s = char_to_str(gap_element_repr(self.value)) + s = gap_element_repr(self.value) return s.strip() cpdef _set_compare_by_id(self):