jeudi 22 juin 2023

Trying to run current script again causes stack overflow or parserror?

I'm writing a script that will modify the code in the script and rerun it automatically but whichever way I do it, it always goes into repeated output until the kernel restarts and if I keyboard interrupt sometimes I get: ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'. And usually this is a Pandas error, but there's no Pandas. Ideally, I would like to accomplish this without using shell and also be able to set a breakpoint so instead of accessing the current file I can just access everything written up until that point. This is every way I've tried it. I think maybe if there was a way to do introspection from a context manager (like if there is a way to access an object from yield) that would be great to know.


    current_file = clear_me()
    code_to_be = "".join(rf"{e}" for e in current_file)
    #str(clear_me()).strip('[]')
    import parser
    xyyx = parser.suite(code_to_be)
    exec(xyyx.compile())
    
    import os
    
    # path_to_script = os.path.dirname(os.path.abspath(__file__))
    # my_filename = os.path.join(path_to_script, "my_file.py")
    
    # with open(my_filename, "w") as handle:
    #     print(code_to_be, file=handle)
    
    #exec(compile(code_to_be, "<string>", "exec"))
    
    import subprocess
    #subprocess.run(["python", f"{__file__}"])
    #exec(open("my_file.py").read())

def clear_me():
    f = open(rf"{__file__}", 'r')
    takethel = f.readlines()
    return takethel




Aucun commentaire:

Enregistrer un commentaire