On Sun, 2010-02-28 at 11:23 +0100, Sven Neumann wrote:
> On Sat, 2010-02-27 at 14:35 -0500, lloyd konneker wrote:
> > This is an enhancement request.  Repeated calls to gimp_env_init should
> > yield warnings and not fatal errors.  It has benefits for gimp plugins
> > written in Python.
> > 
> > Currently gimp_env_init() calls g_error (fatal) if called a second time.
> > Instead, pygimp_main(), which calls gimp_env_init(), should check
> > whether this is a repeated call and issue a warning then return an
> > error.  (I'm not sure if any changes are needed for ScriptFu, whether
> > Pygimp should check the return from gimp_main and raise a warning
> > exception, etc.)
> We would definitely have to change gimp_main() then as it must not be
> called more than once. I have a bad feeling about doing this change.
> > Currently, you can't import a plugin from another plugin unless the
> > imported plugin guards the call to main() to prevent it from being
> > called unless this is a top level invocation:
> > 
> > if __name__=='__main__':
> >   main()
> >   
> > It could be sufficient to have a convention for plugins to guard the
> > call to main(), but the convention is not usually followed. Or you could
> > have a convention that any shared code needs to be in a separate module
> > from the top plugin module, but again, that convention is not often
> > followed.
> >   
> > If you could import a plugin from within a Gimp plugin, then you could
> > share more code.  You could use classes etc. from imported plugins.  You
> > could also invoke the imported plugin's top function without invoking it
> > as a registered PDB procedure.  Neither reason is compelling.
> Importing a plug-in from within a GIMP plug-in only makes sense for
> Python. So I would suggest that we seek for a solution that only
> involves changes to the GIMP Python bindings but does not require a
> change to libgimp or libgimpbase. I haven't looked at the code, but it
> should be possible to deal with this in pygimp_main().
> Sven
That seems reasonable.

Here is proposed addition for plug-ins/gimpmodule.c in pygimp_main()
that I have lightly tested.  Note it raises a warning (Python prints
warning on stderr once, on the second call), not an exception. Note it
compiles with a C90 warning about mixing declarations and code.

    if (query == Py_None) {
        PyErr_SetString(pygimp_error, "a query procedure must be
        return NULL;
    /* lkk 2010 begin enhancement*/
    static int was_called_previously = 0;
    if (was_called_previously) {
      PyErr_WarnEx(PyExc_RuntimeWarning, "main() should only be called
once", 1);
      return Py_None;
    else {
      /* OK to set this here since following code either succeeds in
initializing plugin, or fails hard. */ 
      was_called_previously = 1;
    /* lkk 2010 end enhancement*/

Gimp-developer mailing list

Reply via email to