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));
160 self operator()(
int op,
const self& arg1,
const self& arg2)
const 172 self operator[](
const self& idx)
const {
return PyObject_GetItem(*
this, idx); }
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;
302 template <
class CastType = PythonObject::ptr_type>
316 ptr_type get(
long value) {
return PyInt_FromLong(value); }
317 ptr_type get(
int value) {
return PyInt_FromLong((
long)value); }
318 ptr_type get(
const char* value) {
return PyString_FromString(value); }
319 ptr_type get(
char* value) {
return get(
const_cast<const char*
>(value)); }
324 template <
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));
377 template <
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 BOOLEAN python_to(leftv result) const
int py_opid(int op) const
idhdl ggetid(const char *n)
int SI_MOD_INIT() pyobject(SModulFunctions *psModulFunctions)
Class used for (list of) interpreter objects.
BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
void set_python_defaults()
PythonInterpreter()
Singleton: Only init() is allowed to construct an instance.
BOOLEAN pyobject_Op3(int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
blackbox support - ternary operations
self operator[](const self &idx) const
Get item.
void killid(const char *id, idhdl *ih)
PythonObject python_eval(const char *arg)
Evaluate string in python.
BOOLEAN newstruct_Assign_user(int op, leftv l, leftv r)
PythonObject get(leftv value, int typeId)
BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
BOOLEAN handle_exception() const
PythonCastDynamic(leftv value)
BOOLEAN pyobject_Op1(int op, leftv res, leftv head)
blackbox support - unary operations
static void init(id_type num)
Initialize unique (singleton) python interpreter instance, and set Singular type identifier.
void WerrorS(const char *s)
blackbox * pyobject_blackbox(int &tok)
PyObject * get_current_definition(const char *name)
char * repr() const
Get representative as C-style string.
void * pyobject_Init(blackbox *)
blackbox support - initialization
This class does conversion of Singular objects to python objects on runtime.
BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
size_t size(leftv iter, size_t distance=0) const
self operator()(int op, const self &arg1, const self &arg2) const
Ternary operations.
PythonObject names_from_module(const char *module_name)
BOOLEAN pyobject_OpM(int op, leftv res, leftv args)
blackbox support - n-ary operations
BOOLEAN python_import(leftv result, leftv value)
import python module and export identifiers in Singular namespace
char * pyobject_String(blackbox *, void *ptr)
blackbox support - convert to string representation
int compare(int op, const self &arg) const
idhdl enterid(const char *s, int lev, int t, idhdl *root, BOOLEAN init, BOOLEAN search)
int newstruct_desc_size()
static PythonInterpreter & instance()
Static initialization - safely takes care of destruction on program termination.
PythonCastStatic(leftv value)
void sync_contexts()
getting stuff from python to Singular namespace
BOOLEAN pyobject_Assign(leftv l, leftv r)
blackbox support - assign element
const char * iiTwoOps(int t)
self operator[](long idx) const
PythonObject(ptr_type ptr)
BOOLEAN blackboxDefaultOp3(int, leftv, leftv, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
This class initializes and finalized the python interpreter.
Template specialization for getting handling sequence.
char * feGetResource(const char id, int warn)
void append_iter(self iterator)
ptr_type check_context(ptr_type ptr) const
void append_to(leftv iter) const
ptr_type m_ptr
The actual pointer.
BOOLEAN assign_to(leftv result)
self args2list(const self &args) const
self del_attr(const self &arg) const
static id_type id()
Get Singular type identitfier.
char name(const Variable &v)
This template class does conversion of Singular objects to python objects on compile-time.
PythonObject get_attrib_name(leftv arg)
#define PYOBJECT_ADD_C_PROC(name)
void pyobject_destroy(blackbox *, void *ptr)
blackbox support - destruction
PythonCastStatic(leftv value)
BOOLEAN pyobject_Op2(int op, leftv res, leftv arg1, leftv arg2)
blackbox support - binary operations
void * pyobject_Copy(blackbox *, void *ptr)
blackbox support - copy element
self attr(const self &arg) const
void from_module_import_all(const char *module_name)
int blackboxIsCmd(const char *n, int &tok)
used by scanner: returns ROOT_DECL for known types (and the type number in tok)
void import_as(const char *name) const
BOOLEAN python_run(leftv result, leftv arg)
Execute string in python from Singular.
PythonCastStatic(void *value)
This class defines an interface for calling PyObject from Singular.
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
self operator()(int op, const self &arg) const
Binary and n-ary operations.
self operator()(int op) const
Unary operations.
void Werror(const char *fmt,...)
char * str() const
Extract C-style string.
BOOLEAN none_to(leftv result) const
ptr_type get(ptr_type value)
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t