Belle II Software light-2406-ragdoll
PythonInitializerSingleton Class Reference

Singleton class which handles the initialization and finalization of Python and numpy. More...

Public Member Functions

 PythonInitializerSingleton (const PythonInitializerSingleton &)=delete
 Forbid copy constructor of PythonInitializerSingleton.
 

Static Public Member Functions

static PythonInitializerSingletonGetInstance ()
 Return static instance of PythonInitializerSingleton.
 

Private Member Functions

 PythonInitializerSingleton ()
 Constructor of PythonInitializerSingleton.
 
 ~PythonInitializerSingleton ()
 Destructor of PythonInitializerSingleton.
 
void * init_numpy ()
 Helper function which initializes array system of numpy.
 

Private Attributes

bool m_initialized_python = false
 Member which keeps indicate if this class initialized python.
 

Detailed Description

Singleton class which handles the initialization and finalization of Python and numpy.

Definition at line 82 of file Python.cc.

Constructor & Destructor Documentation

◆ PythonInitializerSingleton()

PythonInitializerSingleton ( )
inlineprivate

Constructor of PythonInitializerSingleton.

Definition at line 99 of file Python.cc.

100 {
101 if (not Py_IsInitialized()) {
102 Py_Initialize();
103 // wchar_t* bla[] = {L""};
104 wchar_t** bla = nullptr;
105 PySys_SetArgvEx(0, bla, 0);
107 }
108
109 if (PyArray_API == nullptr) {
110 init_numpy();
111 }
112 }
void * init_numpy()
Helper function which initializes array system of numpy.
Definition: Python.cc:134
bool m_initialized_python
Member which keeps indicate if this class initialized python.
Definition: Python.cc:141

◆ ~PythonInitializerSingleton()

~PythonInitializerSingleton ( )
inlineprivate

Destructor of PythonInitializerSingleton.

Definition at line 117 of file Python.cc.

118 {
120 if (Py_IsInitialized()) {
121 // We don't finalize Python because this call only frees some memory,
122 // but can cause crashes in loaded python-modules like Theano
123 // https://docs.python.org/3/c-api/init.html
124 // Py_Finalize();
125 }
126 }
127 }

Member Function Documentation

◆ GetInstance()

PythonInitializerSingleton & GetInstance ( )
static

Return static instance of PythonInitializerSingleton.

Definition at line 144 of file Python.cc.

145 {
146 static PythonInitializerSingleton singleton;
147 return singleton;
148 }
PythonInitializerSingleton()
Constructor of PythonInitializerSingleton.
Definition: Python.cc:99

◆ init_numpy()

void * init_numpy ( )
inlineprivate

Helper function which initializes array system of numpy.

Since import_array is a weird macro we need this wrapper function to protect us from the return statement in this macro

Definition at line 134 of file Python.cc.

135 {
136 // Import array is a macro which returns NUMPY_IMPORT_ARRAY_RETVAL
137 import_array();
138 return nullptr;
139 }

Member Data Documentation

◆ m_initialized_python

bool m_initialized_python = false
private

Member which keeps indicate if this class initialized python.

Definition at line 141 of file Python.cc.


The documentation for this class was generated from the following file: