source: git/modules/python/python.cc @ 5eeaa6

fieker-DuValspielwiese
Last change on this file since 5eeaa6 was 9be487, checked in by Michael Brickenstein <bricken@…>, 17 years ago
+ adjusted to Singular 3.0.3/add . to default path git-svn-id: file:///usr/local/Singular/svn/trunk@9715 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2 * This was automatically generated by modgen
3 * version 0.2
4 * module python
5 * Don't edit this file
6 */
7
8
9#line 10 "python.cc"
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include <ctype.h>
14
15#include <unistd.h>
16#include <sys/stat.h>
17#include <boost/python.hpp>
18#include <Python.h>
19#include <mod2.h>
20#include <tok.h>
21#include <structs.h>
22#include <ipid.h>
23
24#include <locals.h>
25#include <omalloc.h>
26#include "python.h"
27#line 19 "python.cc"
28
29idhdl enter_id(char *name, char *value, idtyp t)
30{
31  idhdl h;
32
33  h=enterid(omStrDup(name),0, t, &(currPack->idroot), TRUE/*FALSE*/);
34  if(h!=NULL) {
35     switch(t) {
36         case STRING_CMD: 
37              omFree(IDSTRING(h));
38              IDSTRING(h) = omStrDup(value);
39              break;
40         case PACKAGE_CMD: break;
41         case PROC_CMD: break;
42     }
43  } else 
44      Warn("Cannot create '%s'\n", name);
45  return(h);
46}
47
48#line 37 "python.cc"
49
50idhdl add_singular_proc(FILE* binfp, char *procname,int line,
51                       long pos, long end, BOOLEAN pstatic)
52{
53  idhdl h;
54  procinfov pi;
55  char *tempstr;
56  h = enter_id(procname, NULL, PROC_CMD);
57  if(h == NULL) return NULL;
58
59  pi = IDPROC(h);
60
61  pi->libname = omStrDup(currPack->libname);
62  pi->procname = omStrDup(procname);
63  pi->language = LANG_SINGULAR;
64  pi->ref = 1;
65  pi->is_static = pstatic;
66  pi->data.s.proc_start = pos;
67  pi->data.s.def_end    = pos;
68  pi->data.s.help_start = 0L;
69  pi->data.s.help_end   = 0L;
70  pi->data.s.body_start = pos;
71  pi->data.s.body_end   = end;
72  pi->data.s.proc_end   = end;
73  pi->data.s.example_start = 0L;
74  pi->data.s.proc_lineno = line;
75  pi->data.s.body_lineno = line;
76  pi->data.s.example_lineno = 0;
77  pi->data.s.help_chksum = 0;
78 
79  rewind(binfp);
80  fseek(binfp,pos,SEEK_CUR);
81  tempstr=(char *)omAlloc(end-pos+2);
82  memset(tempstr,0,end-pos+2);
83  fread(tempstr,sizeof(char),end-pos+1,binfp);
84  pi->data.s.body = omStrDup(tempstr);
85  omFree(tempstr);
86
87  return(h);
88}
89
90unsigned long crccheck(
91  char *file
92  ) 
93{
94  unsigned char buf[BUFLEN1];
95  unsigned long crc = 0;
96  long length = 0;
97  long bytes_read;
98  register FILE *fp;
99
100  // open the file
101  fp = fopen (file, "rb");
102  if (fp == NULL) return 0;
103
104  // read the file chunk by chunk, determine the lenght and
105  // start computing the checksum
106  while ((bytes_read = fread (buf, 1, BUFLEN1, fp)) > 0)
107  {
108    unsigned char *cp = buf;
109    length += bytes_read;
110    while (bytes_read--)
111      crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF];
112    }
113
114  // check if something went wrong and close the file
115  if (ferror (fp)) return 0; 
116  if (fclose(fp)==EOF) return 0;
117
118  // second part of checksum computation
119  bytes_read = length;
120  while (bytes_read > 0) {
121    crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];
122    bytes_read >>= 8;
123  }
124
125  crc = ~crc & 0xFFFFFFFF;
126
127  return(crc);
128}
129
130void fill_help_package();
131void fill_example_package();
132
133#line 2 "python.mod"
134/*
135 *  $Id: python.cc,v 1.6 2007-01-15 09:45:54 bricken Exp $
136 *
137 *  Test mod fuer modgen
138 */
139
140#include <stdio.h>
141#include <stdlib.h>
142#include <string.h>
143#include <time.h>
144#include <Python.h>
145#include "wrapper.h"
146
147void mbpython(char* in);
148#line 17 "python.mod"
149
150
151#line 137 "python.cc"
152extern "C" {
153//int mod_init(
154//  int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic,
155//              BOOLEAN(*func)(leftv res, leftv v))
156//  )
157  int mod_init(
158    SModulFunctions* psModulFunctions
159    )
160{
161  idhdl h;
162  char * tempstr;
163  FILE * binfp; 
164  int ret;
165  struct stat sb; 
166
167  tempstr = (char *)omAlloc(strlen(currPack->libname)+5);
168  memset(tempstr,0,strlen(currPack->libname)+1);
169  memcpy(tempstr,currPack->libname,strlen(currPack->libname));
170  memcpy(tempstr+strlen(currPack->libname)-3,".bin",4);
171  ret=stat(tempstr,&sb);
172  if(ret==0) { 
173    if ((sb.st_mode & S_IFMT) == S_IFREG) { 
174      if (crccheck(tempstr)!=crcsum)
175      {   Warn("file %s does not agree with module version - ignoring file",tempstr);
176          ret=-1;
177      }
178      if ((binfp = fopen(tempstr,"r")) == NULL) return -1;
179    } 
180    else 
181      ret=-1; 
182  }
183
184  fill_help_package();
185  fill_example_package();
186
187#line 20 "python.mod"
188  enter_id("category","tests", STRING_CMD);
189#line 22 "python.mod"
190  enter_id("version","$Id: python.cc,v 1.6 2007-01-15 09:45:54 bricken Exp $", STRING_CMD);
191#line 23 "python.mod"
192  enter_id("info","LIBRARY: kernel.lib  PROCEDURES OF GENERAL TYPE WRITEN IN C python(input); eval a string  in python", STRING_CMD);
193#line 26 "python.mod"
194#line 27 "python.mod"
195  Py_Initialize();
196  PyRun_SimpleString("from sys import path\n\
197path.insert(0,'.')\n");
198  initSingular();
199  init_Singular();
200
201#line 36 "python.mod"
202  psModulFunctions->iiAddCproc(currPack->libname,"python",FALSE, mod_python);
203//psModulFunctions->iiAddCproc("packagename","procname",FALSE, procedure);
204  if(ret!=-1) fclose(binfp);
205  return 0;
206}
207}
208
209#line 187 "python.cc"
210/* Help section */
211void fill_help_package() {
212
213#line 192 "python.cc"
214}  /* End of Help section */
215
216/* Example section */
217void fill_example_package() {
218#line 43 "python.mod"
219  enter_id("python_example",
220 "\
221  python(\"print 1+1\");\
222", STRING_CMD);
223
224
225#line 206 "python.cc"
226} /* End of Example section */
227
228#line 36 "python.mod"
229BOOLEAN mod_python(leftv __res, leftv __h)
230{
231#line 37 "python.mod"
232#line 214 "python.cc"
233 
234#line 38 "python.mod"
235#line 217 "python.cc"
236  leftv __v = __h, __v_save;
237  int __tok = NONE, __index = 0;
238  sleftv __sa; leftv __za = &__sa;
239  char * a;
240#line 38 "python.mod"
241
242
243#line 39 "python.mod"
244#line 226 "python.cc"
245  if(__v==NULL) goto mod_python_error;
246  __tok = __v->Typ();
247  if((__index=iiTestConvert(__tok, STRING_CMD))==0)
248     goto mod_python_error;
249  __v_save = __v->next;
250  __v->next = NULL;
251  if(iiConvert(__tok, STRING_CMD, __index, __v, __za))
252     goto mod_python_error;
253  __v = __v_save;
254  a = (char *)__za->Data();
255  if(__v!=NULL) { __tok = __v->Typ(); goto mod_python_error; }
256
257#line 39 "python.mod"
258
259  mbpython(a);
260#line 41 "python.mod"
261#line 243 "python.cc"
262  __res->data = NULL;
263  __res->rtyp = END_RING;
264  return FALSE;
265
266  mod_python_error:
267    Werror("python(`%s`) is not supported", Tok2Cmdname(__tok));
268    Werror("expected python('string')");
269    return TRUE;
270}
271
272
273#line 254 "python.cc"
274#line 53 "python.mod"
275void mbpython(char* inp){
276  PyRun_SimpleString(inp);
277}
Note: See TracBrowser for help on using the repository browser.