My Project  UNKNOWN_GIT_VERSION
Data Structures | Macros | Functions
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 More...
 
PythonObject get_attrib_name (leftv arg)
 
PythonObject python_eval (const char *arg)
 Evaluate string in python. More...
 
BOOLEAN python_eval (leftv result, leftv arg)
 Evaluate string in python from Singular. More...
 
BOOLEAN python_run (leftv result, leftv arg)
 Execute string in python from Singular. More...
 
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 More...
 
void * pyobject_Init (blackbox *)
 blackbox support - initialization More...
 
char * pyobject_String (blackbox *, void *ptr)
 blackbox support - convert to string representation More...
 
void * pyobject_Copy (blackbox *, void *ptr)
 blackbox support - copy element More...
 
BOOLEAN pyobject_Assign (leftv l, leftv r)
 blackbox support - assign element More...
 
BOOLEAN pyobject_Op1 (int op, leftv res, leftv head)
 blackbox support - unary operations More...
 
BOOLEAN pyobject_Op2 (int op, leftv res, leftv arg1, leftv arg2)
 blackbox support - binary operations More...
 
BOOLEAN pyobject_Op3 (int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
 blackbox support - ternary operations More...
 
BOOLEAN pyobject_OpM (int op, leftv res, leftv args)
 blackbox support - n-ary operations More...
 
void pyobject_destroy (blackbox *, void *ptr)
 blackbox support - destruction More...
 
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);

Definition at line 713 of file pyobject.cc.

Function Documentation

◆ from_module_import_all()

void from_module_import_all ( const char *  module_name)

Definition at line 470 of file pyobject.cc.

471 {
472  char buffer[strlen(module_name) + 20];
473  sprintf (buffer, "from %s import *", module_name);
474  PyRun_SimpleString(buffer);
475 }

◆ get_attrib_name()

PythonObject get_attrib_name ( leftv  arg)

Definition at line 414 of file pyobject.cc.

415 {
416  typedef PythonCastStatic<const char*> result_type;
417  if (arg->Typ() == STRING_CMD)
418  return result_type(arg);
419 
420  return result_type((void*)arg->Name());
421 }

◆ get_current_definition()

PyObject* get_current_definition ( const char *  name)

Definition at line 654 of file pyobject.cc.

654  {
655  idhdl handle = ggetid(name);
656  if (!handle || (IDTYP(handle) != PythonInterpreter::id())) return NULL;
657  PythonCastStatic<PyObject*> value(IDDATA(handle));
658  return value;
659 }

◆ names_from_module()

PythonObject names_from_module ( const char *  module_name)

Definition at line 459 of file pyobject.cc.

460 {
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 *')");
466 
467  return python_eval("[str for str in dir(modules[SINGULAR_MODULE_NAME]) if str[0] != '_']");
468 }

◆ pyobject()

int SI_MOD_INIT() pyobject ( SModulFunctions psModulFunctions)

Definition at line 717 of file pyobject.cc.

718 {
719  int tok = -1;
720  blackbox* bbx = pyobject_blackbox(tok);
721  if (bbx->blackbox_Init != pyobject_Init)
722  {
723  bbx->blackbox_destroy = pyobject_destroy;
724  bbx->blackbox_String = pyobject_String;
725  bbx->blackbox_Init = pyobject_Init;
726  bbx->blackbox_Copy = pyobject_Copy;
727  bbx->blackbox_Assign = pyobject_Assign;
728  bbx->blackbox_Op1 = pyobject_Op1;
729  bbx->blackbox_Op2 = pyobject_Op2;
730  bbx->blackbox_Op3 = pyobject_Op3;
731  bbx->blackbox_OpM = pyobject_OpM;
732  bbx->data = (void*)omAlloc0(newstruct_desc_size());
733 
735 
739  }
740  return MAX_TOK;
741 }

◆ pyobject_Assign()

BOOLEAN pyobject_Assign ( leftv  l,
leftv  r 
)

blackbox support - assign element

Definition at line 514 of file pyobject.cc.

515 {
516  Py_XDECREF(l->Data());
517  PyObject* result = PythonCastDynamic(r);
518  Py_XINCREF(result);
519 
520  if (l->rtyp == IDHDL)
521  IDDATA((idhdl)l->data) = (char *)result;
522  else
523  l->data = (void *)result;
524 
525  return !result;
526 }

◆ pyobject_blackbox()

blackbox* pyobject_blackbox ( int &  tok)

Definition at line 702 of file pyobject.cc.

702  {
703  if(blackboxIsCmd("pyobject", tok) != ROOT_DECL)
704  {
705  tok = setBlackboxStuff((blackbox*)omAlloc0(sizeof(blackbox)),
706  "pyobject");
707  }
708  return getBlackboxStuff(tok);
709 }

◆ pyobject_Copy()

