summaryrefslogtreecommitdiff
path: root/loaders/python/peas-python-internal.c
blob: 9ab55d7a2eb644278e141eca82e0af564e4c4138 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * peas-python-internal.c
 * This file is part of libpeas
 *
 * Copyright (C) 2014-2015 - Garrett Regier
 *
 * libpeas is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libpeas is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "peas-python-internal.h"

#include <gio/gio.h>


static PyObject *internal_module = NULL;
static PyObject *internal_hooks = NULL;
static PyObject *FailedError = NULL;


static PyObject *
failed_fn (PyObject *self,
           PyObject *args)
{
  const gchar *msg;
  gchar *clean_msg;

  if (!PyArg_ParseTuple (args, "s:Hooks.failed", &msg))
    return NULL;

  /* Python tracebacks have a trailing newline */
  clean_msg = g_strchomp (g_strdup (msg));

  g_warning ("%s", clean_msg);

  /* peas_python_internal_call() knows to check for this exception */
  PyErr_SetObject (FailedError, NULL);

  g_free (clean_msg);
  return NULL;
}

static PyMethodDef failed_method_def = {
  "failed", (PyCFunction) failed_fn, METH_VARARGS | METH_STATIC,
  "Prints warning and raises an Exception"
};

gboolean
peas_python_internal_setup (gboolean already_initialized)
{
  const gchar *prgname;
  GBytes *internal_python = NULL;
  PyObject *builtins_module, *globals, *result;
  PyObject *code = NULL, *failed_method = NULL;
  gboolean success = FALSE;

#define goto_error_if_failed(cond) \
  G_STMT_START { \
    if (G_UNLIKELY (!(cond))) \
      { \
        g_warn_if_fail (cond); \
        goto error; \
      } \
  } G_STMT_END

  prgname = g_get_prgname ();
  prgname = prgname == NULL ? "" : prgname;

  builtins_module = PyImport_ImportModule ("builtins");

  goto_error_if_failed (builtins_module != NULL);

  /* We don't use the byte-compiled Python source
   * because glib-compile-resources cannot output
   * depends for generated files.
   *
   * https://bugzilla.gnome.org/show_bug.cgi?id=673101
   */
  internal_python = g_resources_lookup_data ("/org/gnome/libpeas/loaders/"
                                             "python/"
                                             "internal.py",
                                             G_RESOURCE_LOOKUP_FLAGS_NONE,
                                             NULL);
  goto_error_if_failed (internal_python != NULL);

  /* Compile it manually so the filename is available */
  code = Py_CompileString (g_bytes_get_data (internal_python, NULL),
                           "peas-python-internal.py",
                           Py_file_input);
  goto_error_if_failed (code != NULL);

  internal_module = PyModule_New ("libpeas-internal");
  goto_error_if_failed (internal_module != NULL);

  goto_error_if_failed (PyModule_AddStringConstant (internal_module, "__file__",
                                                    "peas-python-internal.py") == 0);
  goto_error_if_failed (PyModule_AddObject (internal_module, "__builtins__",
                                            builtins_module) == 0);
  goto_error_if_failed (PyModule_AddObject (internal_module,
                                            "ALREADY_INITIALIZED",
                                            already_initialized ?
                                            Py_True : Py_False) == 0);
  goto_error_if_failed (PyModule_AddStringConstant (internal_module,
                                                    "PRGNAME",
                                                    prgname) == 0);
  goto_error_if_failed (PyModule_AddStringMacro (internal_module,
                                                 GETTEXT_PACKAGE) == 0);

#ifndef G_OS_WIN32
  goto_error_if_failed (PyModule_AddStringMacro (internal_module,
                                                 PEAS_LOCALEDIR) == 0);
#endif

  globals = PyModule_GetDict (internal_module);
  result = PyEval_EvalCode ((gpointer) code, globals, globals);
  Py_XDECREF (result);

  if (PyErr_Occurred ())
    {
      g_warning ("Failed to run internal Python code");
      goto error;
    }

  internal_hooks = PyDict_GetItemString (globals, "hooks");
  goto_error_if_failed (internal_hooks != NULL);

  FailedError = PyDict_GetItemString (globals, "FailedError");
  goto_error_if_failed (FailedError != NULL);

  failed_method = PyCFunction_NewEx (&failed_method_def,
                                     NULL, internal_module);
  goto_error_if_failed (failed_method != NULL);
  goto_error_if_failed (PyObject_SetAttrString (internal_hooks, "failed",
                                                failed_method) == 0);

  success = TRUE;

#undef goto_error_if_failed

error:

  Py_XDECREF (failed_method);
  Py_XDECREF (code);
  g_clear_pointer (&internal_python, g_bytes_unref);

  if (!success)
    {
      FailedError = NULL;
      internal_hooks = NULL;

      if (internal_module != NULL)
        {
          PyDict_Clear (PyModule_GetDict (internal_module));
          Py_DECREF (internal_module);
        }
    }

  return success;
}

void
peas_python_internal_shutdown (void)
{
  peas_python_internal_call ("exit", NULL, NULL);

  FailedError = NULL;
  internal_hooks = NULL;
  PyDict_Clear (PyModule_GetDict (internal_module));
  Py_DECREF (internal_module);
}

PyObject *
peas_python_internal_call (const gchar  *name,
                           PyTypeObject *return_type,
                           const gchar  *format,
                           ...)
{
  PyObject *args;
  PyObject *result = NULL;
  va_list var_args;

  /* The PyTypeObject for Py_None is not exposed directly */
  if (return_type == NULL)
    return_type = Py_None->ob_type;

  va_start (var_args, format);
  args = Py_VaBuildValue (format == NULL ? "()" : format, var_args);
  va_end (var_args);

  if (args != NULL)
    {
      result = PyObject_CallMethod (internal_hooks, "call", "(sOO)",
                                    name, args, return_type);
      Py_DECREF (args);
    }

  if (PyErr_Occurred ())
    {
      /* Raised by failed_fn() to prevent printing the exception */
      if (PyErr_ExceptionMatches (FailedError))
        {
          PyErr_Clear ();
        }
      else
        {
          g_warning ("Failed to run internal Python hook 'call'");
          PyErr_Print ();
        }

      return NULL;
    }

  if (result == Py_None)
    Py_CLEAR (result);

  return result;
}