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

Commit

Permalink
use ViewObj instead of ViewString
Browse files Browse the repository at this point in the history
while this has more consistently with what is displayed at the GAP command
line, and with how GAP objects were represented in earlier versions of Sage,
it requires an unfortunate amount of overhead

there has been some acknowledgement from the GAP developers that there is a
need to more easily obtain the print representation of objects as a raw string
without actually printing it to an I/O stream
  • Loading branch information
embray committed Dec 7, 2018
1 parent 94f09a6 commit f571399
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,33 @@ cdef Obj make_gap_list(sage_list) except NULL:


cdef char *crepr(Obj obj):
cdef Obj s
s = CALL_1ARGS(GAP_ValueGlobalVariable("ViewString"), obj)
return CSTR_STRING(s)
cdef Obj s, stream, output_text_string, view_obj
cdef UInt res
# 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,
# and there is no equivalent that simply returns the string that would be
# printed. The closest approximation would be DisplayString, but this
# bypasses any type-specific overrides for ViewObj so for many objects
# that does not give consistent results.
# TODO: This is probably needlessly slow, but we might need better
# support from GAP to improve this...
try:
GAP_Enter()
s = NEW_STRING(0)
output_text_string = GAP_ValueGlobalVariable("OutputTextString")
stream = CALL_2ARGS(output_text_string, s, GAP_True)

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

viewobj = GAP_ValueGlobalVariable("ViewObj")
CALL_1ARGS(viewobj, obj)
CloseOutput()
return CSTR_STRING(s)
finally:
GAP_Leave()


cdef Obj make_gap_record(sage_dict) except NULL:
Expand Down

0 comments on commit f571399

Please sign in to comment.