Urwid: how to see errors?

I am creating an application with an interactive console interface (htop line, atop utilities) using the library urwid, so my problem is: since the interface takes up all the space in the console window - I did not see errors in python, I tried to do that:

import sys
f = open("test_err", "w")
original_stderr = sys.stderr
sys.stderr = f

print a #a is undefined

sys.stderr = original_stderr
f.close() 

This works when I do not use urwid, but not when I use it ...

+1
source share
2 answers

you can try redirecting errors to a file. after each launch of the program you need to update the file. most editors let you do this easily by pressing f5

def main():
    #your code here
    print someError #raises an error

try: #run main function
    main()
except BaseException as err: #catch all errors
    with open('errors.txt','a') as errors: #open a file to write the errors to
        errors.write(err.message+'\n')#write the error

'a' 'w' , ( , ).

, , .

def main():
    #your code here
    print someErr

try: #run main function
   main()
except BaseException as err: #catch all errors
    import Tkinter as tk #imports the ui module

    root = tk.Tk() #creates the root of the window

    #creates the text and attaches it to the root
    window = tk.Label(root, text=err.message)
    window.pack()

    #runs the window
    root.mainloop()

, Tkinter . ( python, )

0

. unicode-rxvt (urxvt), . , , X, .

from __future__ import print_function

import os
from datetime import datetime

_debugfile = None

def _close_debug(fo):
    fo.close()

def DEBUG(*obj):
    """Open a terminal emulator and write messages to it for debugging."""
    global _debugfile
    if _debugfile is None:
        import atexit
        masterfd, slavefd = os.openpty()
        pid = os.fork()
        if pid: # parent
            os.close(masterfd)
            _debugfile = os.fdopen(slavefd, "w+", 0)
            atexit.register(_close_debug, _debugfile)
        else: # child
            os.close(slavefd)
            os.execlp("urxvt", "urxvt", "-pty-fd", str(masterfd))
    print(datetime.now(), ":", ", ".join(map(repr, obj)), file=_debugfile)

, DEBUG . . " ". , , , .

0

Source: https://habr.com/ru/post/1674128/


All Articles