python4delphi icon indicating copy to clipboard operation
python4delphi copied to clipboard

help function in python opens a command windows

Open gsotiropoulos opened this issue 4 years ago • 4 comments

In the latest PythonEngine.pas the python help function opens a command window instead of redirecting to the open console.

I used Demo 1 and I tried to run the following: print(2+2) import os print("Hello") help(os)

See also image below

image

gsotiropoulos avatar Nov 12 '21 17:11 gsotiropoulos

It has always been like this.

If you run python in embedded mode, the help() function starts a new console and displays the help. The function does not return until the console is closed.

If you want to see help in the output window you can use the following.

def myhelp(ob):
   import sys
   import pydoc
   s = pydoc.getdoc(ob)
   print(s)

and even replace the builtin function with your own.

builtins.help = myhelp

pyscripter avatar Nov 12 '21 19:11 pyscripter

I found that this update of Dec 22, 2020 is responsible for this change in behavior: https://github.com/pyscripter/python4delphi/commit/708064f00993c6bf16294b69628403d217ac0824#diff-4e6e82ecc31f655224471f68dc6087a860c1260c3aea6f61288e66663122ec8c

My understanding is that having 'isatty()' returning True makes Python believe that a terminal is connected. Is it perhaps possible to make this configurable? I also found that e.g. 'help(os)' is more extensive than pydoc.getdoc(os), so this is not a full solution.

LennertP avatar Nov 15 '21 13:11 LennertP

@LennertP You are right. Removing the isatty stuff sends the output to stdout.

The Isatty was introduced by PR #261 because "pip module requires this method in stdout".

I am reopening the issue and I will think about it.

pyscripter avatar Nov 15 '21 22:11 pyscripter

Perhaps we can keep the isatty() function but make the return value (True/False) configurable?

LennertP avatar Nov 16 '21 08:11 LennertP

After some reflection, I concluded that isatty should stay (so that pip is happy), but it is more appropriate to return False.
This sorts out this issue, and prevent pip and other apps sending non printable control characters to the output.

pyscripter avatar Dec 23 '22 13:12 pyscripter

Indeed, this does the job! Thank you for resolving this issue.

LennertP avatar Dec 27 '22 10:12 LennertP