File: pyboot.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (79 lines) | stat: -rw-r--r-- 2,409 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <lib/base/Logging.hpp>
#include <core/Omega.hpp>
#include <signal.h>

CREATE_CPP_LOCAL_LOGGER("pyboot.cpp");

#ifdef YADE_DEBUG
void crashHandler(int sig)
{
	switch (sig) {
		case SIGABRT:
		case SIGSEGV: {
			signal(SIGSEGV, SIG_DFL);
			signal(SIGABRT, SIG_DFL); // prevent loops - default handlers
			std::cerr << "SIGSEGV/SIGABRT handler called; gdb batch file is `" << yade::Omega::instance().gdbCrashBatch << "'" << std::endl;
			int ret = std::system((std::string("gdb -x ") + yade::Omega::instance().gdbCrashBatch).c_str());
			if (ret != 0) std::cerr << "unable to execute gdb" << std::endl;
			raise(sig); // reemit signal after exiting gdb
		} break;
		default: break;
	}
}
#endif

namespace forCtags {
struct pyboot {
}; // for ctags
}

/* Initialize yade, loading given plugins */
void yadeInitialize(boost::python::list& pp, const std::string& confDir)
{
#if PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 7
	PyEval_InitThreads();
#else
	Py_Initialize();
#endif

	yade::Omega& O(yade::Omega::instance());
	O.init();
	O.origArgv = NULL;
	O.origArgc = 0; // not needed, anyway
	O.confDir  = confDir;
	O.initTemps();
#ifdef YADE_DEBUG
	std::ofstream gdbBatch;
	O.gdbCrashBatch = O.tmpFilename();
	gdbBatch.open(O.gdbCrashBatch.c_str());
	gdbBatch << "attach " << boost::lexical_cast<std::string>(getpid()) << "\nset pagination off\nthread apply all backtrace\ndetach\nquit\n";
	gdbBatch.close();
	signal(SIGABRT, crashHandler);
	signal(SIGSEGV, crashHandler);
#endif

	std::vector<std::string> ppp;
	for (int i = 0; i < boost::python::len(pp); i++) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // pp[i] is the argument, not sure why compiler thinks it's uninitialized ...
		ppp.push_back(boost::python::extract<std::string>(pp[i]));
#pragma GCC diagnostic pop
	}
	yade::Omega::instance().loadPlugins(ppp);
}

void yadeFinalize() { yade::Omega::instance().cleanupTemps(); }

BOOST_PYTHON_MODULE(boot)
try {
	boost::python::scope().attr("initialize") = &yadeInitialize;
	boost::python::scope().attr("finalize")   = &yadeFinalize; //,"Finalize yade (only to be used internally).")

} catch (...) {
	LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
	PyErr_Print();
	PyErr_SetString(PyExc_SystemError, __FILE__);
	boost::python::handle_exception();
	throw;
}