Belle II Software light-2405-quaxo
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 83 of file Python.cc.

Constructor & Destructor Documentation

◆ PythonInitializerSingleton()

PythonInitializerSingleton ( )
inlineprivate

Constructor of PythonInitializerSingleton.

Definition at line 100 of file Python.cc.

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

◆ ~PythonInitializerSingleton()

~PythonInitializerSingleton ( )
inlineprivate

Destructor of PythonInitializerSingleton.

Definition at line 118 of file Python.cc.

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

Member Function Documentation

◆ GetInstance()

PythonInitializerSingleton & GetInstance ( )
static

Return static instance of PythonInitializerSingleton.

Definition at line 145 of file Python.cc.

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

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

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

Member Data Documentation

◆ m_initialized_python

bool m_initialized_python = false
private

Member which keeps indicate if this class initialized python.

Definition at line 142 of file Python.cc.


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