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 *b, void *ptr)
 blackbox support - convert to string representation More...
 
void * pyobject_Copy (blackbox *b, 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 *b, 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

struct PythonObject::sequence_tag

Definition at line 113 of file pyobject.cc.

Macro Definition Documentation

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

Definition at line 705 of file pyobject.cc.

Function Documentation

void from_module_import_all ( const char *  module_name)

Definition at line 467 of file pyobject.cc.

468 {
469  char buffer[strlen(module_name) + 20];
470  sprintf (buffer, "from %s import *", module_name);
471  PyRun_SimpleString(buffer);
472 }
char buffer[1024]
Definition: run.c:54
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 }
int Typ()
Definition: subexpr.cc:976
const char * Name()
Definition: subexpr.h:121
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
PyObject* get_current_definition ( const char *  name)

Definition at line 646 of file pyobject.cc.

646  {
647  idhdl handle = ggetid(name);
648  if (!handle || (IDTYP(handle) != PythonInterpreter::id())) return NULL;
649  PythonCastStatic<PyObject*> value(IDDATA(handle));
650  return value;
651 }
Definition: idrec.h:34
#define IDTYP(a)
Definition: ipid.h:118
static id_type id()
Get Singular type identitfier.
Definition: pyobject.cc:56
char name(const Variable &v)
Definition: factory.h:178
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
#define NULL
Definition: omList.c:10
#define IDDATA(a)
Definition: ipid.h:125
idhdl ggetid(const char *n, BOOLEAN, idhdl *packhdl)
Definition: ipid.cc:490
PythonObject names_from_module ( const char *  module_name)

Definition at line 456 of file pyobject.cc.

457 {
458  char buffer[strlen(module_name) + 30];
459  sprintf (buffer, "SINGULAR_MODULE_NAME = '%s'", module_name);
460  PyRun_SimpleString(buffer);
461  PyRun_SimpleString("from sys import modules");
462  PyRun_SimpleString("exec('from ' + SINGULAR_MODULE_NAME + ' import *')");
463 
464  return python_eval("[str for str in dir(modules[SINGULAR_MODULE_NAME]) if str[0] != '_']");
465 }
PythonObject python_eval(const char *arg)
Evaluate string in python.
Definition: pyobject.cc:424
char buffer[1024]
Definition: run.c:54
int SI_MOD_INIT() pyobject ( SModulFunctions psModulFunctions)

Definition at line 709 of file pyobject.cc.

710 {
711  int tok = -1;
712  blackbox* bbx = pyobject_blackbox(tok);
713  if (bbx->blackbox_Init != pyobject_Init)
714  {
715  bbx->blackbox_destroy = pyobject_destroy;
716  bbx->blackbox_String = pyobject_String;
717  bbx->blackbox_Init = pyobject_Init;
718  bbx->blackbox_Copy = pyobject_Copy;
719  bbx->blackbox_Assign = pyobject_Assign;
720  bbx->blackbox_Op1 = pyobject_Op1;
721  bbx->blackbox_Op2 = pyobject_Op2;
722  bbx->blackbox_Op3 = pyobject_Op3;
723  bbx->blackbox_OpM = pyobject_OpM;
724  bbx->data = (void*)omAlloc0(newstruct_desc_size());
725 
727 
731  }
732  return MAX_TOK;
733 }
BOOLEAN pyobject_Op3(int op, leftv res, leftv arg1, leftv arg2, leftv arg3)
blackbox support - ternary operations
Definition: pyobject.cc:583
PythonObject python_eval(const char *arg)
Evaluate string in python.
Definition: pyobject.cc:424
Definition: tok.h:217
BOOLEAN pyobject_Op1(int op, leftv res, leftv head)
blackbox support - unary operations
Definition: pyobject.cc:526
void pyobject_destroy(blackbox *b, void *ptr)
blackbox support - destruction
Definition: pyobject.cc:641
static void init(id_type num)
Initialize unique (singleton) python interpreter instance, and set Singular type identifier.
Definition: pyobject.cc:53
blackbox * pyobject_blackbox(int &tok)
Definition: pyobject.cc:694
void * pyobject_Init(blackbox *)
blackbox support - initialization
Definition: pyobject.cc:490
BOOLEAN pyobject_OpM(int op, leftv res, leftv args)
blackbox support - n-ary operations
Definition: pyobject.cc:597
BOOLEAN python_import(leftv result, leftv value)
import python module and export identifiers in Singular namespace
Definition: pyobject.cc:475
char * pyobject_String(blackbox *b, void *ptr)
blackbox support - convert to string representation
Definition: pyobject.cc:497
int newstruct_desc_size()
Definition: newstruct.cc:41
BOOLEAN pyobject_Assign(leftv l, leftv r)
blackbox support - assign element
Definition: pyobject.cc:510
#define PYOBJECT_ADD_C_PROC(name)
Definition: pyobject.cc:705
BOOLEAN pyobject_Op2(int op, leftv res, leftv arg1, leftv arg2)
blackbox support - binary operations
Definition: pyobject.cc:557
void * pyobject_Copy(blackbox *b, void *ptr)
blackbox support - copy element
Definition: pyobject.cc:503
BOOLEAN python_run(leftv result, leftv arg)
Execute string in python from Singular.
Definition: pyobject.cc:442
#define omAlloc0(size)
Definition: omAllocDecl.h:211
BOOLEAN pyobject_Assign ( leftv  l,
leftv  r 
)

