Commit ae7c80d2 authored by Garrett Regier's avatar Garrett Regier

Move internal Python code to another file

All code that deals with internal.py is now in
peas-python-internal.c instead of cluttering
the plugin loader code.

https://bugzilla.gnome.org/show_bug.cgi?id=742349
parent 637feef2
......@@ -18,6 +18,8 @@ loader_LTLIBRARIES = libpythonloader.la
libpythonloader_la_SOURCES = \
peas-plugin-loader-python.c \
peas-plugin-loader-python.h \
peas-python-internal.c \
peas-python-internal.h \
peas-python-resources.c
libpythonloader_la_LDFLAGS = \
......
......@@ -25,6 +25,7 @@
#endif
#include "peas-plugin-loader-python.h"
#include "peas-python-internal.h"
#include "libpeas/peas-plugin-info-priv.h"
/* _POSIX_C_SOURCE is defined in Python.h and in limits.h included by
......@@ -48,7 +49,7 @@ typedef struct {
guint init_failed : 1;
guint must_finalize_python : 1;
PyThreadState *py_thread_state;
PyObject *hooks;
PeasPythonInternal *internal;
} PeasPluginLoaderPythonPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (PeasPluginLoaderPython,
......@@ -69,21 +70,6 @@ peas_register_types (PeasObjectModule *module)
PEAS_TYPE_PLUGIN_LOADER_PYTHON);
}
/* NOTE: This must be called with the GIL held */
static void
internal_python_hook (PeasPluginLoaderPython *pyloader,
const gchar *name)
{
PeasPluginLoaderPythonPrivate *priv = GET_PRIV (pyloader);
PyObject *result;
result = PyObject_CallMethod (priv->hooks, (gchar *) name, NULL);
Py_XDECREF (result);
if (PyErr_Occurred ())
PyErr_Print ();
}
/* NOTE: This must be called with the GIL held */
static PyTypeObject *
find_python_extension_type (GType exten_type,
......@@ -341,7 +327,7 @@ peas_plugin_loader_python_unload (PeasPluginLoader *loader,
* loader will not be finalized by applications
*/
if (--priv->n_loaded_plugins == 0)
internal_python_hook (pyloader, "all_plugins_unloaded");
peas_python_internal_call (priv->internal, "all_plugins_unloaded");
info->loader_data = NULL;
PyGILState_Release (state);
......@@ -434,8 +420,7 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
PeasPluginLoaderPythonPrivate *priv = GET_PRIV (pyloader);
PyGILState_STATE state = 0;
long hexversion;
GBytes *internal_python;
PyObject *gettext, *result, *builtins_module, *code, *globals;
PyObject *gettext, *result;
const gchar *prgname;
#if PY_VERSION_HEX < 0x03000000
const char *argv[] = { NULL, NULL };
......@@ -572,94 +557,10 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
goto python_init_error;
}
#if PY_VERSION_HEX < 0x03000000
builtins_module = PyImport_ImportModule ("__builtin__");
#else
builtins_module = PyImport_ImportModule ("builtins");
#endif
if (builtins_module == NULL)
goto python_init_error;
/* 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/"
#if PY_VERSION_HEX < 0x03000000
"python/"
#else
"python3/"
#endif
"internal.py",
G_RESOURCE_LOOKUP_FLAGS_NONE,
NULL);
if (internal_python == NULL)
priv->internal = peas_python_internal_new ();
if (priv->internal == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to locate internal python code");
goto python_init_error;
}
/* 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);
g_bytes_unref (internal_python);
if (code == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to compile internal python code");
goto python_init_error;
}
globals = PyDict_New ();
if (globals == NULL)
{
Py_DECREF (code);
goto python_init_error;
}
if (PyDict_SetItemString (globals, "__builtins__",
PyModule_GetDict (builtins_module)) != 0)
{
Py_DECREF (globals);
Py_DECREF (code);
goto python_init_error;
}
result = PyEval_EvalCode ((gpointer) code, globals, globals);
Py_XDECREF (result);
Py_DECREF (code);
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to run internal python code");
Py_DECREF (globals);
goto python_init_error;
}
priv->hooks = PyDict_GetItemString (globals, "hooks");
Py_XINCREF (priv->hooks);
Py_DECREF (globals);
if (priv->hooks == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to find internal python hooks");
/* Already warned */
goto python_init_error;
}
......@@ -715,14 +616,11 @@ peas_plugin_loader_python_finalize (GObject *object)
PyGILState_Release (state);
}
if (priv->hooks != NULL && !priv->init_failed)
if (priv->internal != NULL && !priv->init_failed)
{
state = PyGILState_Ensure ();
internal_python_hook (pyloader, "exit");
peas_python_internal_free (priv->internal);
PyGILState_Release (state);
/* Borrowed Reference */
priv->hooks = NULL;
}
if (priv->py_thread_state)
......
/*
* peas-python-internal.c
* This file is part of libpeas
*
* Copyright (C) 2014-2015 - Garrett Regier
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "peas-python-internal.h"
#include <gio/gio.h>
/* _POSIX_C_SOURCE is defined in Python.h and in limits.h included by
* glib-object.h, so we unset it here to avoid a warning. Yep, that's bad.
*/
#undef _POSIX_C_SOURCE
#include <Python.h>
typedef PyObject _PeasPythonInternal;
PeasPythonInternal *
peas_python_internal_new (void)
{
GBytes *internal_python;
PyObject *builtins_module, *code, *globals, *result, *internal;
#if PY_MAJOR_VERSION < 3
builtins_module = PyImport_ImportModule ("__builtin__");
#else
builtins_module = PyImport_ImportModule ("builtins");
#endif
if (builtins_module == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to get builtins module");
return 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/"
#if PY_MAJOR_VERSION < 3
"python/"
#else
"python3/"
#endif
"internal.py",
G_RESOURCE_LOOKUP_FLAGS_NONE,
NULL);
if (internal_python == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to locate internal Python code");
return 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);
g_bytes_unref (internal_python);
if (code == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to compile internal Python code");
return NULL;
}
globals = PyDict_New ();
if (globals == NULL)
{
Py_DECREF (code);
return NULL;
}
if (PyDict_SetItemString (globals, "__builtins__",
PyModule_GetDict (builtins_module)) != 0)
{
Py_DECREF (globals);
Py_DECREF (code);
return NULL;
}
result = PyEval_EvalCode ((gpointer) code, globals, globals);
Py_XDECREF (result);
Py_DECREF (code);
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to run internal Python code");
Py_DECREF (globals);
return NULL;
}
internal = PyDict_GetItemString (globals, "hooks");
Py_XINCREF (internal);
Py_DECREF (globals);
if (internal == NULL)
{
g_warning ("Error initializing Python Plugin Loader: "
"failed to find internal Python hooks");
return NULL;
}
return (PeasPythonInternal *) internal;
}
/* NOTE: This must be called with the GIL held */
void
peas_python_internal_free (PeasPythonInternal *internal)
{
PyObject *internal_ = (PyObject *) internal;
peas_python_internal_call (internal, "exit");
Py_DECREF (internal_);
}
/* NOTE: This must be called with the GIL held */
void
peas_python_internal_call (PeasPythonInternal *internal,
const gchar *name)
{
PyObject *internal_ = (PyObject *) internal;
PyObject *result;
result = PyObject_CallMethod (internal_, (gchar *) name, NULL);
Py_XDECREF (result);
if (PyErr_Occurred ())
PyErr_Print ();
}
/*
* peas-python-internal.h
* This file is part of libpeas
*
* Copyright (C) 2015 - Garrett Regier
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __PEAS_PYTHON_INTERNAL_H__
#define __PEAS_PYTHON_INTERNAL_H__
#include <glib.h>
G_BEGIN_DECLS
typedef struct _PeasPythonInternal PeasPythonInternal;
PeasPythonInternal *
peas_python_internal_new (void);
void peas_python_internal_free (PeasPythonInternal *internal);
void peas_python_internal_call (PeasPythonInternal *internal,
const gchar *name);
G_END_DECLS
#endif /* __PEAS_PYTHON_INTERNAL_H__ */
......@@ -18,6 +18,8 @@ loader_LTLIBRARIES = libpython3loader.la
libpython3loader_la_SOURCES = \
$(top_srcdir)/loaders/python/peas-plugin-loader-python.c \
$(top_srcdir)/loaders/python/peas-plugin-loader-python.h \
$(top_srcdir)/loaders/python/peas-python-internal.c \
$(top_srcdir)/loaders/python/peas-python-internal.h \
peas-python3-resources.c
libpython3loader_la_LDFLAGS = \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment