More python api
parent
693422c207
commit
08c1c72957
|
@ -680,7 +680,7 @@ moo_app_python_run_file (MooApp *app,
|
|||
file = _moo_fopen (filename, "r");
|
||||
g_return_val_if_fail (file != NULL, FALSE);
|
||||
|
||||
res = moo_python_run_file (file, filename);
|
||||
res = moo_python_run_file (file, filename, NULL, NULL);
|
||||
|
||||
fclose (file);
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ moo_python_api_run_simple_string (const char *str)
|
|||
|
||||
|
||||
static MooPyObject *
|
||||
get_script_dict (const char *name)
|
||||
moo_python_api_create_script_dict (const char *name)
|
||||
{
|
||||
PyObject *dict, *builtins;
|
||||
|
||||
|
@ -58,15 +58,15 @@ get_script_dict (const char *name)
|
|||
|
||||
static MooPyObject*
|
||||
moo_python_api_run_string (const char *str,
|
||||
MooPyObject *locals,
|
||||
MooPyObject *globals)
|
||||
MooPyObject *globals,
|
||||
MooPyObject *locals)
|
||||
{
|
||||
PyObject *ret;
|
||||
|
||||
g_return_val_if_fail (str != NULL, NULL);
|
||||
|
||||
if (!locals)
|
||||
locals = get_script_dict ("__script__");
|
||||
locals = moo_python_api_create_script_dict ("__script__");
|
||||
else
|
||||
moo_Py_INCREF (locals);
|
||||
|
||||
|
@ -75,36 +75,25 @@ moo_python_api_run_string (const char *str,
|
|||
if (!globals)
|
||||
globals = locals;
|
||||
|
||||
ret = PyRun_String (str, Py_file_input, (PyObject*) locals, (PyObject*) globals);
|
||||
ret = PyRun_String (str, Py_file_input, (PyObject*) globals, (PyObject*) locals);
|
||||
|
||||
moo_Py_DECREF (locals);
|
||||
return (MooPyObject*) ret;
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject*
|
||||
moo_python_api_run_file (gpointer fp,
|
||||
const char *filename)
|
||||
{
|
||||
PyObject *dict, *main_mod;
|
||||
g_return_val_if_fail (fp != NULL && filename != NULL, NULL);
|
||||
main_mod = PyImport_AddModule ((char*)"__main__");
|
||||
dict = PyModule_GetDict (main_mod);
|
||||
return (MooPyObject*) PyRun_File (fp, filename, Py_file_input, dict, dict);
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject *
|
||||
moo_python_api_run_code (const char *str,
|
||||
MooPyObject *locals,
|
||||
MooPyObject *globals)
|
||||
moo_python_api_run_file (gpointer fp,
|
||||
const char *filename,
|
||||
MooPyObject *globals,
|
||||
MooPyObject *locals)
|
||||
{
|
||||
PyObject *ret;
|
||||
|
||||
g_return_val_if_fail (str != NULL, NULL);
|
||||
g_return_val_if_fail (fp != NULL && filename != NULL, NULL);
|
||||
|
||||
if (!locals)
|
||||
locals = get_script_dict ("__script__");
|
||||
locals = moo_python_api_create_script_dict ("__script__");
|
||||
else
|
||||
moo_Py_INCREF (locals);
|
||||
|
||||
|
@ -113,7 +102,33 @@ moo_python_api_run_code (const char *str,
|
|||
if (!globals)
|
||||
globals = locals;
|
||||
|
||||
ret = PyRun_String (str, Py_file_input, (PyObject*) locals, (PyObject*) globals);
|
||||
ret = PyRun_File (fp, filename, Py_file_input, (PyObject*) globals, (PyObject*) locals);
|
||||
|
||||
moo_Py_DECREF (locals);
|
||||
return (MooPyObject*) ret;
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject *
|
||||
moo_python_api_run_code (const char *str,
|
||||
MooPyObject *globals,
|
||||
MooPyObject *locals)
|
||||
{
|
||||
PyObject *ret;
|
||||
|
||||
g_return_val_if_fail (str != NULL, NULL);
|
||||
|
||||
if (!locals)
|
||||
locals = moo_python_api_create_script_dict ("__script__");
|
||||
else
|
||||
moo_Py_INCREF (locals);
|
||||
|
||||
g_return_val_if_fail (locals != NULL, NULL);
|
||||
|
||||
if (!globals)
|
||||
globals = locals;
|
||||
|
||||
ret = PyRun_String (str, Py_file_input, (PyObject*) globals, (PyObject*) locals);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
|
@ -158,6 +173,27 @@ moo_python_api_py_object_from_gobject (gpointer gobj)
|
|||
return (MooPyObject*) pygobject_new (gobj);
|
||||
}
|
||||
|
||||
static gpointer
|
||||
moo_python_api_gobject_from_py_object (MooPyObject *pyobj)
|
||||
{
|
||||
GValue val;
|
||||
|
||||
g_return_val_if_fail (pyobj != NULL, NULL);
|
||||
|
||||
val.g_type = 0;
|
||||
g_value_init (&val, G_TYPE_OBJECT);
|
||||
|
||||
if (pyg_value_from_pyobject (&val, (PyObject*) pyobj) == 0)
|
||||
{
|
||||
gpointer ret = g_value_get_object (&val);
|
||||
g_value_unset (&val);
|
||||
return ret;
|
||||
}
|
||||
|
||||
PyErr_Clear ();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject *
|
||||
moo_python_api_dict_get_item (MooPyObject *dict,
|
||||
|
@ -402,7 +438,9 @@ moo_python_api_init (void)
|
|||
moo_python_api_run_string,
|
||||
moo_python_api_run_file,
|
||||
moo_python_api_run_code,
|
||||
moo_python_api_create_script_dict,
|
||||
moo_python_api_py_object_from_gobject,
|
||||
moo_python_api_gobject_from_py_object,
|
||||
moo_python_api_dict_get_item,
|
||||
moo_python_api_dict_set_item,
|
||||
moo_python_api_dict_del_item,
|
||||
|
|
|
@ -3,7 +3,7 @@ type=Python
|
|||
file=simple.py
|
||||
# this version string must match whatever is used in current version
|
||||
# it is not a plugin version
|
||||
version=0.8
|
||||
version=1.0
|
||||
|
||||
[plugin]
|
||||
id=APlugin
|
||||
|
|
|
@ -54,15 +54,19 @@ struct _MooPyAPI {
|
|||
|
||||
MooPyObject* (*run_simple_string) (const char *str);
|
||||
MooPyObject* (*run_string) (const char *str,
|
||||
MooPyObject *locals,
|
||||
MooPyObject *globals);
|
||||
MooPyObject *globals,
|
||||
MooPyObject *locals);
|
||||
MooPyObject* (*run_file) (void *fp,
|
||||
const char *filename);
|
||||
const char *filename,
|
||||
MooPyObject *globals,
|
||||
MooPyObject *locals);
|
||||
MooPyObject* (*run_code) (const char *str,
|
||||
MooPyObject *locals,
|
||||
MooPyObject *globals);
|
||||
MooPyObject *globals,
|
||||
MooPyObject *locals);
|
||||
MooPyObject* (*create_script_dict) (const char *name);
|
||||
|
||||
MooPyObject* (*py_object_from_gobject) (gpointer gobj);
|
||||
gpointer (*gobject_from_py_object) (MooPyObject *pyobj);
|
||||
|
||||
MooPyObject* (*dict_get_item) (MooPyObject *dict,
|
||||
const char *key);
|
||||
|
@ -114,6 +118,7 @@ void moo_Py_DECREF (MooPyObject *obj);
|
|||
#define moo_python_run_string moo_py_api->run_string
|
||||
#define moo_python_run_file moo_py_api->run_file
|
||||
#define moo_python_run_code moo_py_api->run_code
|
||||
#define moo_python_create_script_dict moo_py_api->create_script_dict
|
||||
|
||||
#define moo_py_dict_get_item moo_py_api->dict_get_item
|
||||
#define moo_py_dict_set_item moo_py_api->dict_set_item
|
||||
|
@ -121,6 +126,7 @@ void moo_Py_DECREF (MooPyObject *obj);
|
|||
|
||||
#define moo_py_import_exec moo_py_api->import_exec
|
||||
#define moo_py_object_from_gobject moo_py_api->py_object_from_gobject
|
||||
#define moo_gobject_from_py_object moo_py_api->gobject_from_py_object
|
||||
|
||||
#define moo_PyErr_Print moo_py_api->py_err_print
|
||||
#define moo_PyObject_CallMethod moo_py_api->py_object_call_method
|
||||
|
|
Loading…
Reference in New Issue