blackbox support - assign element

Definition at line 510 of file pyobject.cc.

511 {
512  Py_XDECREF(l->Data());
513  PyObject* result = PythonCastDynamic(r);
514  Py_XINCREF(result);
515 
516  if (l->rtyp == IDHDL)
517  IDDATA((idhdl)l->data) = (char *)result;
518  else
519  l->data = (void *)result;
520 
521  return !result;
522 }
This class does conversion of Singular objects to python objects on runtime.
Definition: pyobject.cc:339
Definition: idrec.h:34
#define IDHDL
Definition: tok.h:35
void * data
Definition: subexpr.h:89
int rtyp
Definition: subexpr.h:92
void * Data()
Definition: subexpr.cc:1118
#define IDDATA(a)
Definition: ipid.h:125
return result
Definition: facAbsBiFact.cc:76
blackbox* pyobject_blackbox ( int &  tok)

Definition at line 694 of file pyobject.cc.

694  {
695  if(blackboxIsCmd("pyobject", tok) != ROOT_DECL)
696  {
697  tok = setBlackboxStuff((blackbox*)omAlloc0(sizeof(blackbox)),
698  "pyobject");
699  }
700  return getBlackboxStuff(tok);
701 }
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:193
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:128
#define omAlloc0(size)
Definition: omAllocDecl.h:211
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:20
void* pyobject_Copy ( blackbox *  b,
void *  ptr 
)

blackbox support - copy element

Definition at line 503 of file pyobject.cc.

504 {
505  Py_XINCREF(ptr);
506  return ptr;
507 }
void pyobject_destroy ( blackbox *  b,
void *  ptr 
)

blackbox support - destruction

Definition at line 641 of file pyobject.cc.

642 {
643  Py_XDECREF(ptr);
644 }
void* pyobject_Init ( blackbox *  )

blackbox support - initialization

Definition at line 490 of file pyobject.cc.

491 {
492  Py_INCREF(Py_None);
493  return Py_None;
494 }
BOOLEAN pyobject_Op1 ( int  op,
leftv  res,
leftv  head 
)

blackbox support - unary operations

Definition at line 526 of file pyobject.cc.

527 {
528  switch(op)
529  {
530  case INT_CMD: // built-in return types first
531  {
532  long value = PyInt_AsLong(PythonCastStatic<>(head));
533  if( (value == -1) && PyErr_Occurred() ) {
534  Werror("'pyobject` cannot be converted to integer");
535  PyErr_Clear();
536  return TRUE;
537  }
538  res->data = (void*) value;
539  res->rtyp = INT_CMD;
540  return FALSE;
541  }
542  case TYPEOF_CMD:
543  res->data = (void*) omStrDup("pyobject");
544  res->rtyp = STRING_CMD;
545  return FALSE;
546  }
547 
548  if (!PythonCastStatic<>(head)(op).assign_to(res))
549  return FALSE;
550 
551  BOOLEAN newstruct_Op1(int, leftv, leftv); // forward declaration
552  return newstruct_Op1(op, res, head);
553 }
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
Definition: tok.h:98
BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
Definition: newstruct.cc:246
#define FALSE
Definition: auxiliary.h:140
#define TRUE
Definition: auxiliary.h:144
void * data
Definition: subexpr.h:89
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
int rtyp
Definition: subexpr.h:92
int BOOLEAN
Definition: auxiliary.h:131
void Werror(const char *fmt,...)
Definition: reporter.cc:199
#define omStrDup(s)
Definition: omAllocDecl.h:263
BOOLEAN pyobject_Op2 ( int  op,
leftv  res,
leftv  arg1,
leftv  arg2 
)

blackbox support - binary operations

Definition at line 557 of file pyobject.cc.

