Add a way to detect whether the basic or the new REPL (PyREPL) is in use?

Would it make sense to add an indicator of/way to detect which REPL, basic or PyREPL, is in use?

I think it would need to detect which REPL was activated during initialization, track calls to sys._baserepl and run_multiline_interactive_console, and probably read CAN_USE_PYREPL at a minimum. Does it sound feasible? Do you have a better idea of how to accomplish this?

My concrete use case is just unittesting that the PYTHON_BASIC_REPL environment variable is working.

It seems it could also be useful for to reliably detect PyREPL. I imagine it could also be used to help further customizing the REPL, specially when usage of the basic REPL is detected. And also to allow programmatically checking that the user (e.g., a student) is getting the expected user experience (e.g., to make sure everyone following a course has the same features enabled).

Do you see any further use cases? Does it sound interesting enough for me to propose a PR?

The premise I’m working from is that it’s currently not possible to detect that, forgive me if I missed a way to do it.

Posting to Python Help as it’s a pretty half-baked idea.

@ambv @pablogsal @lys.nikolaou @trey

Sorry if pinging you about PyREPL isn’t appropriate, I thought you might be interested in this idea.

Why do you need to detect this programmatically? During training, all you need to be able to see is that your prompt is now colored.

Fair enough. Thank you for your time in considering this.

For context, I imagined a scenario where the instructor would ask students to run a script to make sure their environment is properly configured (e.g. in a venv with the right name, with the right Python version, with the right directories to put code on etc.) and whether PyREPL is enabled could be one of those checks, but I agree it’d be easier to just print “is your prompt (‘>>>’) colored?”.

Do you consider that detecting it programmatically could improve the detection of dumb terminals in Or that whether setting PYTHON_BASIC_REPL works or not is valuable? If not, then I’m really out of examples of advantages of adding a way to detect which REPL is in use.

It could be easy to implement, I have a PoC that just adds checks to pymain_run_stdin and pymain_repl, and sets sys.repl="basic" in sys__baserepl_impl, and it results in:

> git diff main --stat
 Lib/_pyrepl/        |  1 +
 Lib/_pyrepl/ |  1 +
 Modules/main.c                 | 32 ++++++++++++++++++++++++++++++++
 Python/sysmodule.c             |  6 ++++++
 4 files changed, 40 insertions(+)

But I can totally understand if YAGNI applies here.

To be clear, I’m not against a way to discover this, but before we jump to implementing, I need to understand your use case. Plus, in all likelihood, this would have to wait until 3.14 as a feature that wasn’t initially implemented before the beta freeze.