Loading...
Searching...
No Matches
pyobject.cc File Reference
`#include "kernel/mod2.h"`
`#include "omalloc/omalloc.h"`
`#include "misc/intvec.h"`
`#include "Singular/subexpr.h"`
`#include "Singular/ipid.h"`
`#include "Singular/blackbox.h"`
`#include "Singular/lists.h"`
`#include "Singular/ipshell.h"`
`#include "Singular/newstruct.h"`
`#include "resources/feResource.h"`
`#include "Singular/mod_lib.h"`
`#include <Python.h>`

Go to the source code of this file.

## Data Structures

class | PythonInterpreter
| This class initializes and finalized the python interpreter. More...
class | PythonObject
| This class defines an interface for calling PyObject from Singular. More...
struct | PythonObject::sequence_tag
class | PythonCastStatic< CastType >
| This template class does conversion of Singular objects to python objects on compile-time. More...
class | PythonCastDynamic
| This class does conversion of Singular objects to python objects on runtime. More...
class | PythonCastStatic< PythonObject::sequence_tag >
| Template specialization for getting handling sequence. More...

## Macros

#define | PYOBJECT_ADD_C_PROC(name)

## Functions

void | sync_contexts ()
| getting stuff from python to Singular namespace
PythonObject | get_attrib_name (leftv arg)
PythonObject | python_eval (const char *arg)
| Evaluate string in python.
BOOLEAN | python_eval (leftv result, leftv arg)
| Evaluate string in python from Singular.
BOOLEAN | python_run (leftv result, leftv arg)
| Execute string in python from Singular.
PythonObject | names_from_module (const char *module_name)
void | from_module_import_all (const char *module_name)
BOOLEAN | python_import (leftv result, leftv value)
| import python module and export identifiers in Singular namespace
void * | pyobject_Init (blackbox *)
| blackbox support - initialization
char * | pyobject_String (blackbox *, void *ptr)
| blackbox support - convert to string representation
void * | pyobject_Copy (blackbox *, void *ptr)
| blackbox support - copy element
BOOLEAN | pyobject_Assign (leftv l, leftv r)
| blackbox support - assign element
BOOLEAN | pyobject_Op1 (int op, leftv res, leftv head)
| blackbox support - unary operations
BOOLEAN | pyobject_Op2 (int op, leftv res, leftv arg1, leftv arg2)
| blackbox support - binary operations
BOOLEAN | pyobject_Op3 (int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
| blackbox support - ternary operations
BOOLEAN | pyobject_OpM (int op, leftv res, leftv args)
| blackbox support - n-ary operations
void | pyobject_destroy (blackbox *, void *ptr)
| blackbox support - destruction
PyObject * | get_current_definition (const char *name)
blackbox * | pyobject_blackbox (int &tok)
int SI_MOD_INIT | pyobject (SModulFunctions *psModulFunctions)

## Detailed Description

Author
  Alexander Dreyer

Date
  2010-12-15

This file defines the `blackbox` operations for the pyobject type.

Copyright:
  (c) 2010 by The Singular Team, see LICENSE file

Definition in file pyobject.cc.

## Data Structure Documentation

## PythonObject::sequence_tag

struct PythonObject::sequence_tag

Definition at line 113 of file pyobject.cc.

## Macro Definition Documentation

## PYOBJECT_ADD_C_PROC

#define PYOBJECT_ADD_C_PROC | ( |  | name | ) |

Value:
psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),\
(char*)#name, FALSE, name);
FALSE
#define FALSE
Definition auxiliary.h:97
name
char name(const Variable &v)
Definition factory.h:189
currPack
VAR package currPack
Definition ipid.cc:55

Definition at line 713 of file pyobject.cc.

#define PYOBJECT_ADD_C_PROC(name) \
psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),\
(char*)#name, FALSE, name);

## Function Documentation

## from_module_import_all()

void from_module_import_all | ( | const char * | module_name | ) |

Definition at line 470 of file pyobject.cc.

{
char buffer[strlen(module_name) + 20];
snprintf (buffer,strlen(module_name) + 20, "from %s import *", module_name);
PyRun_SimpleString(buffer);
}

## get_attrib_name()

PythonObject get_attrib_name | ( | leftv | arg | ) |

Definition at line 414 of file pyobject.cc.