558 {
559  PythonCastStatic<> lhs(arg1);
560 
561  switch(op) // built-in return types and special cases first
562  {
563  case '<': case '>': case EQUAL_EQUAL: case NOTEQUAL: case GE: case LE:
564  {
565  res->data = (void *)(long)(lhs.compare(op, PythonCastDynamic(arg2)));
566  res->rtyp = INT_CMD;
567  return FALSE;
568  }
569  case '.': case COLONCOLON: case ATTRIB_CMD:
570  return lhs.attr(get_attrib_name(arg2)).assign_to(res);
571  }
572 
573  PythonCastDynamic rhs(arg2);
574  if (!lhs(op, rhs).assign_to(res))
575  return FALSE;
576 
577  BOOLEAN newstruct_Op2(int, leftv, leftv, leftv); // forward declaration
578  return newstruct_Op2(op, res, arg1, arg2);
579 
580 }
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
Definition: tok.h:98
#define FALSE
Definition: auxiliary.h:140
Definition: grammar.cc:271
This class does conversion of Singular objects to python objects on runtime.
Definition: pyobject.cc:339
BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
Definition: newstruct.cc:335
void * data
Definition: subexpr.h:89
Definition: tok.h:40
Definition: grammar.cc:270
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
PythonObject get_attrib_name(leftv arg)
Definition: pyobject.cc:414
int rtyp
Definition: subexpr.h:92
int BOOLEAN
Definition: auxiliary.h:131
BOOLEAN pyobject_Op3 ( int  op,
leftv  res,
leftv  arg1,
leftv  arg2,
leftv  arg3 
)

blackbox support - ternary operations

Definition at line 583 of file pyobject.cc.

584 {
585  PythonCastStatic<> lhs(arg1);
586  PythonCastDynamic rhs1(arg2);
587  PythonCastDynamic rhs2(arg3);
588 
589  if (!lhs(op, rhs1, rhs2).assign_to(res))
590  return FALSE;
591 
592  return blackboxDefaultOp3(op, res, arg1, arg2, arg3);
593 }
#define FALSE
Definition: auxiliary.h:140
BOOLEAN blackboxDefaultOp3(int op, leftv, leftv r1, leftv, leftv)
default procedure blackboxDefaultOp3, to be called as "default:" branch
Definition: blackbox.cc:90
This class does conversion of Singular objects to python objects on runtime.
Definition: pyobject.cc:339
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
BOOLEAN pyobject_OpM ( int  op,
leftv  res,
leftv  args 
)

blackbox support - n-ary operations

Definition at line 597 of file pyobject.cc.

598 {
599  switch(op) // built-in return types first
600  {
601  case STRING_CMD:
602  {
603  blackbox* a = getBlackboxStuff(args->Typ());
604  res->data = (void *)a->blackbox_String(a, args->Data());
605  res->rtyp = STRING_CMD;
606  return FALSE;
607  }
608 
609  case INTVEC_CMD:
610  PythonObject obj = PythonCastStatic<>(args->Data());
611  unsigned long len = obj.size();
612 
613  intvec* vec = new intvec(len);
614  for(unsigned long idx = 0; idx != len; ++idx) {
615  long value = PyInt_AsLong(obj[idx]);
616  (*vec)[idx] = static_cast<int>(value);
617 
618  if ((value == -1) && PyErr_Occurred()) {
619  value = 0;
620  PyErr_Clear();
621  }
622  if (value != long((*vec)[idx])) {
623  delete vec;
624  Werror("'pyobject` cannot be converted to intvec");
625  return TRUE;
626  }
627  }
628  res->data = (void *)vec;
629  res->rtyp = op;
630  return FALSE;
631  }
633  if (! PythonCastStatic<>(args)(op, seq_type(args->next)).assign_to(res))
634  return FALSE;
635 
636  BOOLEAN newstruct_OpM(int, leftv, leftv); // forward declaration
637  return newstruct_OpM(op, res, args);
638 }
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
const poly a
Definition: syzextra.cc:212
#define FALSE
Definition: auxiliary.h:140
BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
Definition: newstruct.cc:485
#define TRUE
Definition: auxiliary.h:144
int Typ()
Definition: subexpr.cc:976
void * data
Definition: subexpr.h:89
fq_nmod_poly_t * vec
Definition: facHensel.cc:103
Definition: intvec.h:14
Template specialization for getting handling sequence.
Definition: pyobject.cc:390
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
leftv next
Definition: subexpr.h:87
int rtyp
Definition: subexpr.h:92
void * Data()
Definition: subexpr.cc:1118
This class defines an interface for calling PyObject from Singular.
Definition: pyobject.cc:107
int BOOLEAN
Definition: auxiliary.h:131
Py_ssize_t size() const
Definition: pyobject.cc:187
void Werror(const char *fmt,...)
Definition: reporter.cc:199
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:20
char* pyobject_String ( blackbox *  b,
void *  ptr 
)

blackbox support - convert to string representation

Definition at line 497 of file pyobject.cc.

498 {
499  return PythonCastStatic<>(ptr).repr();
500 }
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
PythonObject python_eval ( const char *  arg)

Evaluate string in python.

Definition at line 424 of file pyobject.cc.

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

Evaluate string in python from Singular.

Definition at line 431 of file pyobject.cc.

