My Project
python_module.cc
Go to the documentation of this file.
1 #include "kernel/mod2.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <ctype.h>
6 
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include "Singular/tok.h"
10 #include "kernel/structs.h"
11 #include "Singular/mod_lib.h"
12 #include "Singular/ipid.h"
13 
14 #include "Singular/locals.h"
15 
16 #include "omalloc/omalloc.h"
17 
18 #include <time.h>
19 #ifdef HAVE_PYTHON
20 #include <boost/python.hpp>
21 #include <Python.h>
22 #include <Python.h>
23 #include "wrapper.h"
24 
25 static BOOLEAN mod_python(leftv __res, leftv __h)
26 {
27  leftv __v = __h, __v_save;
28  int __tok = NONE, __index = 0;
29  sleftv __sa; leftv __za = &__sa;
30  char * a;
31  if(__v==NULL) goto mod_python_error;
32  __tok = __v->Typ();
33  if((__index=iiTestConvert(__tok, STRING_CMD))==0)
34  goto mod_python_error;
35  __v_save = __v->next;
36  __v->next = NULL;
37  if(iiConvert(__tok, STRING_CMD, __index, __v, __za))
38  goto mod_python_error;
39  __v = __v_save;
40  a = (char *)__za->Data();
41  if(__v!=NULL) { __tok = __v->Typ(); goto mod_python_error; }
42 
43  PyRun_SimpleString(a);
44  __res->data = NULL;
45  __res->rtyp = END_RING;
46  return FALSE;
47 
48  mod_python_error:
49  Werror("python(`%s`) is not supported", Tok2Cmdname(__tok));
50  Werror("expected python('string')");
51  return TRUE;
52 }
53 //int mod_init(
54 // int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic,
55 // BOOLEAN(*func)(leftv res, leftv v))
56 // )
57 extern "C" int SI_MOD_INIT(python_module)(SModulFunctions* psModulFunctions)
58 {
59  VAR char *py=getenv("PYTHONPATH");
60  VAR char buf[1024];
61  if (py!=NULL)
62  {
63  strcpy(buf,py);
64  strcat(buf,":");
65  strcat(buf,feResource('s',FALSE));
66  }
67  else
68  strcpy(buf,feResource('s',FALSE));
69  Print("setting PYTHONAPTH to %s\n",buf);
70  setenv("PYTHONPATH",buf,1);
71 
72  Py_Initialize();
73  PyRun_SimpleString("from sys import path\n\
74 path.insert(0,'.')\n");
75  initSingular();
76  init_Singular();
77 
78  psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python);
79  return MAX_TOK;
80 }
81 #else
82 extern "C" int SI_MOD_INIT(python_module)(SModulFunctions* psModulFunctions)
83 {
84  PrintS("no python_mod support\n");
85  return MAX_TOK;
86 }
87 #endif
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int Typ()
Definition: subexpr.cc:1011
int rtyp
Definition: subexpr.h:91
void * Data()
Definition: subexpr.cc:1154
leftv next
Definition: subexpr.h:86
void * data
Definition: subexpr.h:88
#define Print
Definition: emacs.cc:80
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:258
char * getenv()
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:140
int iiTestConvert(int inputType, int outputType)
Definition: gentable.cc:301
#define VAR
Definition: globaldefs.h:5
@ END_RING
Definition: grammar.cc:310
BOOLEAN iiConvert(int inputType, int outputType, int index, leftv input, leftv output, const struct sConvertTypes *dConvertTypes)
Definition: ipconv.cc:435
VAR package currPack
Definition: ipid.cc:57
#define NULL
Definition: omList.c:12
int SI_MOD_INIT() python_module(SModulFunctions *psModulFunctions)
static BOOLEAN mod_python(leftv __res, leftv __h)
void PrintS(const char *s)
Definition: reporter.cc:284
void Werror(const char *fmt,...)
Definition: reporter.cc:189
int status int void * buf
Definition: si_signals.h:59
@ STRING_CMD
Definition: tok.h:185
@ MAX_TOK
Definition: tok.h:218
#define NONE
Definition: tok.h:221