void* pyobject_Copy ( blackbox *  ,
void *  ptr 
)

blackbox support - copy element

Definition at line 507 of file pyobject.cc.

508 {
509  Py_XINCREF(ptr);
510  return ptr;
511 }

◆ pyobject_destroy()

void pyobject_destroy ( blackbox *  ,
void *  ptr 
)

blackbox support - destruction

Definition at line 649 of file pyobject.cc.

650 {
651  Py_XDECREF(ptr);
652 }

◆ pyobject_Init()

void* pyobject_Init ( blackbox *  )

blackbox support - initialization

Definition at line 494 of file pyobject.cc.

495 {
496  Py_INCREF(Py_None);
497  return Py_None;
498 }

◆ pyobject_Op1()

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

blackbox support - unary operations

Definition at line 530 of file pyobject.cc.

531 {
532  switch(op)
533  {
534  case INT_CMD: // built-in return types first
535  {
536  long value = PyInt_AsLong(PythonCastStatic<>(head));
537  if( (value == -1) && PyErr_Occurred() )
538  {
539  WerrorS("'pyobject` cannot be converted to integer");
540  PyErr_Clear();
541  return TRUE;
542  }
543  res->data = (void*) value;
544  res->rtyp = INT_CMD;
545  return FALSE;
546  }
547  case TYPEOF_CMD:
548  res->data = (void*) omStrDup("pyobject");
549  res->rtyp = STRING_CMD;
550  return FALSE;
551  }
552 
553  if (!PythonCastStatic<>(head)(op).assign_to(res))
554  return FALSE;
555 
556  BOOLEAN newstruct_Op1(int, leftv, leftv); // forward declaration
557  return newstruct_Op1(op, res, head);
558 }

◆ 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.

563 {
564  PythonCastStatic<> lhs(arg1);
565 
566  switch(op) // built-in return types and special cases first
567  {
568  case '<': case '>': case EQUAL_EQUAL: case NOTEQUAL: case GE: case LE:
569  {
570  res->data = (void *)(long)(lhs.compare(op, PythonCastDynamic(arg2)));
571  res->rtyp = INT_CMD;
572  return FALSE;
573  }
574  case '.': case COLONCOLON: case ATTRIB_CMD:
575  return lhs.attr(get_attrib_name(arg2)).assign_to(res);
576  }
577 
578  PythonCastDynamic rhs(arg2);
579  if (!lhs(op, rhs).assign_to(res))
580  return FALSE;
581 
582  BOOLEAN newstruct_Op2(int, leftv, leftv, leftv); // forward declaration
583  return newstruct_Op2(op, res, arg1, arg2);
584 
585 }

◆ 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.

589 {
590  PythonCastStatic<> lhs(arg1);
591  PythonCastDynamic rhs1(arg2);
592  PythonCastDynamic rhs2(arg3);
593 
594  if (!lhs(op, rhs1, rhs2).assign_to(res))
595  return FALSE;
596 
597  return blackboxDefaultOp3(op, res, arg1, arg2, arg3);
598 }

◆ pyobject_OpM()

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

blackbox support - n-ary operations

Definition at line 602 of file pyobject.cc.

603 {
604  switch(op) // built-in return types first
605  {
606  case STRING_CMD:
607  {
608  blackbox* a = getBlackboxStuff(args->Typ());
609  res->data = (void *)a->blackbox_String(a, args->Data());
610  res->rtyp = STRING_CMD;
611  return FALSE;
612  }
613 
614  case INTVEC_CMD:
615  PythonObject obj = PythonCastStatic<>(args->Data());
616  unsigned long len = obj.size();
617 
618  intvec* vec = new intvec(len);
619  for(unsigned long idx = 0; idx != len; ++idx)
620  {
621  long value = PyInt_AsLong(obj[idx]);
622  (*vec)[idx] = static_cast<int>(value);
623 
624  if ((value == -1) && PyErr_Occurred())
625  {
626  value = 0;
627  PyErr_Clear();
628  }
629  if (value != long((*vec)[idx]))
630  {
631  delete vec;
632  WerrorS("'pyobject` cannot be converted to intvec");
633  return TRUE;
634  }
635  }
636  res->data = (void *)vec;
637  res->rtyp = op;
638  return FALSE;
639  }
641  if (! PythonCastStatic<>(args)(op, seq_type(args->next)).assign_to(res))
642  return FALSE;
643 
644  BOOLEAN newstruct_OpM(int, leftv, leftv); // forward declaration
645  return newstruct_OpM(op, res, args);
646 }

◆ pyobject_String()

char* pyobject_String ( blackbox *  ,
void *  ptr 
)

blackbox support - convert to string representation

Definition at line 501 of file pyobject.cc.

502 {
503  return PythonCastStatic<>(ptr).repr();
504 }

