CoCalc Shared Fileswww / calc / calc / pari.pyOpen in CoCalc with one click!
Author: William A. Stein
1
###########################################################
2
# #
3
# pari.py -- compute things using system calls to PARI #
4
# #
5
# Original author: William Stein, 10/10/2003 #
6
# Released under GPL, etc. #
7
###########################################################
8
9
import os # operating system calls
10
import re # regular expressions
11
12
PARI="ulimit -t 20; time /var/www/cgi-bin/calc/gp -s 1024000000"
13
BAD=['eval', 'system', 'alias', 'allocatemem', 'extern', 'read', 'plot', 'write',\
14
'addhelp', 'default', 'getheap', 'getstack', 'install', 'quit', '%', 'Str']
15
16
MAXLEN=10000
17
START="START"
18
STOP="STOP"
19
20
def strip(cmd):
21
cmd0 = cmd.lower()
22
ans=""
23
for x in cmd.lower():
24
if ord(x) >= ord("a") and ord(x) <= ord("z"):
25
ans=ans+x
26
return ans
27
28
def pari(cmd):
29
s = strip(cmd)
30
for word in BAD:
31
if s.find(word) != -1:
32
return "WARNING: PARI command contains unsafe command "+\
33
"'%s', so it will not be executed."%word
34
for word in BAD:
35
if cmd.find(word) != -1:
36
return "WARNING: PARI command contains unsafe command "+\
37
"'%s', so it will not be executed."%word
38
39
process = os.popen3(PARI)
40
process[0].write(cmd)
41
process[0].close()
42
out=process[1].read()
43
i = out.find('\n')
44
j = out.find('support')
45
out = out[:i] + out[j+9:]
46
#out=re.sub(r'\[0\d?;?\d?\d?m',"",out)
47
err=process[2].read()
48
if len(out) > MAXLEN:
49
out = out[:MAXLEN]
50
out = out + "\n ** WARNING: Output too long, hence truncated."
51
i = err.find("gprc")
52
if i != -1:
53
err = err[i+5:]
54
err = err.replace("...Done.",'')
55
if err!="":
56
err=err.replace("real","Timing:\nreal")
57
out=out+"\nErrors/timing (if any):"+err
58
if out.find("the PARI stack") != -1:
59
out = "** WARNING: Computation used more memory than allowed. **\n\n" + out
60
61
return out
62
63