File: PythonCommon.h

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (48 lines) | stat: -rw-r--r-- 1,700 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef __FreeOrion__Util__PythonCommon__
#define __FreeOrion__Util__PythonCommon__

#include "../util/boost_fix.h"
#include <boost/python.hpp>

#include "Export.h"

class FO_COMMON_API PythonCommon {
public:
    PythonCommon() = default;
    virtual ~PythonCommon();

    /** IsPythonRunning returns true is the python interpreter is
        initialized.  It is typically called after
        HandleErrorAlreadySet() to determine if the error caused the
        interpreter to shutdown. */
    bool IsPythonRunning() const;

    /** Handles boost::python::error_already_set.
        If the error is SystemExit the python interpreter is finalized
        and no longer available.

        Call PyErr_Print() if the exception is an error.

        HandleErrorAlreadySet is idempotent, calling it multiple times
        won't crash or hang the process. */
    void HandleErrorAlreadySet();

    bool Initialize();        // initializes and runs the Python interpreter, prepares the Python environment

    virtual bool InitCommonImports(); // initializes Python imports

    void Finalize();          // stops Python interpreter and releases its resources
private:
    // some helper objects needed to initialize and run the Python interface
#if defined(FREEORION_MACOSX) || defined(FREEORION_WIN32) || defined(FREEORION_ANDROID)
    wchar_t* m_home_dir = nullptr;
    wchar_t* m_program_name = nullptr;
#endif
    // A copy of the systemExit exception to compare with returned
    // exceptions.  It can't be created in the exception handler.
    boost::python::object m_system_exit;
    boost::python::object m_traceback_format_exception;
};

#endif /* defined(__FreeOrion__Util__PythonCommon__) */