Belle II Software development
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 100 of file Python.cc.

Constructor & Destructor Documentation

◆ PythonInitializerSingleton()

PythonInitializerSingleton ( )
inlineprivate

Constructor of PythonInitializerSingleton.

Definition at line 117 of file Python.cc.

118 {
119 if (not Py_IsInitialized()) {
120 Py_Initialize();
121 m_initialized_python = true;
122 }
123
124 if (PyArray_API == nullptr) {
125 init_numpy();
126 }
127 }

◆ ~PythonInitializerSingleton()

~PythonInitializerSingleton ( )
inlineprivate

Destructor of PythonInitializerSingleton.

Definition at line 132 of file Python.cc.

133 {
134 if (m_initialized_python) {
135 if (Py_IsInitialized()) {
136 // We don't finalize Python because this call only frees some memory,
137 // but can cause crashes in loaded python-modules like Theano
138 // https://docs.python.org/3/c-api/init.html
139 // Py_Finalize();
140 }
141 }
142 }

Member Function Documentation

◆ GetInstance()

PythonInitializerSingleton & GetInstance ( )
static

Return static instance of PythonInitializerSingleton.

Definition at line 159 of file Python.cc.

160 {
161 static PythonInitializerSingleton singleton;
162 return singleton;
163 }

◆ 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 149 of file Python.cc.

150 {
151 // Import array is a macro which returns NUMPY_IMPORT_ARRAY_RETVAL
152 import_array();
153 return nullptr;
154 }

Member Data Documentation

◆ m_initialized_python

bool m_initialized_python = false
private

Member which keeps indicate if this class initialized python.

Definition at line 156 of file Python.cc.


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