My Project  debian-1:4.1.1-p2+ds-4
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 #include "omalloc/omalloc.h"
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <Python.h>
25 #include "wrapper.h"
26 
27 static BOOLEAN mod_python(leftv __res, leftv __h)
28 {
29  leftv __v = __h, __v_save;
30  int __tok = NONE, __index = 0;
31  sleftv __sa; leftv __za = &__sa;
32  char * a;
33  if(__v==NULL) goto mod_python_error;
34  __tok = __v->Typ();
35  if((__index=iiTestConvert(__tok, STRING_CMD))==0)
36  goto mod_python_error;
37  __v_save = __v->next;
38  __v->next = NULL;
39  if(iiConvert(__tok, STRING_CMD, __index, __v, __za))
40  goto mod_python_error;
41  __v = __v_save;
42  a = (char *)__za->Data();
43  if(__v!=NULL) { __tok = __v->Typ(); goto mod_python_error; }
44 
45  PyRun_SimpleString(a);
46  __res->data = NULL;
47  __res->rtyp = END_RING;
48  return FALSE;
49 
50  mod_python_error:
51  Werror("python(`%s`) is not supported", Tok2Cmdname(__tok));
52  Werror("expected python('string')");
53  return TRUE;
54 }
55 //int mod_init(
56 // int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic,
57 // BOOLEAN(*func)(leftv res, leftv v))
58 // )
59 extern "C" int SI_MOD_INIT(python_module)(SModulFunctions* psModulFunctions)
60 {
61  char *py=getenv("PYTHONPATH");
62  char buf[1024];
63  if (py!=NULL)
64  {
65  strcpy(buf,py);
66  strcat(buf,":");
67  strcat(buf,feResource('s',FALSE));
68  }
69  else
70  strcpy(buf,feResource('s',FALSE));
71  Print("setting PYTHONAPTH to %s\n",buf);
72  setenv("PYTHONPATH",buf,1);
73 
74  Py_Initialize();
75  PyRun_SimpleString("from sys import path\n\
76 path.insert(0,'.')\n");
77  initSingular();
78  init_Singular();
79 
80  psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python);
81  return MAX_TOK;
82 }
83 #endif
iiConvert
BOOLEAN iiConvert(int inputType, int outputType, int index, leftv input, leftv output, const struct sConvertTypes *dConvertTypes)
Definition: ipconv.cc:400
FALSE
#define FALSE
Definition: auxiliary.h:94
mod_lib.h
omalloc.h
sleftv::Data
void * Data()
Definition: subexpr.cc:1133
getenv
char * getenv()
STRING_CMD
Definition: tok.h:182
NONE
#define NONE
Definition: tok.h:217
MAX_TOK
Definition: tok.h:214
wrapper.h
currPack
package currPack
Definition: ipid.cc:58
sleftv
Class used for (list of) interpreter objects.
Definition: subexpr.h:81
TRUE
#define TRUE
Definition: auxiliary.h:98
buf
int status int void * buf
Definition: si_signals.h:58
BOOLEAN
int BOOLEAN
Definition: auxiliary.h:85
structs.h
locals.h
mod2.h
sleftv::data
void * data
Definition: subexpr.h:87
END_RING
Definition: grammar.cc:308
iiTestConvert
int iiTestConvert(int inputType, int outputType)
Definition: gentable.cc:298
feResource
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:254
Print
#define Print
Definition: emacs.cc:79
Werror
void Werror(const char *fmt,...)
Definition: reporter.cc:188
tok.h
sleftv::Typ
int Typ()
Definition: subexpr.cc:991
SModulFunctions
Definition: ipid.h:66
sleftv::rtyp
int rtyp
Definition: subexpr.h:90
NULL
#define NULL
Definition: omList.c:9
Tok2Cmdname
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:137
ipid.h
sleftv::next
leftv next
Definition: subexpr.h:85