◆ python_eval() [1/2]

PythonObject python_eval ( const char *  arg)

Evaluate string in python.

Definition at line 424 of file pyobject.cc.

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

◆ 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.

432 {
433  if ( !arg || (arg->Typ() != STRING_CMD) )
434  {
435  WerrorS("expected python_eval('string')");
436  return TRUE;
437  }
438 
439  return python_eval(reinterpret_cast<const char*>(arg->Data())).assign_to(result);
440 }

◆ 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.

479 {
480  if ((value == NULL) || (value->Typ()!= STRING_CMD))
481  {
482  WerrorS("expected python_import('string')");
483  return TRUE;
484  }
485 
486  from_module_import_all(reinterpret_cast<const char*>(value->Data()));
487  sync_contexts();
488 
489  Py_INCREF(Py_None);
490  return PythonCastStatic<>(Py_None).assign_to(result);
491 }

◆ python_run()

BOOLEAN python_run ( leftv  result,
leftv  arg 
)

Execute string in python from Singular.

Definition at line 444 of file pyobject.cc.

445 {
446  if ( !arg || (arg->Typ() != STRING_CMD) )
447  {
448  WerrorS("expected python_run('string')");
449  return TRUE;
450  }
451 
452  PyRun_SimpleString(reinterpret_cast<const char*>(arg->Data()));
453  sync_contexts();
454 
455  Py_INCREF(Py_None);
456  return PythonCastStatic<>(Py_None).assign_to(result);
457 }

◆ sync_contexts()

void sync_contexts ( )

getting stuff from python to Singular namespace

Definition at line 662 of file pyobject.cc.

