How does Pyroot change the python interpreter?

If I try to run .lsin python, it is not surprising that I get a SyntaxError

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

But if I import PyROOT, it somehow makes this syntax legal (and behaves the same as in ROOT, listing the contents of the current file, in this example I did not open it).

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

Likewise, it .qworks to exit the Python interpreter after I imported ROOT, as in a regular ROOT interpreter.

How it works?

+4
source share
1 answer

This can be found in lib / ROOT.py

there is a condition that if not ipython, sys.excepthook is overridden:

sys.excepthook = _excepthook

which, in turn, contains such things as:

### 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 )

or a few lines below:

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

, .

+4

All Articles