Skip to content

Some suggestions for the GAP interface #35361

@fingolfin

Description

@fingolfin

Some remarks on the GAP integration:

We (= GAP) have a new helper function CALL_WITH_STREAM which you could use to rewrite your capture_stdout with pure GAP. As an example, in GAP.jl (Julia interface to GAP), I use these two helpers; adapting that code to match capture_stdout is straight-forward. Note that I use SetPrintFormattingStatus there to avoid diffs caused by the width of the user's terminal; but if you want to 100% faithfully reproduce what GAP does, then leave that out.

BindGlobal( "StringDisplayObj",
function(obj)
  local str, out;
  str := "";
  out := OutputTextString(str, false);
  SetPrintFormattingStatus(out, false);
  CALL_WITH_STREAM(out, Display, [obj]);
  CloseStream(out);
  return str;
end );

BindGlobal( "StringViewObj",
function(obj)
  local str, out;
  str := "";
  out := OutputTextString(str, false);
  SetPrintFormattingStatus(out, false);
  CALL_WITH_STREAM(out, ViewObj, [obj]);
  CloseStream(out);
  return str;
end );

This is of course in the end equivalent to what capture_stdout does, but while CALL_WITH_STREAM is also undocumented, it'll be more stable than calling internal kernel functions like OpenOutputStream. We could also eventually offer a documented, high-level version of it as CallWithStream; the main reason that's not yet there is that I first wanted to validate the design. If Sage also finds it useful, that would be a strong motivation to provide a fully documented CallWithStream in GAP 4.13. In addition or alternatively, functions like StringViewObj or StringPrint could be provided, but there it is already less clear that we can agree on a common version (see SetPrintFormattingStatus ...) ?

  • Another point: you could replace your uses of GVarName and AssGVar by GAP_AssignGlobalVariable (the performance overhead of re-calling GVarName should be miniscule; if hold_reference ever is called in a tight loop, that code likely has more serious problems than that...

  • alternatively just remove hold_reference (nothing uses it?)

  • remove NewBag and CallbackForAllBags from gap_includes.pxd ?

  • there are a lot of other things that could be replaced by "official" libgap API usage, e.g. IS_INT -> GAP_IsInt, IS_INTOBJ -> GAP_IsSmallInt, SUM -> GAP_SUM, VAL_MACFLOAT -> GAP_ValueMacFloat, etc. -- most of these were added specifically for use by Sage years ago but apparently are not actually used by Sage? Overall I would expect that a majority of the imports from outside libgap-api.h can be replaced. If someone wants to work on that and needs help to figure out what replaces what, I'll be happy to assist

Originally posted by @e4745025-e0be-4c7e-a474-0267c8aa7406 in #34391 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions