My Project
Loading...
Searching...
No Matches
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);
#define FALSE
Definition: auxiliary.h:96
VAR package currPack
Definition: ipid.cc:57
int name
New type name for int.
Definition: templateForC.h:21

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}
This template class does conversion of Singular objects to python objects on compile-time.
Definition: pyobject.cc:304
int Typ()
Definition: subexpr.cc:1019
const char * Name()
Definition: subexpr.h:120
@ STRING_CMD
Definition: tok.h:185

◆ 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}
static id_type id()
Get Singular type identitfier.
Definition: pyobject.cc:56
Definition: idrec.h:35
idhdl ggetid(const char *n)
Definition: ipid.cc:581
#define IDDATA(a)
Definition: ipid.h:126
#define IDTYP(a)
Definition: ipid.h:119
#define NULL
Definition: omList.c:12

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

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

◆ 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}
int l
Definition: cfEzgcd.cc:100
This class does conversion of Singular objects to python objects on runtime.
Definition: pyobject.cc:340
return result
Definition: facAbsBiFact.cc:75
#define IDHDL
Definition: tok.h:31

◆ 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}
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:142
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:17
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:218
@ ROOT_DECL
Definition: grammar.cc:319

◆ 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}
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
CanonicalForm head(const CanonicalForm &f)
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
CanonicalForm res
Definition: facAbsFact.cc:60
void WerrorS(const char *s)
Definition: feFopen.cc:24
BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
Definition: newstruct.cc:251
#define omStrDup(s)
Definition: omAllocDecl.h:263
@ TYPEOF_CMD
Definition: tok.h:194
@ 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.

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}
@ GE
Definition: grammar.cc:269
@ EQUAL_EQUAL
Definition: grammar.cc:268
@ LE
Definition: grammar.cc:270
@ NOTEQUAL
Definition: grammar.cc:273
@ COLONCOLON
Definition: grammar.cc:275
BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
Definition: newstruct.cc:336
PythonObject get_attrib_name(leftv arg)
Definition: pyobject.cc:414
@ 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.

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

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}
This class defines an interface for calling PyObject from Singular.
Definition: pyobject.cc:108
Py_ssize_t size() const
Definition: pyobject.cc:187
Definition: intvec.h:23
void * Data()
Definition: subexpr.cc:1162
leftv next
Definition: subexpr.h:86
fq_nmod_poly_t * vec
Definition: facHensel.cc:108
BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
Definition: newstruct.cc:478
@ 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.

502{
503 return PythonCastStatic<>(ptr).repr();
504}
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.

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}
BOOLEAN assign_to(leftv result)
Definition: pyobject.cc:189

◆ 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()));
488
489 Py_INCREF(Py_None);
490 return PythonCastStatic<>(Py_None).assign_to(result);
491}
void sync_contexts()
getting stuff from python to Singular namespace
Definition: pyobject.cc:662
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.

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()));
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}
int i
Definition: cfEzgcd.cc:132
void import_as(const char *name) const
Definition: pyobject.cc:194
char * str() const
Extract C-style string.
Definition: pyobject.cc:185
void killid(const char *id, idhdl *ih)
Definition: ipid.cc:387
#define IDROOT
Definition: ipid.h:19
PyObject * get_current_definition(const char *name)
Definition: pyobject.cc:654