flet-dev / serious-python

Python runtime for Flutter apps
Apache License 2.0
212 stars 22 forks source link

Python REPL stacktrace #56

Open khaven opened 10 months ago

khaven commented 10 months ago

Hi, Is it possible to get full stacktrace of exception in python REPL flask example code. Right now the code is like this

`class PythonRunner: globals = {} locals = {}

def run(self, code):
    f = StringIO()
    with redirect_stdout(f):
        exec(code, self.__globals, self.__locals)
    return f.getvalue()` 

I've managed to get the full stacktrace but is it possible to exclude the info about the main.py file in the stacktrace? Capture I want the stacktrace to look like the code was run from a file on a command line.

khaven commented 10 months ago

This code works well on normal compiler but doesn't work on android.

`import subprocess import traceback import sys

code = "import numpy as np\nprint(1/0)" # Example code with a division by zero error

python_executable = sys.executable # Get the path to the current Python interpreter

try: result = subprocess.run( [python_executable, "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True, ) except subprocess.CalledProcessError as e: print(e.stderr)`

It shows "[Errno 13] Permission denied:"

FeodorFitsner commented 10 months ago

subprocess won't work on Android. Mobiles don't allow spawning new processes.

khaven commented 10 months ago

Is it possible to directly run a file and get it's stacktrace? I see a function "runProgram" is available but is it possible to get the stacktrace from it?

FeodorFitsner commented 10 months ago

Working on that: https://github.com/flet-dev/flet/issues/2352 It's #1 priority right now.

khaven commented 10 months ago

Thank you! :)

khaven commented 10 months ago

Hi, I'm testing the new ^0.7.0 version. I'm trying to get the stacktrace from a file. This file was created runtime and not exists in asset. But i'm getting a message instead of a stacktrace. Is it supposed to work like that?

Capture Capture1

and if i put print("Hello") in the file and no errors then it returning empty string. Am i doing something wrong here?