CoCalc Shared Fileswww / calc / calc / pari.py
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
9import os   # operating system calls
10import re   # regular expressions
11
12PARI="ulimit -t 20; time /var/www/cgi-bin/calc/gp -s 1024000000"
14     'addhelp', 'default', 'getheap', 'getstack', 'install', 'quit', '%', 'Str']
15
16MAXLEN=10000
17START="START"
18STOP="STOP"
19
20def 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
28def pari(cmd):
29    s = strip(cmd)
31        if s.find(word) != -1:
32            return "WARNING: PARI command contains unsafe command "+\
33                   "'%s', so it will not be executed."%word
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()
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)
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