663 {
664  PyRun_SimpleString("_SINGULAR_NEW = modules['__main__'].__dict__.copy()");
665 
666  PythonObject newElts = python_eval("[(_k, _e) \
667  for (_k, _e) in _SINGULAR_NEW.iteritems() \
668  if _k not in _SINGULAR_IMPORTED or not _SINGULAR_IMPORTED[_k] is _e]");
669 
670  long len = newElts.size();
671  for (long idx = 0; idx < len; ++idx)
672  {
673  long i = 0;
674  char* name = newElts[idx][i].str();
675  if (name && (*name != '\0') && (*name != '_'))
676  {
677  Py_XDECREF(get_current_definition(name));
678  i = 1;
679  newElts[idx][i].import_as(name);
680  }
681 
682  }
683 
684  PythonObject deletedElts =
685  python_eval("list(set(_SINGULAR_IMPORTED.iterkeys()) - \
686  set(_SINGULAR_NEW.iterkeys()))");
687  len = deletedElts.size();
688 
689  for (long idx = 0; idx < len; ++idx)
690  {
691  char* name = deletedElts[idx].str();
692  if (name && (*name != '\0') && (*name != '_'))
693  killid(name, &IDROOT);
694  }
695 
696  PyRun_SimpleString("_SINGULAR_IMPORTED =_SINGULAR_NEW");
697  PyRun_SimpleString("del _SINGULAR_NEW");
698 }
ATTRIB_CMD
@ ATTRIB_CMD
Definition: tok.h:36
PythonObject::size
Py_ssize_t size() const
Definition: pyobject.cc:187
FALSE
#define FALSE
Definition: auxiliary.h:94
get_attrib_name
PythonObject get_attrib_name(leftv arg)
Definition: pyobject.cc:414
sleftv::Data
void * Data()
Definition: subexpr.cc:1182
PythonCastStatic< PythonObject::sequence_tag >
Template specialization for getting handling sequence.
Definition: pyobject.cc:392
pyobject_Op3
BOOLEAN pyobject_Op3(int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
blackbox support - ternary operations
Definition: pyobject.cc:588
newstruct_desc_size
int newstruct_desc_size()
Definition: newstruct.cc:42
COLONCOLON
@ COLONCOLON
Definition: grammar.cc:275
result
return result
Definition: facAbsBiFact.cc:76
STRING_CMD
@ STRING_CMD
Definition: tok.h:183
PythonObject::repr
char * repr() const
Get representative as C-style string.
Definition: pyobject.cc:179
killid
void killid(const char *id, idhdl *ih)
Definition: ipid.cc:359
PythonCastStatic
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:304
MAX_TOK
@ MAX_TOK
Definition: tok.h:216
IDDATA
#define IDDATA(a)
Definition: ipid.h:121
omStrDup
#define omStrDup(s)
Definition: omAllocDecl.h:263
PythonObject::import_as
void import_as(const char *name) const
Definition: pyobject.cc:194
currPack
package currPack
Definition: ipid.cc:59
sleftv
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
python_import
BOOLEAN python_import(leftv result, leftv value)
import python module and export identifiers in Singular namespace
Definition: pyobject.cc:478
TRUE
#define TRUE
Definition: auxiliary.h:98
i
int i
Definition: cfEzgcd.cc:125
pyobject_Op1
BOOLEAN pyobject_Op1(int op, leftv res, leftv head)
blackbox support - unary operations
Definition: pyobject.cc:530
res
CanonicalForm res
Definition: facAbsFact.cc:64
INT_CMD
@ INT_CMD
Definition: tok.h:96
PythonInterpreter::init
static void init(id_type num)
Initialize unique (singleton) python interpreter instance, and set Singular type identifier.
Definition: pyobject.cc:53
sync_contexts
void sync_contexts()
getting stuff from python to Singular namespace
Definition: pyobject.cc:662
blackboxDefaultOp3
BOOLEAN blackboxDefaultOp3(int, leftv, leftv, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
Definition: blackbox.cc:86
BOOLEAN
int BOOLEAN
Definition: auxiliary.h:85
PythonCastDynamic
This class does conversion of Singular objects to python objects on runtime.
Definition: pyobject.cc:340
pyobject_Assign
BOOLEAN pyobject_Assign(leftv l, leftv r)
blackbox support - assign element
Definition: pyobject.cc:514
IDROOT
#define IDROOT
Definition: ipid.h:18
pyobject_Init
void * pyobject_Init(blackbox *)
blackbox support - initialization
Definition: pyobject.cc:494
PYOBJECT_ADD_C_PROC
#define PYOBJECT_ADD_C_PROC(name)
Definition: pyobject.cc:713
newstruct_Op1
BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
Definition: newstruct.cc:245
EQUAL_EQUAL
@ EQUAL_EQUAL
Definition: grammar.cc:268
intvec
Definition: intvec.h:21
LE
@ LE
Definition: grammar.cc:270
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:192
python_eval
PythonObject python_eval(const char *arg)
Evaluate string in python.
Definition: pyobject.cc:424
from_module_import_all
void from_module_import_all(const char *module_name)
Definition: pyobject.cc:470
IDTYP
#define IDTYP(a)
Definition: ipid.h:114
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_String
char * pyobject_String(blackbox *, void *ptr)
blackbox support - convert to string representation
Definition: pyobject.cc:501
INTVEC_CMD
@ INTVEC_CMD
Definition: tok.h:101
idrec
Definition: idrec.h:35
PythonObject::str
char * str() const
Extract C-style string.
Definition: pyobject.cc:185
newstruct_OpM
BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
Definition: newstruct.cc:491
PythonInterpreter::id
static id_type id()
Get Singular type identitfier.
Definition: pyobject.cc:56
name
char name(const Variable &v)
Definition: factory.h:180
IDHDL
#define IDHDL
Definition: tok.h:31
WerrorS
void WerrorS(const char *s)
Definition: feFopen.cc:24
sleftv::Typ
int Typ()
Definition: subexpr.cc:1039
NULL
#define NULL
Definition: omList.c:10
l
int l
Definition: cfEzgcd.cc:93
newstruct_Op2
BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
Definition: newstruct.cc:330
GE
@ GE
Definition: grammar.cc:269
sleftv::Name
const char * Name()
Definition: subexpr.h:120
ggetid
idhdl ggetid(const char *n)
Definition: ipid.cc:523
pyobject_Op2
BOOLEAN pyobject_Op2(int op, leftv res, leftv arg1, leftv arg2)
blackbox support - binary operations
Definition: pyobject.cc:562
NOTEQUAL
@ NOTEQUAL
Definition: grammar.cc:273
head
CanonicalForm head(const CanonicalForm &f)
Definition: canonicalform.h:353
pyobject_blackbox
blackbox * pyobject_blackbox(int &tok)
Definition: pyobject.cc:702
get_current_definition
PyObject * get_current_definition(const char *name)
Definition: pyobject.cc:654
getBlackboxStuff
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:16
PythonObject
This class defines an interface for calling PyObject from Singular.
Definition: pyobject.cc:108
ROOT_DECL
@ ROOT_DECL
Definition: grammar.cc:319
omAlloc0
#define omAlloc0(size)
Definition: omAllocDecl.h:211
PythonObject::assign_to
BOOLEAN assign_to(leftv result)
Definition: pyobject.cc:189
pyobject_Copy
void * pyobject_Copy(blackbox *, void *ptr)
blackbox support - copy element
Definition: pyobject.cc:507
sleftv::next
leftv next
Definition: subexpr.h:86
TYPEOF_CMD
@ TYPEOF_CMD
Definition: tok.h:192
pyobject_OpM
BOOLEAN pyobject_OpM(int op, leftv res, leftv args)
blackbox support - n-ary operations
Definition: pyobject.cc:602
vec
fq_nmod_poly_t * vec
Definition: facHensel.cc:103
setBlackboxStuff
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:126