Skip to content

Commit

Permalink
Introducing system("pyobject") to ensure pyobject functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderdreyer committed Dec 11, 2012
1 parent 7b9d49e commit e87d402
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Singular/extra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
#include "silink.h"
#include "walk.h"
#include <Singular/newstruct.h>

#include <Singular/pyobject_setup.h>


#ifdef HAVE_RINGS
Expand Down Expand Up @@ -2946,6 +2946,13 @@ static BOOLEAN jjEXTENDED_SYSTEM(leftv res, leftv h)
}
else

/*==== pyobject (or pyobject*, like pyobject.so) force loading python ===*/
if (strncmp(sys_cmd, "pyobject", 8) == 0)
{
res->rtyp = NONE;
return pyobject_ensure();
}
else

/*==================== DLL =================*/
#ifdef ix86_Win
Expand Down
16 changes: 14 additions & 2 deletions Singular/pyobject_setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ static BOOLEAN pyobject_load()
/// blackbox support - initialization via autoloading
void* pyobject_autoload(blackbox* bbx)
{
if (pyobject_load()) return NULL;
return bbx->blackbox_Init(bbx);
assume(bbx != NULL);
return (pyobject_load()? NULL: bbx->blackbox_Init(bbx));
}

void pyobject_default_destroy(blackbox *b, void *d)
Expand All @@ -66,3 +66,15 @@ void pyobject_setup()
setBlackboxStuff(bbx, "pyobject");
}

/// Explicitely load, if not loaded already
BOOLEAN pyobject_ensure() {

int tok = -1;
blackbox* bbx = (blackboxIsCmd("pyobject", tok) == ROOT_DECL?
getBlackboxStuff(tok): (blackbox*)NULL);
if (bbx == NULL) return TRUE;
return (bbx->blackbox_Init == pyobject_autoload? pyobject_load(): FALSE);
}



4 changes: 4 additions & 0 deletions Singular/pyobject_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#ifndef SINGULAR_PYOBJECT_SETUP_H
#define SINGULAR_PYOBJECT_SETUP_H

/// initialize blackbox support for @c pyobject; functionilty os autoloaded on demand
void pyobject_setup();

/// force leading of pyobject functionality
BOOLEAN pyobject_ensure();

#endif
9 changes: 9 additions & 0 deletions doc/pyobject.doc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Together with some basic operations and funtions, pyobject instances
access @code{python} functionality from within @sc{Singular} and store
the results for re-use:

Note that this feature is automatically loaded on demand when initializing
an object of type @code{pyobject}. For accessing @code{pyobject}-related
functions before using any @code{python} object, please type
@code{system("pyobject");} at the @sc{Singular} prompt.

@smallexample
@c example error
pyobject pystr = "Hello";
Expand Down Expand Up @@ -228,6 +233,7 @@ deletes an attribute from a pyobject (see @ref{killattrib})
@*@strong{Example:}
@smallexample
@c example
system("pyobject");
python_run("def new_pyobj(): pass");
attrib(new_pyobj, "new_attr", "something");
attrib(new_pyobj, "new_attr");
Expand Down Expand Up @@ -271,6 +277,7 @@ as pyobject.
@item @strong{Example:}
@smallexample
@c example
system("pyobject");
python_eval("17 + 4");
typeof(_);
list ll = python_eval("range(10)");
Expand All @@ -292,6 +299,7 @@ Imports python module (given as a string) in the @sc{Singular} context.
@item @strong{Example:}
@smallexample
@c example
system("pyobject");
python_import("os");
name; // e. g. 'posix'
sep; // pathname separator
Expand All @@ -315,6 +323,7 @@ and syncs the contexts afterwards.
@item @strong{Example:}
@smallexample
@c example
system("pyobject");
python_run("def newfunc(*args): return list(args)");
newfunc(1, 2, 3); // newfunc also known to SINGULAR now
Expand Down

0 comments on commit e87d402

Please sign in to comment.