GnuCash  5.6-150-g038405b370+
gncmod-python.c
1 /*********************************************************************
2  * gncmod-python.c
3  * Python in GnuCash?! Sweet.
4  *
5  * Copyright (c) 2011 Andy Clayton
6  *********************************************************************/
7 /********************************************************************\
8  * This program is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License as *
10  * published by the Free Software Foundation; either version 2 of *
11  * the License, or (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License*
19  * along with this program; if not, contact: *
20  * *
21  * Free Software Foundation Voice: +1-617-542-5942 *
22  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
23  * Boston, MA 02110-1301, USA gnu@gnu.org *
24  * *
25 \********************************************************************/
26 
27 
28 #include <Python.h>
29 #include <config.h>
30 #include <gmodule.h>
31 #include <stdio.h>
32 
33 #include "gnc-module.h"
34 #include "gnc-module-api.h"
35 #include "gnc-path.h"
36 
37 GNC_MODULE_API_DECL(libgncmod_python)
38 
39 /* version of the gnc module system interface we require */
40 int libgncmod_python_gnc_module_system_interface = 0;
41 
42 /* module versioning uses libtool semantics. */
43 int libgncmod_python_gnc_module_current = 0;
44 int libgncmod_python_gnc_module_revision = 0;
45 int libgncmod_python_gnc_module_age = 0;
46 
47 
48 char *
49 libgncmod_python_gnc_module_path(void)
50 {
51  return g_strdup("gnucash/python");
52 }
53 
54 char *
55 libgncmod_python_gnc_module_description(void)
56 {
57  return g_strdup("An embedded Python interpreter");
58 }
59 
60 #if PY_VERSION_HEX >= 0x030b0000
61 // PySys_SetArgv is deprecated in 3.11
62 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
63 #endif
64 
65 int
66 libgncmod_python_gnc_module_init(int refcount)
67 {
68 #ifdef __WIN32
69  wchar_t* argv = NULL;
70 #else
71  char* argv = NULL;
72 #endif
73  PyStatus status;
74  PyConfig config;
75  PyConfig_InitPythonConfig(&config);
76  status = PyConfig_SetBytesArgv(&config, 0, &argv);
77  if (PyStatus_Exception(status))
78  {
79  PyConfig_Clear(&config);
80  return FALSE;
81  }
82  Py_Initialize();
83  gchar *pkgdatadir = gnc_path_get_pkgdatadir();
84  gchar *init_filename = g_build_filename(pkgdatadir, "python/init.py", (char*)NULL);
85  g_debug("Looking for python init script at %s", init_filename);
86 #ifdef __WIN32
87  FILE *fp = fopen(init_filename, "rb");
88 #else
89  FILE *fp = fopen(init_filename, "r");
90 #endif
91  if (fp)
92  {
93  PyRun_SimpleFile(fp, init_filename);
94  fclose(fp);
95 
96  /* PyRun_InteractiveLoop(stdin, "foo"); */
97  }
98  else
99  {
100  g_warning("Unable to initialize Python module (unable to open %s)", init_filename);
101  }
102  g_free(init_filename);
103  g_free(pkgdatadir);
104  PyConfig_Clear(&config);
105 
106  return TRUE;
107 }
108 
109 int
110 libgncmod_python_gnc_module_end(int refcount)
111 {
112  Py_Finalize();
113  return TRUE;
114 }