Loading...
Searching...
No Matches
python_module.cc
Go to the documentation of this file.
#include "kernel/mod2.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include "Singular/tok.h"
#include "kernel/structs.h"
#include "Singular/mod_lib.h"
#include "Singular/ipid.h"
#include "Singular/locals.h"
#include "omalloc/omalloc.h"
#include <time.h>
#ifdef HAVE_PYTHON
#include <boost/python.hpp>
#include <Python.h>
#include <Python.h>
#include "wrapper.h"
static BOOLEAN mod_python(leftv __res, leftv __h)
{
leftv __v = __h, __v_save;
int __tok = NONE, __index = 0;
sleftv __sa; leftv __za = &__sa;
char * a;
if(__v==NULL) goto mod_python_error;
__tok = __v->Typ();
if((__index=iiTestConvert(__tok, STRING_CMD))==0)
goto mod_python_error;
__v_save = __v->next;
__v->next = NULL;
if(iiConvert(__tok, STRING_CMD, __index, __v, __za))
goto mod_python_error;
__v = __v_save;
a = (char *)__za->Data();
if(__v!=NULL) { __tok = __v->Typ(); goto mod_python_error; }
PyRun_SimpleString(a);
__res->data = NULL;
__res->rtyp = END_RING;
return FALSE;
mod_python_error:
Werror("python(`%s`) is not supported", Tok2Cmdname(__tok));
Werror("expected python('string')");
return TRUE;
}
//int mod_init(
// int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic,
// BOOLEAN(*func)(leftv res, leftv v))
// )
extern "C" int SI_MOD_INIT(python_module)(SModulFunctions* psModulFunctions)
{
VAR char *py=getenv("PYTHONPATH");
VAR char buf[1024];
if (py!=NULL)
{
strcpy(buf,py);
strcat(buf,":");
strcat(buf,feResource('s',FALSE));
}
else
strcpy(buf,feResource('s',FALSE));
Print("setting PYTHONAPTH to %s\n",buf);
setenv("PYTHONPATH",buf,1);
Py_Initialize();
PyRun_SimpleString("from sys import path\n\
path.insert(0,'.')\n");
initSingular();
init_Singular();
psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python);
return MAX_TOK;
}
#else
extern "C" int SI_MOD_INIT(python_module)(SModulFunctions* psModulFunctions)
{
PrintS("no python_mod support\n");
return MAX_TOK;
}
#endif
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
sleftv
Class used for (list of) interpreter objects.
Definition subexpr.h:83
sleftv::Typ
int Typ()
Definition subexpr.cc:1048
sleftv::rtyp
int rtyp
Definition subexpr.h:91
sleftv::Data
void * Data()
Definition subexpr.cc:1192
sleftv::next
leftv next
Definition subexpr.h:86
sleftv::data
void * data
Definition subexpr.h:88
Print
#define Print
Definition emacs.cc:80
feResource
static char * feResource(feResourceConfig config, int warn)
Definition feResource.cc:233
getenv
char * getenv()
iiTestConvert
int iiTestConvert(int inputType, int outputType)
Definition gentable.cc:298
Tok2Cmdname
const char * Tok2Cmdname(int tok)
Definition gentable.cc:137
VAR
#define VAR
Definition globaldefs.h:5
END_RING
@ END_RING
Definition grammar.cc:311
iiConvert
BOOLEAN iiConvert(int inputType, int outputType, int index, leftv input, leftv output, const struct sConvertTypes *dConvertTypes)
Definition ipconv.cc:450
currPack
VAR package currPack
Definition ipid.cc:55
ipid.h
SModulFunctions
Definition ipid.h:69
locals.h
mod2.h
mod_lib.h
NULL
#define NULL
Definition omList.c:12
omalloc.h
python_module
int SI_MOD_INIT python_module(SModulFunctions *psModulFunctions)
Definition python_module.cc:57
mod_python
static BOOLEAN mod_python(leftv __res, leftv __h)
Definition python_module.cc:25
PrintS
void PrintS(const char *s)
Definition reporter.cc:288
Werror
void Werror(const char *fmt,...)
Definition reporter.cc:189
buf
int status int void * buf
Definition si_signals.h:69
structs.h
leftv
sleftv * leftv
Definition structs.h:53
tok.h
STRING_CMD
@ STRING_CMD
Definition tok.h:187
MAX_TOK
@ MAX_TOK
Definition tok.h:220
NONE
#define NONE
Definition tok.h:223
wrapper.h