431  {
432  if ( !arg || (arg->Typ() != STRING_CMD) ) {
433  Werror("expected python_eval('string')");
434  return TRUE;
435  }
436 
437  return python_eval(reinterpret_cast<const char*>(arg->Data())).assign_to(result);
438 }
PythonObject python_eval(const char *arg)
Evaluate string in python.
Definition: pyobject.cc:424
#define TRUE
Definition: auxiliary.h:144
int Typ()
Definition: subexpr.cc:976
BOOLEAN assign_to(leftv result)
Definition: pyobject.cc:189
void * Data()
Definition: subexpr.cc:1118
void Werror(const char *fmt,...)
Definition: reporter.cc:199
BOOLEAN python_import ( leftv  result,
leftv  value 
)

import python module and export identifiers in Singular namespace

Definition at line 475 of file pyobject.cc.

475  {
476 
477  if ((value == NULL) || (value->Typ()!= STRING_CMD)) {
478  Werror("expected python_import('string')");
479  return TRUE;
480  }
481 
482  from_module_import_all(reinterpret_cast<const char*>(value->Data()));
483  sync_contexts();
484 
485  Py_INCREF(Py_None);
486  return PythonCastStatic<>(Py_None).assign_to(result);
487 }
#define TRUE
Definition: auxiliary.h:144
int Typ()
Definition: subexpr.cc:976
void sync_contexts()
getting stuff from python to Singular namespace
Definition: pyobject.cc:654
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
#define NULL
Definition: omList.c:10
void from_module_import_all(const char *module_name)
Definition: pyobject.cc:467
void * Data()
Definition: subexpr.cc:1118
void Werror(const char *fmt,...)
Definition: reporter.cc:199
BOOLEAN python_run ( leftv  result,
leftv  arg 
)

Execute string in python from Singular.

Definition at line 442 of file pyobject.cc.

443 {
444  if ( !arg || (arg->Typ() != STRING_CMD) ) {
445  Werror("expected python_run('string')");
446  return TRUE;
447  }
448 
449  PyRun_SimpleString(reinterpret_cast<const char*>(arg->Data()));
450  sync_contexts();
451 
452  Py_INCREF(Py_None);
453  return PythonCastStatic<>(Py_None).assign_to(result);
454 }
#define TRUE
Definition: auxiliary.h:144
int Typ()
Definition: subexpr.cc:976
void sync_contexts()
getting stuff from python to Singular namespace
Definition: pyobject.cc:654
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:303
void * Data()
Definition: subexpr.cc:1118
void Werror(const char *fmt,...)
Definition: reporter.cc:199
void sync_contexts ( )

getting stuff from python to Singular namespace

Definition at line 654 of file pyobject.cc.

655 {
656  PyRun_SimpleString("_SINGULAR_NEW = modules['__main__'].__dict__.copy()");
657 
658  PythonObject newElts = python_eval("[(_k, _e) \
659  for (_k, _e) in _SINGULAR_NEW.iteritems() \
660  if _k not in _SINGULAR_IMPORTED or not _SINGULAR_IMPORTED[_k] is _e]");
661 
662  long len = newElts.size();
663  for (long idx = 0; idx < len; ++idx)
664  {
665  long i = 0;
666  char* name = newElts[idx][i].str();
667  if (name && (*name != '\0') && (*name != '_'))
668  {
669  Py_XDECREF(get_current_definition(name));
670  i = 1;
671  newElts[idx][i].import_as(name);
672  }
673 
674  }
675 
676  PythonObject deletedElts =
677  python_eval("list(set(_SINGULAR_IMPORTED.iterkeys()) - \
678  set(_SINGULAR_NEW.iterkeys()))");
679  len = deletedElts.size();
680 
681  for (long idx = 0; idx < len; ++idx)
682  {
683  char* name = deletedElts[idx].str();
684  if (name && (*name != '\0') && (*name != '_'))
685  killid(name, &IDROOT);
686  }
687 
688  PyRun_SimpleString("_SINGULAR_IMPORTED =_SINGULAR_NEW");
689  PyRun_SimpleString("del _SINGULAR_NEW");
690 }
char * str() const
Extract C-style string.
Definition: pyobject.cc:185
void killid(const char *id, idhdl *ih)
Definition: ipid.cc:345
PythonObject python_eval(const char *arg)
Evaluate string in python.
Definition: pyobject.cc:424
#define IDROOT
Definition: ipid.h:20
PyObject * get_current_definition(const char *name)
Definition: pyobject.cc:646
void import_as(const char *name) const
Definition: pyobject.cc:194
int i
Definition: cfEzgcd.cc:123
char name(const Variable &v)
Definition: factory.h:178
This class defines an interface for calling PyObject from Singular.
Definition: pyobject.cc:107
Py_ssize_t size() const
Definition: pyobject.cc:187