gem5 v24.0.0.0
Loading...
Searching...
No Matches
pybind_init.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef __PYTHON_PYBIND_INIT_HH__
29#define __PYTHON_PYBIND_INIT_HH__
30
31#include <Python.h>
32
33#include "pybind11/pybind11.h"
34
35namespace gem5
36{
37
39{
40 PybindModuleInit(const char *name, PyObject *(*func)())
41 {
42 if (Py_IsInitialized()) {
43 // If python is running, initialize and inject the module.
44 PyImport_AddModule(name);
45 PyObject *sys_modules = PyImport_GetModuleDict();
46 PyDict_SetItemString(sys_modules, name, func());
47 } else {
48 // If not, tell python how to build importer itself later.
49 PyImport_AppendInittab(name, func);
50 }
51 }
52};
53
54} // namespace gem5
55
56#define GEM5_PYBIND_MODULE_INIT(name, func) \
57namespace { \
58 \
59::PyObject * \
60initializer() \
61{ \
62 static ::pybind11::module_::module_def mod_def; \
63 static auto m = ::pybind11::module_::create_extension_module( \
64 #name, nullptr, &mod_def); \
65 func(m); \
66 m.inc_ref(); \
67 return m.ptr(); \
68} \
69 \
70::gem5::PybindModuleInit modInit(#name, initializer); \
71 \
72} // anonymous namespace
73
74#endif // __PYTHON_PYBIND_INIT_HH__
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
PybindModuleInit(const char *name, PyObject *(*func)())
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0