2016-06-09 3 views
4

나는 당연한 내가 구문 에러를 얻을, 파이썬에서 .ls를 실행하려고하면Python이 파이썬 인터프리터를 어떻게 변경합니까?

>>> .ls 
    File "<stdin>", line 1 
    .ls 
    ^
SyntaxError: invalid syntax 

그러나의 내용을 나열, 내가 PyROOT를 가져 오는 경우, 어떻게 든 법률이 구문을한다 (그리고 ROOT에서와 같이 동작 현재 파일은 여기에 예제에서 나는 어떤을 열지 않은)

>>> import ROOT 
>>> .ls 
>>> 

마찬가지로, .q는 일반 루트 인터프리터에서와 같이 내가 ROOT를 가져온 후 파이썬 인터프리터를 종료하기 위해 노력하고 있습니다..

어떻게 작동합니까?

답변

4
그것은 lib에 발견함으로써 수 있습니다

/ROOT.py

ipython없는 경우 sys.excepthook 재정의되는 조건이있다 :

### RINT command emulation  
------------------------------------------------------ 
def _excepthook(exctype, value, traceb): 
# catch syntax errors only (they contain the full line) 
    if isinstance(value, SyntaxError) and value.text: 
     cmd, arg = split(value.text[:-1]) 

    # mimic ROOT/CINT commands 
     if cmd == '.q': 
     sys.exit(0) 
: 다시 같은 물건을 포함

sys.excepthook = _excepthook 

또는 몇 줄은 노호 :

elif cmd == '.ls': 
    return sys.modules[ __name__ ].gDirectory.ls() 

이 아닌 경우 정상적인 물건 취급을 재개합니다.