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