ROOT  6.06/09
Reference Guide
TPyROOTApplication.cxx
Go to the documentation of this file.
1 // Author: Wim Lavrijsen, February 2006
2 
3 // Bindings
4 #include "PyROOT.h"
5 #include "TPyROOTApplication.h"
6 #include "Utility.h"
7 
8 // ROOT
9 #include "TROOT.h"
10 #include "TInterpreter.h"
11 #include "TSystem.h"
12 #include "TBenchmark.h"
13 #include "TStyle.h"
14 #include "TError.h"
15 #include "Getline.h"
16 //#include "TVirtualX.h"
17 
18 // Standard
19 #include <string.h>
20 
21 
22 //______________________________________________________________________________
23 // Setup interactive application for python
24 // ========================================
25 //
26 // The TPyROOTApplication sets up the nuts and bolts for interactive ROOT use
27 // from python, closely following TRint. Note that not everything is done here,
28 // some bits (such as e.g. the use of exception hook for shell escapes) are more
29 // easily done in python and you'll thus find them ROOT.py
30 //
31 // The intended use of this class is from python only. It is used by default in
32 // ROOT.py, so if you do not want to have a TApplication derived object created
33 // for you, you'll need to load libPyROOT.so instead.
34 //
35 // The static InitXYZ functions are used in conjunction with TPyROOTApplication
36 // in ROOT.py, but they can be used independently.
37 //
38 // NOTE: This class will receive the command line arguments from sys.argv. A
39 // distinction between arguments for TApplication and user arguments can be
40 // made by using "-" or "--" as a separator on the command line.
41 
42 
43 //- data ---------------------------------------------------------------------
45 
46 
47 //- constructors/destructor --------------------------------------------------
48 PyROOT::TPyROOTApplication::TPyROOTApplication(
49  const char* acn, int* argc, char** argv, Bool_t /*bLoadLibs*/ ) :
50  TApplication( acn, argc, argv )
51 {
52 // The following code is redundant with ROOT6 and the PCH: the headers are
53 // available to the interpreter.
54 // // Create a TApplication derived for use with interactive ROOT from python. A
55 // // set of standard, often used libs is loaded if bLoadLibs is true (default).
56 //
57 // if ( bLoadLibs ) // note that this section could be programmed in python
58 // {
59 // // follow TRint to minimize differences with root.exe (note: changed <pair>
60 // // to <utility> for Cling, which is correct)
61 // ProcessLine( "#include <iostream>", kTRUE );
62 // ProcessLine( "#include <string>", kTRUE ); // for std::string iostream.
63 // ProcessLine( "#include <vector>", kTRUE ); // needed because they're used within the
64 // ProcessLine( "#include <utility>", kTRUE ); // core ROOT dicts and CINT won't be able
65 // // to properly unload these files
66 // }
67 
68 #ifdef WIN32
69  // switch win32 proxy main thread id
70  if (gVirtualX)
71  ProcessLine("((TGWin32 *)gVirtualX)->SetUserThreadId(0);", kTRUE);
72 #endif
73 
74 // save current interpreter context
75  gInterpreter->SaveContext();
76  gInterpreter->SaveGlobalsContext();
77 
78 // prevent crashes on accessing history
79  Gl_histinit( (char*)"-" );
80 
81 // prevent ROOT from exiting python
82  SetReturnFromRun( kTRUE );
83 }
84 
85 
86 //- static public members ----------------------------------------------------
88 {
89 // Create a TPyROOTApplication. Returns false if gApplication is not null.
90 
91  if ( ! gApplication ) {
92  // retrieve arg list from python, translate to raw C, pass on
93  PyObject* argl = PySys_GetObject( const_cast< char* >( "argv" ) );
94 
95  int argc = 1;
96  if ( argl && 0 < PyList_Size( argl ) ) argc = (int)PyList_GET_SIZE( argl );
97  char** argv = new char*[ argc ];
98  for ( int i = 1; i < argc; ++i ) {
99  char* argi = PyROOT_PyUnicode_AsString( PyList_GET_ITEM( argl, i ) );
100  if ( strcmp( argi, "-" ) == 0 || strcmp( argi, "--" ) == 0 ) {
101  // stop collecting options, the remaining are for the python script
102  argc = i; // includes program name
103  break;
104  }
105  argv[ i ] = argi;
106  }
107 #if PY_VERSION_HEX < 0x03000000
108  if ( Py_GetProgramName() && strlen( Py_GetProgramName() ) != 0 )
109  argv[ 0 ] = Py_GetProgramName();
110  else
111  argv[ 0 ] = (char*)"python";
112 #else
113 // TODO: convert the wchar_t*
114  argv[ 0 ] = (char*)"python";
115 #endif
116 
117  gApplication = new TPyROOTApplication( "PyROOT", &argc, argv, bLoadLibs );
118  delete[] argv; // TApplication ctor has copied argv, so done with it
119 
120  return kTRUE;
121  }
122 
123  return kFALSE;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Setup the basic ROOT globals gBenchmark, gStyle, gProgname, if not already
128 /// set. Always returns true.
129 
131 {
132  if ( ! gBenchmark ) gBenchmark = new TBenchmark();
133  if ( ! gStyle ) gStyle = new TStyle();
134 
135  if ( ! gProgName ) // should have been set by TApplication
136 #if PY_VERSION_HEX < 0x03000000
137  gSystem->SetProgname( Py_GetProgramName() );
138 #else
139 // TODO: convert the wchar_t*
140  gSystem->SetProgname( "python" );
141 #endif
142 
143  return kTRUE;
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Install ROOT message handler which will turn ROOT error message into
148 /// python exceptions. Always returns true.
149 
151 {
153  return kTRUE;
154 }
ErrorHandlerFunc_t SetErrorHandler(ErrorHandlerFunc_t newhandler)
Set an errorhandler function. Returns the old handler.
Definition: TError.cxx:106
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
#define gInterpreter
Definition: TInterpreter.h:502
R__EXTERN TApplication * gApplication
Definition: TApplication.h:171
#define PyROOT_PyUnicode_AsString
Definition: PyROOT.h:66
static Bool_t InitROOTGlobals()
Setup the basic ROOT globals gBenchmark, gStyle, gProgname, if not already set.
TStyle objects may be created to define special styles.
Definition: TStyle.h:52
void ErrMsgHandler(int level, Bool_t abort, const char *location, const char *msg)
Translate ROOT error/warning to python.
Definition: Utility.cxx:791
R__EXTERN const char * gProgName
Definition: TSystem.h:234
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
static Bool_t InitROOTMessageCallback()
Install ROOT message handler which will turn ROOT error message into python exceptions.
This class is a ROOT utility to help benchmarking applications.
Definition: TBenchmark.h:33
TPyROOTApplication(const char *acn, Int_t *argc, char **argv, Bool_t bLoadLibs=kTRUE)
#define gVirtualX
Definition: TVirtualX.h:362
ClassImp(PyROOT::TPyROOTApplication) PyROOT
virtual void SetProgname(const char *name)
Set the application name (from command line, argv[0]) and copy it in gProgName.
Definition: TSystem.cxx:227
void(* ErrorHandlerFunc_t)(int level, Bool_t abort, const char *location, const char *msg)
Definition: TError.h:48
static Bool_t CreatePyROOTApplication(Bool_t bLoadLibs=kTRUE)
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
const Bool_t kTRUE
Definition: Rtypes.h:91
_object PyObject
Definition: TPyArg.h:22