1

When python is used in interactive mode, cppyy provides excellent information via python's help() function. I would like to access these answers in a non-interactive python script. Is this possible?

jkr
  • 17,119
  • 2
  • 42
  • 68

1 Answers1

0

I agree with @jakub that your question is unclear as long as you don't explain what you want to do with the result. Point being that, yes, it's easy to capture help() output and that has nothing to do with cppyy proper, it's a pure python thing. See e.g. the example code below.

But help (and pydoc) is just using the inspect module for queries, among others, for types and docs/comments (for cppyy, those are __doc__ strings primarily). Iow., using that module may give you more manageable results than the pure string information.

If OTOH the question is really about getting C++ reflection information as displayed by help(), then I refer to this answer: Can I get the AST from cppyy .

import cppyy
import io
import pydoc

class StringPager:
    def __init__(self, sio):
        self._sio = sio

    def __call__(self, text):
        self._sio.write(text)

    def __enter__(self):
        self._orgpager = pydoc.pager
        pydoc.pager = self

    def __exit__(self, tp, val, trace):
        pydoc.pager = self._orgpager


helptxt = io.StringIO()

with StringPager(helptxt):
    pydoc.doc(cppyy.gbl.gInterpreter)

print(helptxt.getvalue())
Wim Lavrijsen
  • 3,453
  • 1
  • 9
  • 21
  • The problem was that the error messages I was getting from c++ through cppyy were unreadable. I wrote a crude pretty print program. Here is the output for one line of the error message: – Edward C. Jones Dec 08 '21 at 14:21
  • I was not able to read the complex error messages passed through cppyy. I wrote a crude pretty print program. Here is the output for – Edward C. Jones Dec 08 '21 at 14:24