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 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();
104 }
105
106 if (PyArray_API == nullptr) {
107 init_numpy();
108 }
109 }
void * init_numpy()
Helper function which initializes array system of numpy.
Definition: Python.cc:131
bool m_initialized_python
Member which keeps indicate if this class initialized python.
Definition: Python.cc:138

◆ ~PythonInitializerSingleton()

~PythonInitializerSingleton ( )
inlineprivate

Destructor of PythonInitializerSingleton.

Definition at line 114 of file Python.cc.

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

Member Function Documentation

◆ GetInstance()

PythonInitializerSingleton & GetInstance ( )
static

Return static instance of PythonInitializerSingleton.

Definition at line 141 of file Python.cc.

142 {
143 static PythonInitializerSingleton singleton;
144 return singleton;
145 }
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 131 of file Python.cc.

132 {
133 // Import array is a macro which returns NUMPY_IMPORT_ARRAY_RETVAL
134 import_array();
135 return nullptr;
136 }

Member Data Documentation

◆ m_initialized_python

bool m_initialized_python = false
private

Member which keeps indicate if this class initialized python.

Definition at line 138 of file Python.cc.


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