{
typedef PythonCastStatic<const char*> result_type;
if (arg->Typ() == STRING_CMD)
return result_type(arg);
return result_type((void*)arg->Name());
}
PythonCastStatic
This template class does conversion of Singular objects to python objects on compile-time.
Definition pyobject.cc:304
sleftv::Typ
int Typ()
Definition subexpr.cc:1048
sleftv::Name
const char * Name()
Definition subexpr.h:120
STRING_CMD
@ STRING_CMD
Definition tok.h:187

## get_current_definition()

PyObject * get_current_definition | ( | const char * | name | ) |

Definition at line 654 of file pyobject.cc.

{
idhdl handle = ggetid(name);
if (!handle || (IDTYP(handle) != PythonInterpreter::id())) return NULL;
PythonCastStatic<PyObject*> value(IDDATA(handle));
return value;
}
PythonInterpreter::id
static id_type id()
Get Singular type identitfier.
Definition pyobject.cc:56
ggetid
idhdl ggetid(const char *n)
Definition ipid.cc:581
IDDATA
#define IDDATA(a)
Definition ipid.h:126
IDTYP
#define IDTYP(a)
Definition ipid.h:119
NULL
#define NULL
Definition omList.c:12
idhdl
idrec * idhdl
Definition ring.h:22

## names_from_module()

PythonObject names_from_module | ( | const char * | module_name | ) |

Definition at line 459 of file pyobject.cc.

{
char buffer[strlen(module_name) + 30];
snprintf (buffer,strlen(module_name) + 30, "SINGULAR_MODULE_NAME = '%s'", module_name);
PyRun_SimpleString(buffer);
PyRun_SimpleString("from sys import modules");
PyRun_SimpleString("exec('from ' + SINGULAR_MODULE_NAME + ' import *')");
return python_eval("[str for str in dir(modules[SINGULAR_MODULE_NAME]) if str[0] != '_']");
}
python_eval
PythonObject python_eval(const char *arg)
Evaluate string in python.
Definition pyobject.cc:424

## pyobject()

int SI_MOD_INIT pyobject | ( | SModulFunctions * | psModulFunctions | ) |

Definition at line 717 of file pyobject.cc.

