파이썬에서 구현 된 언어에 추적 백 / 디버깅 기능을 추가하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com//questions/9633363

문제

Python을 사용하여 'foo'라는 다른 프로그래밍 언어를 구현합니다. 모든 Foo의 코드는 파이썬으로 변환되며 동일한 Python 인터프리터에서 실행되므로 Python으로 변환됩니다.

여기에 작은 foo의 코드가 있습니다.

function bar(arg1, arg2) {
    while (arg1 > arg2) {
        arg2 += 5;
    }
    return arg2 - arg1;
}
.

번역 할 것 :

def _bar(arg1, arg2):
    while arg1 > arg2:
        arg2 += 5
        watchdog.switch()
    watchdog.switch()
    return arg2 - arg1
.

'Watchdog'는 언어가 신뢰할 수없는 코드를 실행할 것이기 때문에 리소스 사용을 모니터 / 제한하는 녹색의 컨텍스트에서 그리스턴스 컨텍스트에서도 실행중인 녹색입니다.

Python 코드가 생성되기 전에 워치 독 스위치를 추가하고 기능 식별자를 작게 변경하려면 파이썬 코드가 생성되기 위해서는 소규모 변경 사항이 소규모 변경됩니다.

모든 기간을 충족시키기 위해 언어에 대한 추적 백 / 디버깅 기능을 추가해야하므로 Python 런타임이 예외를 던 졌을 때 사용자가 볼 수있는 방법은 Foo의 코드 추적이 생성됩니다 (생성 된 Python 코드 추적 ).

사용자가 다음 내용을 사용하여 'program.foo'라는 파일을 만듭니다.

1  function bar() {
2      throw Exception('Some exception message');
3  }
4
5  function foo() {
6      output('invoking function bar');
7      bar();
8  }
9
10 foo();
.

번역 할 것 :

def _bar():
    watchdog.switch()
    raise Exception('Some exception message')

def _foo():
    print 'invoking function bar'
    watchdog.switch()
    _bar()

watchdog.switch()
_foo()
.

그런 다음 'program.foo'의 출력은 다음과 같아야합니다.

invoking function bar
Traceback (most recent call last):
  File "program.foo", line 10
    foo();
  File "program.foo", line 7, inside function 'foo'
    bar();
  File "program.foo", line 2, inside function 'bar'
    throw Exception('Some exception message');
Exception: Some exception message
.

쉽게 할 수있는 방법이 있습니까? 나는 인터프리터 구현의 내부에 있기 때문에 파이썬 바이트 코드를 계측하는 솔루션을 선호하지만 다른 것이 없으면 계측 바이트 코드도 할 수 있습니다.

도움이 되었습니까?

해결책

You could decorate each generated Python function with a decorator which record the context (filename, function, line number, etc.) to a global stack. Then you could derive your own Exception class and catch it at the top level of the interpreter. Finally, you print out what you like, using information from the global debug stack.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top