Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Protect GAP_ValueGlobalVariable calls by GAP_Enter/GAP_Leave and use …
Browse files Browse the repository at this point in the history
…char_to_str earlier
  • Loading branch information
collares committed Oct 28, 2022
1 parent eb8cd42 commit 49be111
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/sage/libs/gap/element.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
31 changes: 20 additions & 11 deletions src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 49be111

Please sign in to comment.