{
int tok = -1;
blackbox* bbx = pyobject_blackbox(tok);
if (bbx->blackbox_Init != pyobject_Init)
{
bbx->blackbox_destroy = pyobject_destroy;
bbx->blackbox_String = pyobject_String;
bbx->blackbox_Init = pyobject_Init;
bbx->blackbox_Copy = pyobject_Copy;
bbx->blackbox_Assign = pyobject_Assign;
bbx->blackbox_Op1 = pyobject_Op1;
bbx->blackbox_Op2 = pyobject_Op2;
bbx->blackbox_Op3 = pyobject_Op3;
bbx->blackbox_OpM = pyobject_OpM;
bbx->data = (void*)omAlloc0(newstruct_desc_size());
PythonInterpreter::init(tok);
PYOBJECT_ADD_C_PROC(python_import);
PYOBJECT_ADD_C_PROC(python_eval);
PYOBJECT_ADD_C_PROC(python_run);
}
return MAX_TOK;
}
PythonInterpreter::init
static void init(id_type num)
Initialize unique (singleton) python interpreter instance, and set Singular type identifier.
Definition pyobject.cc:53
newstruct_desc_size
int newstruct_desc_size()
Definition newstruct.cc:42
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
python_import
BOOLEAN python_import(leftv result, leftv value)
import python module and export identifiers in Singular namespace
Definition pyobject.cc:478
pyobject_Init
void * pyobject_Init(blackbox *)
blackbox support - initialization
Definition pyobject.cc:494
pyobject_OpM
BOOLEAN pyobject_OpM(int op, leftv res, leftv args)
blackbox support - n-ary operations
Definition pyobject.cc:602
pyobject_Assign
BOOLEAN pyobject_Assign(leftv l, leftv r)
blackbox support - assign element
Definition pyobject.cc:514
PYOBJECT_ADD_C_PROC
#define PYOBJECT_ADD_C_PROC(name)
Definition pyobject.cc:713
python_run
BOOLEAN python_run(leftv result, leftv arg)
Execute string in python from Singular.
Definition pyobject.cc:444
pyobject_destroy
void pyobject_destroy(blackbox *, void *ptr)
blackbox support - destruction
Definition pyobject.cc:649
pyobject_Op1
BOOLEAN pyobject_Op1(int op, leftv res, leftv head)
blackbox support - unary operations
Definition pyobject.cc:530
pyobject_blackbox
blackbox * pyobject_blackbox(int &tok)
Definition pyobject.cc:702
pyobject_Op2
BOOLEAN pyobject_Op2(int op, leftv res, leftv arg1, leftv arg2)
blackbox support - binary operations
Definition pyobject.cc:562
pyobject_String
char * pyobject_String(blackbox *, void *ptr)
blackbox support - convert to string representation
Definition pyobject.cc:501
pyobject_Op3
BOOLEAN pyobject_Op3(int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
blackbox support - ternary operations
Definition pyobject.cc:588
pyobject_Copy
void * pyobject_Copy(blackbox *, void *ptr)
blackbox support - copy element
Definition pyobject.cc:507
MAX_TOK
@ MAX_TOK
Definition tok.h:220

## pyobject_Assign()

BOOLEAN pyobject_Assign | ( | leftv | l,
|  | leftv | r )

blackbox support - assign element

Definition at line 514 of file pyobject.cc.

{
Py_XDECREF(l->Data());
PyObject* result = PythonCastDynamic(r);
Py_XINCREF(result);
if (l->rtyp == IDHDL)
IDDATA((idhdl)l->data) = (char *)result;
else
l->data = (void *)result;
return !result;
}
l
int l
Definition cfEzgcd.cc:100
PythonCastDynamic
This class does conversion of Singular objects to python objects on runtime.
Definition pyobject.cc:340
result
return result
Definition facAbsBiFact.cc:76
IDHDL
#define IDHDL
Definition tok.h:31

## pyobject_blackbox()

blackbox * pyobject_blackbox | ( | int & | tok | ) |

Definition at line 702 of file pyobject.cc.

{
if(blackboxIsCmd("pyobject", tok) != ROOT_DECL)
{
tok = setBlackboxStuff((blackbox*)omAlloc0(sizeof(blackbox)),
"pyobject");
}
return getBlackboxStuff(tok);
}
setBlackboxStuff
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition blackbox.cc:143
getBlackboxStuff
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition blackbox.cc:17
blackboxIsCmd
int blackboxIsCmd(const char *n, int &tok)
used by scanner: returns ROOT_DECL for known types (and the type number in tok)
Definition blackbox.cc:219
ROOT_DECL
@ ROOT_DECL
Definition grammar.cc:320

## pyobject_Copy()

void * pyobject_Copy | ( | blackbox * | ,
|  | void * | ptr )

blackbox support - copy element

Definition at line 507 of file pyobject.cc.

{
Py_XINCREF(ptr);
return ptr;
}

## pyobject_destroy()

void pyobject_destroy | ( | blackbox * | ,
|  | void * | ptr )

blackbox support - destruction

Definition at line 649 of file pyobject.cc.

{
Py_XDECREF(ptr);
}

## pyobject_Init()

void * pyobject_Init | ( | blackbox * |  | ) |

blackbox support - initialization

Definition at line 494 of file pyobject.cc.

{
Py_INCREF(Py_None);
return Py_None;
}

## pyobject_Op1()

BOOLEAN pyobject_Op1 | ( | int | op,
|  | leftv | res,
|  | leftv | head )

blackbox support - unary operations

Definition at line 530 of file pyobject.cc.

{
switch(op)
{
case INT_CMD: // built-in return types first
{
long value = PyInt_AsLong(PythonCastStatic<>(head));
if( (value == -1) && PyErr_Occurred() )
{
WerrorS("'pyobject` cannot be converted to integer");
PyErr_Clear();
return TRUE;
}
res->data = (void*) value;
res->rtyp = INT_CMD;
return FALSE;
}
case TYPEOF_CMD:
res->data = (void*) omStrDup("pyobject");
res->rtyp = STRING_CMD;
return FALSE;
}
if (!PythonCastStatic<>(head)(op).assign_to(res))
return FALSE;
BOOLEAN newstruct_Op1(int, leftv, leftv); // forward declaration
return newstruct_Op1(op, res, head);
}
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
head
CanonicalForm head(const CanonicalForm &f)
Definition canonicalform.h:360
res
CanonicalForm res
Definition facAbsFact.cc:60
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
newstruct_Op1
BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
Definition newstruct.cc:259
omStrDup
#define omStrDup(s)
Definition omAllocDecl.h:263
leftv
sleftv * leftv
Definition structs.h:53
TYPEOF_CMD
@ TYPEOF_CMD
Definition tok.h:196
INT_CMD
@ INT_CMD
Definition tok.h:96

## pyobject_Op2()

BOOLEAN pyobject_Op2 | ( | int | op,
|  | leftv | res,
|  | leftv | arg1,
|  | leftv | arg2 )

blackbox support - binary operations

Definition at line 562 of file pyobject.cc.

{
PythonCastStatic<> lhs(arg1);
switch(op) // built-in return types and special cases first
{
case '<': case '>': case EQUAL_EQUAL: case NOTEQUAL: case GE: case LE:
{
res->data = (void *)(long)(lhs.compare(op, PythonCastDynamic(arg2)));
res->rtyp = INT_CMD;
return FALSE;
}
case '.': case COLONCOLON: case ATTRIB_CMD:
return lhs.attr(get_attrib_name(arg2)).assign_to(res);
}
PythonCastDynamic rhs(arg2);
if (!lhs(op, rhs).assign_to(res))
return FALSE;
BOOLEAN newstruct_Op2(int, leftv, leftv, leftv); // forward declaration
return newstruct_Op2(op, res, arg1, arg2);
}
GE
@ GE
Definition grammar.cc:269
EQUAL_EQUAL
@ EQUAL_EQUAL
Definition grammar.cc:268
LE
@ LE
Definition grammar.cc:270
NOTEQUAL
@ NOTEQUAL
Definition grammar.cc:273
COLONCOLON
@ COLONCOLON
Definition grammar.cc:275
newstruct_Op2
BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
Definition newstruct.cc:344
get_attrib_name
PythonObject get_attrib_name(leftv arg)
Definition pyobject.cc:414
ATTRIB_CMD
@ ATTRIB_CMD
Definition tok.h:36

## pyobject_Op3()

BOOLEAN pyobject_Op3 | ( | int | op,
|  | leftv | res,
|  | leftv | arg1,
|  | leftv | arg2,
|  | leftv | arg3 )

blackbox support - ternary operations

Definition at line 588 of file pyobject.cc.

{
PythonCastStatic<> lhs(arg1);
PythonCastDynamic rhs1(arg2);
PythonCastDynamic rhs2(arg3);
if (!lhs(op, rhs1, rhs2).assign_to(res))
return FALSE;
return blackboxDefaultOp3(op, res, arg1, arg2, arg3);
}
blackboxDefaultOp3
BOOLEAN blackboxDefaultOp3(int, leftv, leftv, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
Definition blackbox.cc:102

## pyobject_OpM()

BOOLEAN pyobject_OpM | ( | int | op,
|  | leftv | res,
|  | leftv | args )

blackbox support - n-ary operations

Definition at line 602 of file pyobject.cc.

{
switch(op) // built-in return types first
{
case STRING_CMD:
{
blackbox* a = getBlackboxStuff(args->Typ());
res->data = (void *)a->blackbox_String(a, args->Data());
res->rtyp = STRING_CMD;
return FALSE;
}
case INTVEC_CMD:
PythonObject obj = PythonCastStatic<>(args->Data());
unsigned long len = obj.size();
intvec* vec = new intvec(len);
for(unsigned long idx = 0; idx != len; ++idx)
{
long value = PyInt_AsLong(obj[idx]);
(*vec)[idx] = static_cast<int>(value);
if ((value == -1) && PyErr_Occurred())
{
value = 0;
PyErr_Clear();
}
if (value != long((*vec)[idx]))
{
delete vec;
WerrorS("'pyobject` cannot be converted to intvec");
return TRUE;
}
}
res->data = (void *)vec;
res->rtyp = op;
return FALSE;
}
typedef PythonCastStatic<PythonObject::sequence_tag> seq_type;
if (! PythonCastStatic<>(args)(op, seq_type(args->next)).assign_to(res))
return FALSE;
BOOLEAN newstruct_OpM(int, leftv, leftv); // forward declaration
return newstruct_OpM(op, res, args);
}
PythonObject
This class defines an interface for calling PyObject from Singular.
Definition pyobject.cc:108
PythonObject::size
Py_ssize_t size() const
Definition pyobject.cc:187
PythonObject::assign_to
BOOLEAN assign_to(leftv result)
Definition pyobject.cc:189
intvec
Definition intvec.h:24
sleftv::Data
void * Data()
Definition subexpr.cc:1192
sleftv::next
leftv next
Definition subexpr.h:86
vec
fq_nmod_poly_t * vec
Definition facHensel.cc:108
newstruct_OpM
BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
Definition newstruct.cc:486
INTVEC_CMD
@ INTVEC_CMD
Definition tok.h:101

## pyobject_String()

char * pyobject_String | ( | blackbox * | ,
|  | void * | ptr )

blackbox support - convert to string representation

Definition at line 501 of file pyobject.cc.

{
return PythonCastStatic<>(ptr).repr();
}
PythonObject::repr
char * repr() const
Get representative as C-style string.
Definition pyobject.cc:179

## python_eval() [1/2]

PythonObject python_eval | ( | const char * | arg | ) |

Evaluate string in python.

Definition at line 424 of file pyobject.cc.

{
PyObject* globals = PyModule_GetDict(PyImport_Import(PyString_FromString("__main__")));
return PyRun_String(arg, Py_eval_input, globals, globals);
}

## python_eval() [2/2]

BOOLEAN python_eval | ( | leftv | result,
|  | leftv | arg )

Evaluate string in python from Singular.

Definition at line 431 of file pyobject.cc.

{
if ( !arg || (arg->Typ() != STRING_CMD) )
{
WerrorS("expected python_eval('string')");
return TRUE;
}
return python_eval(reinterpret_cast<const char*>(arg->Data())).assign_to(result);
}

## python_import()

BOOLEAN python_import | ( | leftv | result,
|  | leftv | value )

import python module and export identifiers in Singular namespace

Definition at line 478 of file pyobject.cc.

{
if ((value == NULL) || (value->Typ()!= STRING_CMD))
{
WerrorS("expected python_import('string')");
return TRUE;
}
from_module_import_all(reinterpret_cast<const char*>(value->Data()));
sync_contexts();
Py_INCREF(Py_None);
return PythonCastStatic<>(Py_None).assign_to(result);
}
sync_contexts
void sync_contexts()
getting stuff from python to Singular namespace
Definition pyobject.cc:662
from_module_import_all
void from_module_import_all(const char *module_name)
Definition pyobject.cc:470

## python_run()

BOOLEAN python_run | ( | leftv | result,
|  | leftv | arg )

Execute string in python from Singular.

Definition at line 444 of file pyobject.cc.

{
if ( !arg || (arg->Typ() != STRING_CMD) )
{
WerrorS("expected python_run('string')");
return TRUE;
}
PyRun_SimpleString(reinterpret_cast<const char*>(arg->Data()));
sync_contexts();
Py_INCREF(Py_None);
return PythonCastStatic<>(Py_None).assign_to(result);
}

## sync_contexts()

void sync_contexts | ( |  | ) |

getting stuff from python to Singular namespace

Definition at line 662 of file pyobject.cc.

{
PyRun_SimpleString("_SINGULAR_NEW = modules['__main__'].__dict__.copy()");
PythonObject newElts = python_eval("[(_k, _e) \
for (_k, _e) in _SINGULAR_NEW.iteritems() \
if _k not in _SINGULAR_IMPORTED or not _SINGULAR_IMPORTED[_k] is _e]");
long len = newElts.size();
for (long idx = 0; idx < len; ++idx)
{
long i = 0;
char* name = newElts[idx][i].str();
if (name && (*name != '\0') && (*name != '_'))
{
Py_XDECREF(get_current_definition(name));
i = 1;
newElts[idx][i].import_as(name);
}
}
PythonObject deletedElts =
python_eval("list(set(_SINGULAR_IMPORTED.iterkeys()) - \
set(_SINGULAR_NEW.iterkeys()))");
len = deletedElts.size();
for (long idx = 0; idx < len; ++idx)
{
char* name = deletedElts[idx].str();
if (name && (*name != '\0') && (*name != '_'))
killid(name, &IDROOT);
}
PyRun_SimpleString("_SINGULAR_IMPORTED =_SINGULAR_NEW");
PyRun_SimpleString("del _SINGULAR_NEW");
}
i
int i
Definition cfEzgcd.cc:132
PythonObject::import_as
void import_as(const char *name) const
Definition pyobject.cc:194
PythonObject::str
char * str() const
Extract C-style string.
Definition pyobject.cc:185
killid
void killid(const char *id, idhdl *ih)
Definition ipid.cc:387
IDROOT
#define IDROOT
Definition ipid.h:19
get_current_definition
PyObject * get_current_definition(const char *name)
Definition pyobject.cc:654