68 return init_interpreter;
86 char* argv = (
char*)
"";
87 PySys_SetArgv(1, &argv);
88 PyRun_SimpleString(
"from sys import path, modules");
89 PyRun_SimpleString(
"_SINGULAR_IMPORTED = dict()");
93 PyRun_SimpleString(cmd);
94 PyRun_SimpleString(
"del path");
145 case '+':
return PyNumber_Add(*
this, arg);
146 case '-':
return PyNumber_Subtract(*
this, arg);
147 case '*':
return PyNumber_Multiply(*
this, arg);
148 case '/':
return PyNumber_Divide(*
this, arg);
149 case '^':
return PyNumber_Power(*
this, arg, Py_None);
150 case '(':
return check_context(PyObject_CallObject(*
this, arg));
181 return omStrDup(PyString_AsString(PyObject_Repr(*
this)));
187 Py_ssize_t
size()
const {
return PyObject_Size(
m_ptr); }
204 else {
WerrorS(
"Importing pyobject to Singular failed"); }
208 {
return PyObject_RichCompareBool(*
this, arg,
py_opid(op)); }
211 self attr(
const self& arg)
const {
return PyObject_GetAttr(*
this, arg); }
215 if (!PyObject_HasAttr(*
this, arg))
216 Werror(
"Cannot delete attribute %s.", arg.
repr());
218 PyObject_DelAttr(*
this, arg);
226 self pylist(PyList_New(0));
227 PyList_Append(pylist, *
this);
228 if(PyTuple_Check(args)) pylist.
append_iter(PyObject_GetIter(args));
229 else PyList_Append(pylist, args);
236 if(!PyErr_Occurred())
return FALSE;
238 PyObject *pType, *pMessage, *pTraceback;
239 PyErr_Fetch(&pType, &pMessage, &pTraceback);
241 WerrorS(
"pyobject error occurred");
242 WerrorS(PyString_AsString(pMessage));
245 Py_XDECREF(pMessage);
246 Py_XDECREF(pTraceback);
254 while ((item = PyIter_Next(iterator))) {
255 PyList_Append(*
this, item);
263 case '<':
return Py_LT;
264 case '>':
return Py_GT;
267 case GE:
return Py_GE;
268 case LE:
return Py_LE;
302template <
class CastType = PythonObject::ptr_type>
318 ptr_type get(
const char* value) {
return PyString_FromString(value); }
324template <
class CastType>
329 for (
int idx = 0; idx < value->
length(); ++idx)
330 PyList_Append(pylist, self::get((*value)[idx]));
372 Werror(
"type '%s` incompatible with 'pyobject`",
iiTwoOps(typeId));
377template <
class CastType>
382 for (
int i = 0;
i <= value->
nr; ++
i)
402 if (
iter) {
do { ++distance; }
while((
iter =
iter->next)); };
418 return result_type(arg);
420 return result_type((
void*)arg->
Name());
426 PyObject* globals = PyModule_GetDict(PyImport_Import(PyString_FromString(
"__main__")));
427 return PyRun_String(arg, Py_eval_input, globals, globals);
435 WerrorS(
"expected python_eval('string')");
448 WerrorS(
"expected python_run('string')");
452 PyRun_SimpleString(
reinterpret_cast<const char*
>(arg->
Data()));
461 char buffer[strlen(module_name) + 30];
462 sprintf (buffer,
"SINGULAR_MODULE_NAME = '%s'", module_name);
463 PyRun_SimpleString(buffer);
464 PyRun_SimpleString(
"from sys import modules");
465 PyRun_SimpleString(
"exec('from ' + SINGULAR_MODULE_NAME + ' import *')");
467 return python_eval(
"[str for str in dir(modules[SINGULAR_MODULE_NAME]) if str[0] != '_']");
472 char buffer[strlen(module_name) + 20];
473 sprintf (buffer,
"from %s import *", module_name);
474 PyRun_SimpleString(buffer);
482 WerrorS(
"expected python_import('string')");
516 Py_XDECREF(
l->Data());
537 if( (value == -1) && PyErr_Occurred() )
539 WerrorS(
"'pyobject` cannot be converted to integer");
543 res->data = (
void*) value;
579 if (!lhs(op, rhs).assign_to(
res))
594 if (!lhs(op, rhs1, rhs2).assign_to(
res))
609 res->data = (
void *)a->blackbox_String(a, args->
Data());
616 unsigned long len = obj.
size();
619 for(
unsigned long idx = 0; idx != len; ++idx)
621 long value = PyInt_AsLong(obj[idx]);
622 (*vec)[idx] =
static_cast<int>(value);
624 if ((value == -1) && PyErr_Occurred())
629 if (value !=
long((*
vec)[idx]))
632 WerrorS(
"'pyobject` cannot be converted to intvec");
664 PyRun_SimpleString(
"_SINGULAR_NEW = modules['__main__'].__dict__.copy()");
667 for (_k, _e) in _SINGULAR_NEW.iteritems() \
668 if _k not in _SINGULAR_IMPORTED or not _SINGULAR_IMPORTED[_k] is _e]");
670 long len = newElts.
size();
671 for (
long idx = 0; idx < len; ++idx)
685 python_eval(
"list(set(_SINGULAR_IMPORTED.iterkeys()) - \
686 set(_SINGULAR_NEW.iterkeys()))");
687 len = deletedElts.
size();
689 for (
long idx = 0; idx < len; ++idx)
691 char*
name = deletedElts[idx].
str();
696 PyRun_SimpleString(
"_SINGULAR_IMPORTED =_SINGULAR_NEW");
697 PyRun_SimpleString(
"del _SINGULAR_NEW");
713#define PYOBJECT_ADD_C_PROC(name) \
714 psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),\
715 (char*)#name, FALSE, name);
742#undef PYOBJECT_ADD_C_PROC
746 PrintS(
"no pyobject support\n");
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
BOOLEAN blackboxDefaultOp3(int, leftv, leftv, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
int blackboxIsCmd(const char *n, int &tok)
used by scanner: returns ROOT_DECL for known types (and the type number in tok)
This class does conversion of Singular objects to python objects on runtime.
PythonObject get(leftv value, int typeId)
PythonCastDynamic(leftv value)
void append_to(leftv iter) const
size_t size(leftv iter, size_t distance=0) const
PythonCastStatic(leftv value)
This template class does conversion of Singular objects to python objects on compile-time.
ptr_type get(char *value)
ptr_type get(const char *value)
PythonCastStatic(leftv value)
PythonCastStatic(void *value)
ptr_type get(ptr_type value)
This class initializes and finalized the python interpreter.
void set_python_defaults()
PythonInterpreter()
Singleton: Only init() is allowed to construct an instance.
static id_type id()
Get Singular type identitfier.
static void init(id_type num)
Initialize unique (singleton) python interpreter instance, and set Singular type identifier.
static PythonInterpreter & instance()
Static initialization - safely takes care of destruction on program termination.
This class defines an interface for calling PyObject from Singular.
self attr(const self &arg) const
int py_opid(int op) const
void import_as(const char *name) const
ptr_type check_context(ptr_type ptr) const
BOOLEAN none_to(leftv result) const
self args2list(const self &args) const
self operator()(int op) const
Unary operations.
void append_iter(self iterator)
self operator[](long idx) const
char * str() const
Extract C-style string.
PythonObject(ptr_type ptr)
BOOLEAN python_to(leftv result) const
self operator()(int op, const self &arg1, const self &arg2) const
Ternary operations.
self del_attr(const self &arg) const
self operator()(int op, const self &arg) const
Binary and n-ary operations.
char * repr() const
Get representative as C-style string.
self operator[](const self &idx) const
Get item.
BOOLEAN assign_to(leftv result)
BOOLEAN handle_exception() const
ptr_type m_ptr
The actual pointer.
int compare(int op, const self &arg) const
Class used for (list of) interpreter objects.
void WerrorS(const char *s)
char * feGetResource(const char id, int warn)
const char * iiTwoOps(int t)
void killid(const char *id, idhdl *ih)
idhdl ggetid(const char *n)
idhdl enterid(const char *s, int lev, int t, idhdl *root, BOOLEAN init, BOOLEAN search)
BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
BOOLEAN newstruct_Assign_user(int op, leftv l, leftv r)
BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
int newstruct_desc_size()
BOOLEAN python_import(leftv result, leftv value)
import python module and export identifiers in Singular namespace
void * pyobject_Init(blackbox *)
blackbox support - initialization
BOOLEAN pyobject_OpM(int op, leftv res, leftv args)
blackbox support - n-ary operations
BOOLEAN pyobject_Assign(leftv l, leftv r)
blackbox support - assign element
PythonObject get_attrib_name(leftv arg)
#define PYOBJECT_ADD_C_PROC(name)
int SI_MOD_INIT() pyobject(SModulFunctions *psModulFunctions)
PyObject * get_current_definition(const char *name)
BOOLEAN python_run(leftv result, leftv arg)
Execute string in python from Singular.
PythonObject python_eval(const char *arg)
Evaluate string in python.
PythonObject names_from_module(const char *module_name)
void pyobject_destroy(blackbox *, void *ptr)
blackbox support - destruction
BOOLEAN pyobject_Op1(int op, leftv res, leftv head)
blackbox support - unary operations
void sync_contexts()
getting stuff from python to Singular namespace
void from_module_import_all(const char *module_name)
blackbox * pyobject_blackbox(int &tok)
BOOLEAN pyobject_Op2(int op, leftv res, leftv arg1, leftv arg2)
blackbox support - binary operations
char * pyobject_String(blackbox *, void *ptr)
blackbox support - convert to string representation
BOOLEAN pyobject_Op3(int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
blackbox support - ternary operations
void * pyobject_Copy(blackbox *, void *ptr)
blackbox support - copy element
void PrintS(const char *s)
void Werror(const char *fmt,...)
int name
New type name for int.