Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. import sys
  2. is_tinypy = "tinypy" in sys.version
  3. if not is_tinypy:
  4.     from boot import *
  5. import asm
  6. import disasm
  7.  
  8. ################################################################################
  9. RM = 'rm -f '
  10. VM = './vm '
  11. TINYPY = './tinypy '
  12. TMP = 'tmp.txt'
  13. if '-mingw32' in ARGV or "-win" in ARGV:
  14.     RM = 'del '
  15.     VM = 'vm '
  16.     TINYPY = '..\\tinypy '
  17.     TMP = 'tmp.txt'
  18.     #TMP = 'stdout.txt'
  19. SANDBOX = '-sandbox' in ARGV
  20. def system_rm(fname):
  21.     system(RM+fname)
  22.  
  23. ################################################################################
  24. #if not is_tinypy:
  25.     #v = chksize()
  26.     #assert (v < 65536)
  27.  
  28. ################################################################################
  29. def t_show(t):
  30.     if t.type == 'string': return '"'+t.val+'"'
  31.     if t.type == 'number': return t.val
  32.     if t.type == 'symbol': return t.val
  33.     if t.type == 'name': return '$'+t.val
  34.     return t.type
  35. def t_tokenize(s,exp=''):
  36.     import tokenize
  37.     result = tokenize.tokenize(s)
  38.     res = ' '.join([t_show(t) for t in result])
  39.     #print(s); print(exp); print(res)
  40.     assert(res == exp)
  41.  
  42. if __name__ == '__main__':
  43.     t_tokenize("234",'234')
  44.     t_tokenize("234.234",'234.234')
  45.     t_tokenize("phil",'$phil')
  46.     t_tokenize("_phil234",'$_phil234')
  47.     t_tokenize("'phil'",'"phil"')
  48.     t_tokenize('"phil"','"phil"')
  49.     t_tokenize("'phil' x",'"phil" $x')
  50.     t_tokenize("#comment","")
  51.     t_tokenize("and","and")
  52.     t_tokenize("=","=")
  53.     t_tokenize("()","( )")
  54.     t_tokenize("(==)","( == )")
  55.     t_tokenize("phil=234","$phil = 234")
  56.     t_tokenize("a b","$a $b")
  57.     t_tokenize("a\nb","$a nl $b")
  58.     t_tokenize("a\n    b","$a nl indent $b dedent")
  59.     t_tokenize("a\n    b\n        c", "$a nl indent $b nl indent $c dedent dedent")
  60.     t_tokenize("a\n    b\n    c", "$a nl indent $b nl $c dedent")
  61.     t_tokenize("a\n    b\n        \n      c", "$a nl indent $b nl nl indent $c dedent dedent")
  62.     t_tokenize("a\n    b\nc", "$a nl indent $b nl dedent $c")
  63.     t_tokenize("a\n  b\n    c\nd", "$a nl indent $b nl indent $c nl dedent dedent $d")
  64.     t_tokenize("(\n  )","( )")
  65.     t_tokenize("  x","indent $x dedent")
  66.     t_tokenize("  #","")
  67.     t_tokenize("None","None")
  68.  
  69.  
  70. ################################################################################
  71.  
  72. def t_lisp(t):
  73.     if t.type == 'block':
  74.         return """{%s}"""%' '.join([t_lisp(tt) for tt in t.items])
  75.     if t.type == 'statement':
  76.         return """%s;"""%' '.join([t_lisp(tt) for tt in t.items])
  77.     if t.items == None: return t.val
  78.     args = ''.join([" "+t_lisp(tt) for tt in t.items])
  79.     return "("+t.val+args+")"
  80.  
  81. def t_parse(s,ex=''):
  82.     import tokenize, parse
  83.     r = ''
  84.     tokens = tokenize.tokenize(s)
  85.     tree = parse.parse(s,tokens)
  86.     r = t_lisp(tree)
  87.     #print(s); print(ex); print(r)
  88.     assert(r==ex)
  89.  
  90. def t_unparse(s):
  91.     import tokenize, parse
  92.     ok = False
  93.     try:
  94.         tokens = tokenize.tokenize(s)
  95.         tree = parse.parse(s,tokens)
  96.     except:
  97.         ok = True
  98.     assert(ok == True)
  99.  
  100.  
  101. if __name__ == '__main__':
  102.     t_parse('2+4*3', '(+ 2 (* 4 3))')
  103.     t_parse('4*(2+3)', '(* 4 (+ 2 3))')
  104.     t_parse('(2+3)*4', '(* (+ 2 3) 4)')
  105.     t_parse('1<2', '(< 1 2)')
  106.     t_parse('x=3', '(= x 3)')
  107.     t_parse('x = 2*3', '(= x (* 2 3))')
  108.     t_parse('x = y', '(= x y)')
  109.     t_parse('2,3', '(, 2 3)')
  110.     t_parse('2,3,4', '(, 2 3 4)')
  111.     t_parse('[]', '([])')
  112.     t_parse('[1]', '([] 1)')
  113.     t_parse('[2,3,4]', '([] 2 3 4)')
  114.     t_parse('print(3)', '($ print 3)')
  115.     t_parse('print()', '($ print)')
  116.     t_parse('print(2,3)', '($ print 2 3)')
  117.     t_parse('def fnc():pass', '(def fnc (():) pass)')
  118.     t_parse('def fnc(x):pass', '(def fnc ((): x) pass)')
  119.     t_parse('def fnc(x,y):pass', '(def fnc ((): x y) pass)')
  120.     t_parse('x\ny\nz', '(; x y z)')
  121.     t_parse('return x', '(return x)')
  122.     t_parse('print(test(2,3))', '($ print ($ test 2 3))')
  123.     t_parse('x.y', '(. x y)')
  124.     t_parse('get(2).x', '(. ($ get 2) x)')
  125.     t_parse('{}', '({})')
  126.     t_parse('True', 'True')
  127.     t_parse('False', 'False')
  128.     t_parse('None', 'None')
  129.     t_parse('while 1:pass', '(while 1 pass)')
  130.     t_parse('for x in y:pass', '(for x y pass)')
  131.     t_parse('print("x")', '($ print x)')
  132.     t_parse('if 1: pass', '(if (elif 1 pass))')
  133.     t_parse('x = []', '(= x ([]))')
  134.     t_parse('x[1]', '(. x 1)')
  135.     t_parse('import sdl', '(import sdl)')
  136.     t_parse('2 is 3', '(is 2 3)')
  137.     t_parse('2 is not 3', '(isnot 2 3)')
  138.     t_parse('x is None', '(is x None)')
  139.     t_parse('2 is 3 or 4 is 5', '(or (is 2 3) (is 4 5))')
  140.     t_parse('e.x == 3 or e.x == 4', '(or (== (. e x) 3) (== (. e x) 4))')
  141.     t_parse('if 1==2: 3\nelif 4:5\nelse:6', '(if (elif (== 1 2) 3) (elif 4 5) (else 6))')
  142.     t_parse('x = y(2)*3 + y(4)*5', '(= x (+ (* ($ y 2) 3) (* ($ y 4) 5)))')
  143.     t_parse('x(1,2)+y(3,4)', '(+ ($ x 1 2) ($ y 3 4))')
  144.     t_parse('x(a,b,c[d])', '($ x a b (. c d))')
  145.     t_parse('x(1,2)*j+y(3,4)*k+z(5,6)*l', '(+ (+ (* ($ x 1 2) j) (* ($ y 3 4) k)) (* ($ z 5 6) l))')
  146.     t_parse('a = b.x/c * 2 - 1', '(= a (- (* (/ (. b x) c) 2) 1))')
  147.     t_parse('for x in y: z', '(for x y z)')
  148.  
  149.     t_parse('min(255,n*2)','($ min 255 (* n 2))')
  150.     t_parse('c = pal[i*8]','(= c (. pal (* i 8)))')
  151.     t_parse('{x:y,a:b}','({} x y a b)')
  152.     t_parse('x[1:2:3]','(. x (: 1 2 3))')
  153.     if is_tinypy: t_parse('x - -234','(- x -234)')
  154.     else: t_parse('x - -234','(- x -234.0)')
  155.     t_parse('x - -y','(- x (- 0 y))')
  156.     t_parse('x = ((y*4)-2)','(= x (- (* y 4) 2))')
  157.    
  158.     if is_tinypy: t_parse('print([1,2,"OK",4][-3:3][1])','($ print (. (. ([] 1 2 OK 4) (: -3 3)) 1))')
  159.     else: t_parse('print([1,2,"OK",4][-3:3][1])','($ print (. (. ([] 1 2 OK 4) (: -3.0 3)) 1))')
  160.    
  161.     t_parse('a,b = 1,2','(= (, a b) (, 1 2))')
  162.     t_parse('class C: pass','(class C (methods pass))')
  163.     t_parse('def test(*v): pass','(def test ((): (* v)) pass)')
  164.     t_parse('def test(**v): pass','(def test ((): (** v)) pass)')
  165.     t_parse('test(*v)','($ test (* v))')
  166.     t_parse('test(**v)','($ test (** v))')
  167.     t_parse('def test(x=y): pass','(def test ((): (= x y)) pass)')
  168.     t_parse('test(x=y)','($ test (= x y))')
  169.     t_parse('def test(y="K",x="Z"): pass','(def test ((): (= y K) (= x Z)) pass)')
  170.     t_parse('return x+y','(return (+ x y))')
  171.     t_parse('if "a" is not "b": pass','(if (elif (isnot a b) pass))')
  172.     t_parse('z = 0\nfor x in y: pass','(; (= z 0) (for x y pass))')
  173.     t_parse('for k in {"OK":0}: pass','(for k ({} OK 0) pass)')
  174.     t_parse('print(test(10,3,z=50000,*[200],**{"x":4000}))','($ print ($ test 10 3 (= z 50000) (* ([] 200)) (** ({} x 4000))))')
  175.     t_parse('x="OK";print(x)','(; (= x OK) ($ print x))')
  176.     t_parse('[(1,3)]','([] (, 1 3))')
  177.     t_parse('x[:]','(. x (: None None))')
  178.     t_parse('x[:1]','(. x (: None 1))')
  179.     t_parse('x[1:]','(. x (: 1 None))')
  180.     t_parse('return\nx','(; return x)')
  181.     t_parse('"""test"""','test')
  182.     t_parse('return a,b','(return (, a b))')
  183.    
  184.    
  185.  
  186.     # this should throw an error - bug #26
  187.     t_unparse("""
  188. while 1:
  189. pass
  190. """)
  191.        
  192.     # test cases for python 2.x print statements - bug #17
  193.     # since this is python 2.x syntax it should output an syntax Exception
  194.    
  195.     # mini-block style
  196.     t_unparse("""
  197. def x(): print "OK"
  198. """)
  199.  
  200.     # block level
  201.     t_unparse("""
  202. def x():
  203.    print "OK"
  204. """)
  205.  
  206.     # module level
  207.     t_unparse("""
  208. print "OK"
  209. """)
  210.  
  211.  
  212.        
  213.  
  214. ################################################################################
  215.  
  216. def showerror(cmd, ss, ex, res):
  217.     print(cmd)
  218.     print("ss : '" + str(ss) + "'")
  219.     print("ex : '" + str(ex) + "'")
  220.     print("res: '" + str(res) + "'")
  221.  
  222. def t_render(ss,ex,exact=True):
  223.     import tokenize, parse, encode
  224.        
  225.     if not istype(ss,'list'): ss =[ss]
  226.     n = 1
  227.     for s in ss:
  228.         fname = 'tmp'+str(n)+'.tpc'
  229.         system_rm(fname)
  230.         tokens = tokenize.tokenize(s)
  231.         t = parse.parse(s,tokens)
  232.         r = encode.encode(fname,s,t)
  233.         f = save(fname,r)
  234.         n += 1
  235.     system_rm('tmp.txt')
  236.     cmd = VM + fname + " > tmp.txt"
  237.     system(cmd)
  238.     res = load(TMP).strip()
  239.    
  240.     #print(ss,ex,res)
  241.     if exact:
  242.         if res != ex: showerror(cmd, ss, ex, res)
  243.         assert(res == ex)
  244.     else:
  245.         if ex not in res: showerror(cmd, ss, ex, res)
  246.         assert(ex in res)
  247.        
  248. def t_unrender(s):
  249.     import tokenize, parse, encode
  250.        
  251.     ok = False
  252.     try:
  253.         tokens = tokenize.tokenize(s)
  254.         t = parse.parse(s,tokens)
  255.         r = encode.encode('tmp.tpc',s,t)
  256.     except:
  257.         ok = True
  258.     assert(ok == True)
  259.  
  260. def test_range():
  261.     t_render("""print(str(range(4))[:5])""","<list")
  262.     t_render("""print(len(range(4)))""","4")
  263.     t_render("""print(range(4)[0])""","0")
  264.     t_render("""print(range(4)[1])""","1")
  265.     t_render("""print(range(4)[-1])""","3")
  266.  
  267.     t_render("""print(str(range(-4))[:5])""","<list")
  268.     t_render("""print(len(range(-4)))""","0")
  269.  
  270.     t_render("""print(str(range(0,5,3))[:5])""","<list")
  271.     t_render("""print(len(range(0,5,3)))""","2")
  272.     t_render("""print(range(0,5,3)[0])""","0")
  273.     t_render("""print(range(0,5,3)[1])""","3")
  274.     t_render("""print(range(0,5,3)[-1])""","3")
  275.  
  276.     t_render("""print(str(range(5,0,-3))[:5])""","<list")
  277.     t_render("""print(len(range(5,0,-3)))""","2")
  278.     t_render("""print(range(5,0,-3)[0])""","5")
  279.     t_render("""print(range(5,0,-3)[1])""","2")
  280.     t_render("""print(range(5,0,-3)[-1])""","2")
  281.  
  282.     t_render("""print(str(range(-8,-4))[:5])""","<list")
  283.     t_render("""print(len(range(-8,-4)))""","4")
  284.     t_render("""print(range(-8,-4)[0])""","-8")
  285.     t_render("""print(range(-8,-4)[1])""","-7")
  286.     t_render("""print(range(-8,-4)[-1])""","-5")
  287.  
  288.     t_render("""print(str(range(-4,-8,-1))[:5])""","<list")
  289.     t_render("""print(len(range(-4,-8,-1)))""","4")
  290.     t_render("""print(range(-4,-8,-1)[0])""","-4")
  291.     t_render("""print(range(-4,-8,-1)[1])""","-5")
  292.     t_render("""print(range(-4,-8,-1)[-1])""","-7")
  293.  
  294.     t_render("""print(str(range(-4,-8))[:5])""","<list")
  295.     t_render("""print(len(range(-4,-8)))""","0")
  296.  
  297.     t_render("""print(str(range(-8,-4,-1))[:5])""","<list")
  298.     t_render("""print(len(range(-8,-4,-1)))""","0")
  299.  
  300.     t_render("""print(str(range(0,4,0))[:5])""","<list")
  301.     t_render("""print(len(range(0,4,0)))""","0")
  302.  
  303.    
  304. if __name__ == '__main__':
  305.     t_render('print("hello world")',"hello world")
  306.     t_render('print(234)',"234")
  307.     t_render('a=3\nprint(a)',"3")
  308.     t_render('print(2+3)',"5")
  309.     t_render('print(4|1)',"5")
  310.     t_render('print(5&7)',"5")
  311.     t_render('print(2^7)',"5")
  312.     t_render('print(7^2&2)',"5")
  313.     t_render('print(7^2|4)',"5")
  314.     t_render('x=4\nx|=1\nprint(x)',"5")
  315.     t_render('x=5\nx&=7\nprint(x)',"5")
  316.     t_render('x=2\nx^=7\nprint(x)',"5")
  317.     t_render("""
  318. x = 2
  319. x += 3
  320. print(x)
  321. """
  322. ,"5")
  323.     t_render("""
  324. x = "OK"
  325. print(x)
  326. """
  327. ,"OK")
  328.     t_render("""
  329. a,b = 1,2
  330. print(a+b)
  331. """
  332. ,"3")
  333.     t_render("""
  334. x = 1
  335. if x == 1:
  336.    print("yes")
  337. ""","yes")
  338.     t_render("""
  339. if 0 == 1:
  340.    print("X")
  341. else:
  342.    print("OK")
  343. """
  344. ,"OK")
  345.    
  346.     t_render("""
  347. if 0 == 1:
  348.    print("X")
  349. elif 1 == 2:
  350.    print("Y")
  351. else:
  352.    print("OK")
  353. """
  354. ,"OK")
  355.  
  356.     t_render("""
  357. def test(x,y):
  358.    return x+y
  359. r = test(3,5)
  360. print(r)
  361. ""","8")
  362.     t_render("""
  363. x = 1
  364. t = 0
  365. while x<=5:
  366.    t = t+x
  367.    x = x+1
  368. print(t)
  369. ""","15")
  370.     t_render("""
  371. x = {}
  372. x.y = "test"
  373. print(x.y)
  374. ""","test")
  375.    
  376.     t_render("""
  377. if "a" is "a":
  378.    print("OK")
  379. """
  380. ,"OK")
  381.  
  382.     t_render("""
  383. if "a" is not "b":
  384.    print("OK")
  385. """
  386. ,"OK")
  387.  
  388.     t_render("""
  389. if None is None:
  390.    print("OK")
  391. """
  392. ,"OK")
  393.     t_render("""
  394. if "x" is "x" or "y" is "y":
  395.    print("OK")
  396. """
  397. ,"OK")
  398.     t_render("""
  399. x = 1
  400. while x < 3:
  401.    break
  402.    x = x + 1
  403. print(x)
  404. """
  405. ,"1")
  406.     t_render("""
  407. x = 1
  408. n = 0
  409. while x < 10:
  410.    x = x + 1
  411.    if n == 2:
  412.        continue
  413.    n = n + 1
  414. print(n)
  415. """
  416. ,"2")
  417.     t_render("""
  418. def test(x): return x
  419. y = test(1)*2 + test(3)*4 + test(5)*6
  420. print(y)
  421. """
  422. ,"44")
  423.     t_render("""
  424. def test(a,b): return a+b
  425. print(test(1,1)+test(1,1))
  426. """
  427. ,"4")
  428.  
  429.     t_render("""
  430. def test(): print("OK")
  431. x = test
  432. x()
  433. """
  434. ,"OK")
  435.  
  436.     t_render("""
  437. x = [2,4,6]
  438. print(x[1])
  439. """
  440. ,"4")
  441.     t_render("""
  442. def test(): print("OK")
  443. x = [1,test,2]
  444. x[1]()
  445. """
  446. ,"OK")
  447.  
  448.  
  449.     t_render("""
  450. z = 0
  451. for x in [1,2,3]:
  452.    z += x
  453. print(z)
  454. """
  455. ,"6")
  456.  
  457.     t_render("""
  458. z = 0
  459. for x in range(1,4):
  460.    z += x
  461. print(z)
  462. """
  463. ,"6")
  464.  
  465.     t_render("""
  466. x = {'a':'OK'}
  467. print(x.a)
  468. """
  469. ,"OK")
  470.  
  471.     t_render("""print("1234"[1:3])""","23")
  472.     t_render("""print("1234"[-3:3])""","23")
  473.     t_render("""print([1,2,"OK",4][-3:3][1])""","OK")
  474.     t_render("""
  475. n = 0
  476. for x in range(0,10,2):
  477.    n += 1
  478. print(n)
  479. """
  480. ,"5")
  481.  
  482.     t_render("""print(max(3,8,2,6))""","8")
  483.     t_render("""print(min(3,4,2,6))""","2")
  484.     t_render("""for k in {'OK':0}: print(k)""","OK")
  485.  
  486.     t_render("""
  487. X = "OK"
  488. def test(): print(X)
  489. test()
  490. """
  491. ,"OK")
  492.  
  493.     t_render("""
  494. a = 4
  495. def test(z):
  496.    for i in range(0,a):
  497.        z += i
  498.    return z
  499. print(test(1))
  500. """
  501. ,"7")
  502.  
  503.     t_render("""
  504. def test(self): print(self)
  505. fnc = bind(test,"OK")
  506. fnc()
  507. """
  508. ,"OK")
  509.  
  510.  
  511.     t_render("""
  512. x = [v*v for v in range(0,5)]
  513. print(x[3])
  514. """
  515. ,"9")
  516.     t_render("""
  517. t = [[y*10+x for x in range(0,10)] for y in range(0,10)]
  518. print(t[2][3])
  519. """
  520. ,"23")
  521.  
  522.     t_render("""
  523. x = [1]
  524. x.extend([2,3])
  525. print(x[1])
  526. """
  527. ,"2")
  528.  
  529.     #t_render("""
  530. #x = {'a':3}
  531. #merge(x,{'b':4})
  532. #print(x.b)
  533. #"""
  534. #,"4")
  535.  
  536.     t_render("""
  537. x = [1,2,3]
  538. y = copy(x)
  539. y[0] *= 10
  540. print(x[0]+y[0])
  541. """
  542. ,"11")
  543.     t_render("""
  544. x = {'a':3}
  545. y = copy(x)
  546. y.a *= 10
  547. print(x.a+y.a)
  548. """
  549. ,"33")
  550.     t_render("""
  551. x = {}
  552. y = x['x']
  553. """
  554. ,'KeyError', False)
  555.     t_render("""
  556. x = []
  557. y = x[1]
  558. """
  559. ,'KeyError', False)
  560.     t_render("""print("O"+"K")""","OK")
  561.     t_render("""print("-".join(["O","K"]))""","O-K")
  562.     t_render("""print("OK-OK".split("-")[1])""","OK")
  563.     t_render("""
  564. def test(*v): return max(v[2])
  565. print(test(*[1,2,"OK"]))
  566. """
  567. ,"OK")
  568.     t_render("""
  569. def test(**v): return v['x']
  570. print(test(**{'x':'OK'}))
  571. """
  572. ,"OK")
  573.     #t_render("""
  574. #def test(y='K',x='Z'): print(x+y)
  575. #test(x='O')
  576. #"""
  577. #,"OK")
  578.     t_render("""
  579. def test(y='K',x='Z'): print(x+y)
  580. test('O')
  581. """
  582. ,"ZO")
  583.  
  584.     #t_render("""
  585. #def test(a,b=2,*c,**d): return a+b+c[0]+d['x']+d['z']
  586. #print(test(10,3,z=50000,*[200],**{'x':4000}))
  587. #"""
  588. #,"54213")
  589.  
  590.     t_render("""print("".join(["O"]+["K"]))""","OK")
  591.     t_render("""x="OK";print(x)""","OK")
  592.     t_render("""x = [1,2,] ; print(x[1])""","2")
  593.     t_render("""a,b,d = [0],0,'OK'; print(d)""","OK")
  594.    
  595.     t_render("""
  596. def test():
  597.    raise
  598. try:
  599.    test()
  600. except:
  601.    print("OK")
  602. ""","OK")
  603.  
  604.     t_render("""print("OKx"[:-1])""","OK")
  605.     t_render("""print("xOK"[1:])""","OK")
  606.     t_render("""a,b = "OK"; print(a+b)""","OK")
  607.    
  608.     t_render("""
  609. def test(a,b):
  610.    print (a+b[2])
  611. test(1,3)
  612. ""","Exception",False)
  613.  
  614.     t_render("""
  615. def test(): raise
  616. test()
  617. ""","Exception",False)
  618.    
  619.     t_render(['OK="OK"',"import tmp1\nprint(tmp1.OK)"],"OK")
  620.    
  621.     t_render(['O="O"','K="K"',"import tmp1, tmp2\nprint(tmp1.O+tmp2.K)"],"OK")
  622.        
  623.     t_render("""
  624. def test(): return
  625. x = 1
  626. print(test())
  627. ""","None")
  628.  
  629.     t_render("""
  630. def test(): pass
  631. x = 1
  632. print(test())
  633. ""","None")
  634.  
  635.     t_render("""
  636. def test():
  637.    global x
  638.    x = "OK"
  639. test()
  640. print(x)
  641. ""","OK")
  642.  
  643.     t_render("print(len([1,2,3]))","3")
  644.    
  645.     t_render('if not "?" in "xyz": print("OK")',"OK")
  646.    
  647.     t_render('print({1:"OK"}[1])',"OK")
  648.    
  649.     t_render('print(len("\0"))',"1")
  650.    
  651.     t_render('print(1 in {1:2})',"1")
  652.    
  653.     t_render('x = {1:2}; del x[1]; print(len(x))','0')
  654.    
  655.     t_render("""
  656. def test(t):
  657.    t = "O"+t
  658.    print(t)
  659. test("K")
  660. ""","OK")
  661.  
  662.     t_render("""print([1,2,3].index(3))""","2")
  663.     t_render("""print("1,2,3".split(",").index("3"))""","2")
  664.    
  665.     t_render("""v = [3,2,1]; v.sort(); print(v[0])""","1")
  666.  
  667.     t_render("""print(abs(-5))""","5")
  668.     t_render("""print(int(1.234))""","1")
  669.    
  670.     t_render("print(int(round(1.5)))","2")
  671.     t_render("print(ord('X'))","88")
  672.     t_render("print(ord(chr(128)))","128")
  673.     #t_render("print(fsize('LICENSE.txt'))","181")
  674.     t_render("print(int('ff',16))","255")
  675.     t_render("""
  676. def test(x,y): print(x); return y
  677. test('a',1) or test('b',1) and test('c',0)
  678. ""","a")
  679.  
  680.     #t_render("def test(): print('OK')\n{'__call__':test}()","OK")
  681.  
  682.  
  683.     t_render("""
  684. def test():
  685.    def fnc():
  686.        print("OK")
  687.    fnc()
  688. test()
  689. ""","OK")
  690.  
  691.     t_render("""print("aa..bbb...ccc".replace("..","X"))""","aaXbbbX.ccc")
  692.     t_render("""print("..bbb..".replace("..","X"))""","XbbbX")
  693.     t_render("""print("234".replace("\r\n","\n"))""","234")
  694.     t_render("""print("a\0b".replace("\0","X"))""","aXb")
  695.     t_render("""x = "a\0b"; x = x.replace("\0","c"); print(x)""","acb")
  696.  
  697.     t_render("""print(0xff)""","255")
  698.    
  699.     t_render("""x=(1,3);print({x:'OK'}[x])""","OK")
  700.     t_render("""x=(1,3);y=(1,3);print({x:'OK'}[y])""","OK")
  701.     t_render("""print({(1,3):'OK'}[(1,3)])""","OK")
  702.     t_render("def test(): test()\ntest()","Exception", False)
  703.     t_render("x = []; x.append(x); print(x<x)","0");
  704.     t_render("x = []; x.append(x); print({x:'OK'}[x])","OK")
  705.     #t_render("print(float(str(4294967296))==float('4294967296'))","1")
  706.     t_render("print(2**3)","8")
  707.     #t_render("x = 'OK',\nprint(x[0])","OK")
  708.  
  709.     test_range()
  710.    
  711.     t_render(['v="OK"',"from tmp1 import *\nprint(v)"],"OK")
  712.     t_render(['v="OK"',"from tmp1 import v\nprint(v)"],"OK")
  713.     t_render(['x="X";y="K"',"x = 'O'\nfrom tmp1 import y\nprint(x+y)"],"OK")
  714.  
  715.     t_render("""
  716. def test(**e):
  717.    print(e['x'])
  718. test(x='OK')
  719. ""","OK")
  720.  
  721.     # test register allocator
  722.     s = "def f():pass\n"+("f()\n"*256)+"print('OK')"
  723.     t_render(s,"OK")
  724.    
  725.     t_render("print(2**3)","8")
  726.     t_render("print(2*3**2)", "18", False)
  727.    
  728.    
  729.     t_render("""
  730. def test(**v): return 'OK'
  731. print(test())
  732. """
  733. ,"OK")
  734.     t_render("""
  735. def test(**v):
  736.    v['x'] = 'OK'
  737.    return v
  738. print(test()['x'])
  739. """
  740. ,"OK")
  741.  
  742.     t_render("""
  743. def get(self,k):
  744.    return k+"K"
  745. v = object()
  746. v.__get__ = bind(get,v)
  747. print(v.O)
  748. """,
  749. "OK")
  750.  
  751.     t_render("""
  752. def set(self,k,v):
  753.    self = getraw(self)
  754.    self[k] = v + "K"
  755. v = object()
  756. v.__set__ = bind(set,v)
  757. v.x = "O"
  758. print(v.x)
  759. """,
  760. "OK")
  761.  
  762.     t_render("""
  763. def call(self,x):
  764.    print(x)
  765. v = object()
  766. v.__call__ = bind(call,v)
  767. v("OK")
  768. """
  769. ,"OK")
  770.  
  771.  
  772.     #a REG related test
  773.     t_render("""
  774. def test():
  775.    init = True
  776.    if init and True:
  777.        pass
  778. print("OK")
  779. ""","OK")
  780.  
  781.     meta_objs_init = """
  782. def my_new(klass,*p):
  783.    self = object()
  784.    setmeta(self,klass)
  785.    self.__init__(*p)
  786.    return self
  787.  
  788. def A_init(self,v):
  789.    if v: print("A_init")
  790. def A_test1(self):
  791.    print("A_test1")
  792. def A_test2(self):
  793.    print("A_test2")
  794. A = {'__new__':my_new,'__init__':A_init,'test1':A_test1,'test2':A_test2}
  795.  
  796. def B_init(self,v):
  797.    if v: print("B_init")
  798. def B_test2(self):
  799.    print("B_test2")
  800. B = {'__init__':B_init,'test2':B_test2}
  801. setmeta(B,A)
  802. """
  803.  
  804.     t_render(meta_objs_init+"""A(True)""","A_init")
  805.     t_render(meta_objs_init+"""A(False).test1()""","A_test1")
  806.     t_render(meta_objs_init+"""A(False).test2()""","A_test2")
  807.     t_render(meta_objs_init+"""B(True)""","B_init")
  808.     t_render(meta_objs_init+"""B(False).test1()""","A_test1")
  809.     t_render(meta_objs_init+"""B(False).test2()""","B_test2")
  810.  
  811.     #various class construct use tests
  812.     t_render("""
  813. class C:
  814.    def __init__(self,data): self.data = data
  815.    def print(self): print(self.data)
  816. C("OK").print()
  817. """
  818. ,"OK")
  819.  
  820.     t_render("""
  821. class X:
  822.    pass
  823. y = X()
  824. print("OK")
  825. ""","OK")
  826.  
  827.     t_render("""
  828. class X: pass
  829. def test(): y = X()
  830. test()
  831. print("OK")
  832. ""","OK")
  833.  
  834.     t_render(["class X: pass\ndef test(): y = X()","import tmp1\ntmp1.test();print('OK')"],"OK")
  835.  
  836.     t_render("""
  837. class A:
  838.    def __init__(self):
  839.        self.a = 'O'
  840.        self.b = 'x'
  841.    def test(self):
  842.        print("KO")
  843. class B(A):
  844.    def __init__(self):
  845.        A.__init__(self)
  846.        self.b = 'K'
  847.    def test(self):
  848.        print(self.a+self.b)
  849. B().test()
  850. ""","OK")
  851.  
  852.     t_render("""
  853. class A:
  854.    def test(self):
  855.        print(self)
  856. A.test("OK")
  857. ""","OK")
  858.  
  859.  
  860.     #test that you can make a callable object
  861.     t_render("""
  862. class Test:
  863.    def __init__(self,v):
  864.        self.value = v
  865.    def __call__(self):
  866.        print(self.value)
  867.  
  868. x = Test('OK')
  869. x()
  870. ""","OK")
  871.  
  872.     #test that you can use a __get__
  873.     t_render("""
  874. class Test:
  875.    def __get__(self,k):
  876.        return k+"K"
  877. x = Test()
  878. print(x.O)
  879. ""","OK")
  880.     #test that you can use __set__
  881.     t_render("""
  882. class Test:
  883.    def __set__(self,k,v):
  884.        getraw(self)[k] = "O"+v
  885. x = Test()
  886. x.v = "K"
  887. print(x.v)
  888. ""","OK")
  889.  
  890.     #test that exceptions are cleared after they are caught
  891.     #and not repeated
  892.     t_render("""
  893. def test():
  894.    try:
  895.        pass
  896.    except:
  897.        pass
  898.    print("OK")
  899.    raise
  900. try:
  901.    test()
  902. except:
  903.    pass
  904. ""","OK")
  905.  
  906.     #check that missing attributes throw an error
  907.     t_render("""
  908. class A: pass
  909. try:
  910.    A().x
  911. except:
  912.    print('OK')
  913. ""","OK")
  914.  
  915.     #check that a changed attribute gets changed
  916.     t_render("""
  917. class A:
  918.    def x(self): pass
  919. a = A()
  920. a.x = "OK"
  921. print(a.x)
  922. ""","OK")
  923.    
  924.     #test that you can use a __get__ gets inherited
  925.     t_render("""
  926. class A:
  927.    def __get__(self,k):
  928.        return k+"K"
  929. class B(A):
  930.    pass
  931. x = B()
  932. print(x.O)
  933. ""","OK")
  934.  
  935.     #test that meta methods aren't called on non-objects
  936.     t_render("""
  937. def get(): pass
  938. x = {"__get__":get}
  939. try:
  940.    z = x.y
  941. except:
  942.    print("OK")
  943. ""","OK")
  944.  
  945.     #test that meta stuff is inheritited in dicts
  946.     t_render("""
  947. x = {1:"O"}
  948. y = {2:"K"}
  949. setmeta(y,x)
  950. print(y[1]+y[2])
  951. ""","OK")
  952.  
  953.     #test that meta stuff doesn't change into methods in dicts
  954.     t_render("""
  955. def get(k): return k
  956. x = {"get":get}
  957. print(x.get("OK"))
  958. ""","OK")
  959.  
  960.     #tests issue 14: string.index() should give an exception if substring not found
  961.     t_render("""
  962. try:
  963.    "test-".index("=")
  964. except:
  965.    print("OK")
  966. ""","OK")
  967.  
  968.     #issue 19: test that multiplying a string with a negative value returns an empty string
  969.     t_render("""
  970. foo = "abc" * -1
  971. print(foo)
  972. """, "")
  973.  
  974.     #issue 18:  tests that strings containing NUL chars are printed correctly
  975.     t_render("""
  976. foo = "abc" + chr(0) + "d"
  977. print(foo)
  978. """, "abc" + chr(0) + "d")
  979.  
  980.     #issue 18 (related): tests that "".strip() treats strings containing NUL chars correctly
  981.     t_render("""
  982. foo = "abc" + chr(0) + "d\n"
  983. print(foo.strip())
  984. """, "abc" + chr(0) + "d")
  985.  
  986.     #test that class variables work as expected
  987.     t_render("""
  988. class A:
  989.    foo = 42
  990. s = str(A.foo)
  991. A.foo += 1
  992. s += str(A.foo)
  993. print(s)
  994. """, "4243")
  995.  
  996.     #check that class variables are not leaked to the global scope
  997.     t_render("""
  998. class A:
  999.    foo = 42
  1000. print(foo)
  1001. """, "KeyError", False)
  1002.  
  1003.     #test that class variables can correctly be accessed by a subclass
  1004.     t_render("""
  1005. class A:
  1006.    foo = "OK"
  1007. class B(A):
  1008.    pass
  1009. print(B.foo)
  1010. """, "OK")
  1011.  
  1012.     #test that class variables can be accessed from instances
  1013.     t_render("""
  1014. class A:
  1015.    foo = "OK"
  1016. o = A()
  1017. print(o.foo)
  1018. """, "OK")
  1019.  
  1020.     #test case for possible register allocation bug #22
  1021.     t_render("""
  1022. x = [1, 2, 3]
  1023.  
  1024. for i in x:
  1025.    if i != 0 and i:
  1026.        y = "OK"
  1027. print(y)
  1028. ""","OK")
  1029.  
  1030.     #test that sandbox() raises an exception when the time limit is passed
  1031.     if SANDBOX:
  1032.         t_render("""
  1033. sandbox(1, False)
  1034. while True:
  1035.    pass
  1036. """, "SandboxError", False)
  1037.  
  1038.     #test that calling sandbox() removes the sandbox builtin
  1039.     if SANDBOX:
  1040.         t_render("""
  1041. sandbox(500, False)
  1042. try:
  1043.    sandbox(200)
  1044. except:
  1045.    print("OK")
  1046. """, "OK")
  1047.  
  1048.     #test that sandbox() raises an exception when the memory limit is passed
  1049.     if SANDBOX:
  1050.         t_render("""
  1051. sandbox(False, 1)
  1052. a = 42
  1053. """, "SandboxError", False)
  1054.  
  1055.     #test that circular inheritance doesn't cause an infinite lookup chain
  1056.     t_render("""
  1057. class A:
  1058.    pass
  1059.  
  1060. class B:
  1061.    pass
  1062.  
  1063. setmeta(A, B)
  1064. setmeta(B, A)
  1065.  
  1066. foo = A()
  1067. print("OK")
  1068. """, "tp_lookup",False)
  1069.  
  1070.  
  1071.     #tests issue #20: test that string multiplication is commutative
  1072.     t_render("""
  1073. foo = "O"
  1074. bar = "K"
  1075. print(3 * foo, bar * 3)
  1076. """, "OOO KKK")
  1077.  
  1078.     #test issue #27: that the __main__ module doesn't get GC'd
  1079.     t_render("""
  1080. MODULES["__main__"] = None
  1081. for n in range(0,50000):
  1082.    x = [n]
  1083. print("OK")
  1084. ""","OK")
  1085.  
  1086.     #test case for UnboundLocalError - bug #16
  1087.     t_unrender("""
  1088. def foo():
  1089.    print(v)
  1090.    v = "ERROR"
  1091. """)
  1092.  
  1093.     #test for segfault on large split
  1094.     t_render("""
  1095. x = " ".join([str(n) for n in range(0,50000)])
  1096. y = x.split("1")
  1097. print("OK")
  1098. ""","OK")
  1099.  
  1100.     #test for Issue 42: 'not' operator only works for numbers,
  1101.     #not dicts, lists, or strings
  1102.     #Reported by kiwidrew, Apr 08, 2009
  1103.     #see also: http://code.google.com/p/tinypy/issues/detail?id=42
  1104.     t_render("""
  1105. if not None:
  1106.    print('OK')
  1107. """, "OK")
  1108.  
  1109.     t_render("""
  1110. n = 0
  1111. if not n:
  1112.    print('OK')
  1113. ""","OK")
  1114.  
  1115.     t_render("""
  1116. d = {}
  1117. if not d:
  1118.    print('OK')
  1119. ""","OK")
  1120.  
  1121.     t_render("""
  1122. l = []
  1123. if not l:
  1124.    print('OK')
  1125. ""","OK")
  1126.  
  1127.     t_render("""
  1128. s = ''
  1129. if not s:
  1130.    print('OK')
  1131. ""","OK")
  1132.  
  1133. ################################################################################
  1134.  
  1135. def t_boot(ss,ex,exact=True):
  1136.     if not istype(ss,'list'): ss =[ss]
  1137.     n = 1
  1138.     for s in ss:
  1139.         fname = 'tmp'+str(n)+'.tpc'
  1140.         system_rm(fname)
  1141.         fname = 'tmp'+str(n)+'.py'
  1142.         save(fname,s)
  1143.         n += 1
  1144.     system_rm('tmp.txt')
  1145.     #system(TINYPY+fname+' > tmp.txt')
  1146.     system("../build/tinypy "+fname+' > tmp.txt')
  1147.     res = load(TMP).strip()
  1148.     print(ss,ex,res)
  1149.     if exact: assert(res == ex)
  1150.     else: assert(ex in res)
  1151.  
  1152. is_boot = False
  1153. try:
  1154.     assert(is_tinypy == True)
  1155.     x = compile('x=3','')
  1156.     is_boot = True
  1157. except:
  1158.     pass
  1159.  
  1160. def t_api(ss_py,ss,ex):
  1161.     if not '-linux' in ARGV:
  1162.         return
  1163.    
  1164.     #first verify that the python code produces the result
  1165.     t_render(ss_py,ex)
  1166.    
  1167.     #then verify that the C code does ...
  1168.     fname = "tmp.c"
  1169.     system_rm("tmp.c")
  1170.     system_rm("tmp")
  1171.     system_rm(TMP)
  1172.     save(fname,ss)
  1173.     system("gcc tmp.c -Wall -g -lm -o tmp")
  1174.     cmd = "./tmp > "+TMP
  1175.     system(cmd)
  1176.     res = load(TMP).strip()
  1177.     if res != ex: showerror(cmd, ss, ex, res)
  1178.     assert(res == ex)
  1179.  
  1180. if is_boot == True and __name__ == '__main__':
  1181.     print("# t_boot")
  1182.     t_boot(["def test(): print('OK')","import tmp1; tmp1.test()"],"OK")
  1183.  
  1184.     print("# t_api")
  1185.    
  1186.     # all t_api examples include a python equivalent
  1187.     # also include a brief explanation of the point
  1188.     # of the example
  1189.    
  1190.     # just a "hello world" style example
  1191.     t_api("""
  1192. print ("OK")
  1193. ""","""
  1194. #include "tp.c"
  1195. int main(int argc, char *argv[]) {
  1196.    tp_vm *tp = tp_init(argc,argv);
  1197.    
  1198.    tp_params_v(tp,1,tp_string("OK"));
  1199.    tp_print(tp);
  1200.    
  1201.    tp_deinit(tp);
  1202.    return(0);
  1203. }
  1204. """,
  1205. "OK")
  1206.  
  1207.     # how to create a c function and call it
  1208.     t_api("""
  1209. def test(v):
  1210.    print(v)
  1211. test("OK")
  1212. ""","""
  1213. #include "tp.c"
  1214. tp_obj test(TP) {
  1215.    tp_obj v = TP_OBJ();
  1216.    tp_params_v(tp,1,v);
  1217.    tp_print(tp);
  1218.    return tp_None;
  1219. }
  1220. int main(int argc, char *argv[]) {
  1221.    tp_vm *tp = tp_init(argc,argv);
  1222.    
  1223.    tp_obj fnc = tp_fnc(tp,test);
  1224.    tp_call(tp,fnc,tp_params_v(tp,1,tp_string("OK")));
  1225.    
  1226.    tp_deinit(tp);
  1227.    return(0);
  1228. }
  1229. """,
  1230. "OK")
  1231.  
  1232.     # how to create a simple class
  1233.     t_api("""
  1234. class A:
  1235.    def __init__(self,value):
  1236.        self.value = value
  1237.    def test(self):
  1238.        print(self.value)
  1239. A("OK").test()
  1240. ""","""
  1241.  
  1242. #include "tp.c"
  1243. tp_obj A_init(TP) {
  1244.    tp_obj self = TP_TYPE(TP_DICT);
  1245.    tp_obj v = TP_OBJ();
  1246.    tp_set(tp,self,tp_string("value"),v);
  1247.    return tp_None;
  1248. }
  1249. tp_obj A_test(TP) {
  1250.    tp_obj self = TP_TYPE(TP_DICT);
  1251.    tp_obj v = tp_get(tp,self,tp_string("value"));
  1252.    tp_params_v(tp,1,v);
  1253.    tp_print(tp);
  1254.    return tp_None;
  1255. }
  1256. int main(int argc, char *argv[]) {
  1257.    tp_vm *tp = tp_init(argc,argv);
  1258.    
  1259.    /* create our class */
  1260.    tp_obj tmp;
  1261.    tp_obj A = tp_class(tp);
  1262.    tp_set(tp,A,tp_string("__init__"),tp_fnc(tp,A_init));
  1263.    tp_set(tp,A,tp_string("test"),tp_fnc(tp,A_test));
  1264.    
  1265.    /* instantiate it and call test */
  1266.    tmp = tp_call(tp,A,tp_params_v(tp,1,tp_string("OK")));
  1267.    tp_call(tp,tp_get(tp,tmp,tp_string("test")),tp_params_v(tp,0));
  1268.    
  1269.    tp_deinit(tp);
  1270.    return(0);
  1271. }
  1272. """,
  1273. "OK")
  1274.  
  1275. ################################################################################
  1276.  
  1277. def unformat(x):
  1278.     x = x.split(' ')
  1279.     r = []
  1280.     for i in x:
  1281.         if i != ':':
  1282.             if i:
  1283.                 r.append(i)
  1284.     return " ".join(r)
  1285.  
  1286. def t_asm(ass, ex, exact=True,check_dis=True):
  1287.     ass = ass.strip()
  1288.     bc = asm.assemble(ass)
  1289.     dis = disasm.disassemble(bc)
  1290.     dis = unformat(dis)
  1291.     if check_dis and dis != ass:
  1292.         print (ass)
  1293.         print (dis)
  1294.         assert(dis == ass)
  1295.     fname = "tmp.tpc"
  1296.     system_rm(fname)
  1297.     system_rm(TMP)
  1298.     save(fname,bc)
  1299.     cmd = VM + fname + " > " + TMP
  1300.     system(cmd)
  1301.     res = load("tmp.txt").strip()
  1302.     if exact:
  1303.         if ex != res:
  1304.             print (ass)
  1305.             print (ex)
  1306.             print (res)
  1307.             assert(ex == res)
  1308.     else:
  1309.         if ex not in res:
  1310.             print (ass)
  1311.             print (ex)
  1312.             print (res)
  1313.             assert(ex in res)
  1314.    
  1315. if is_boot == True and __name__ == '__main__':
  1316.     print("# t_asm")
  1317.    
  1318.     t_asm("""
  1319. NUMBER 0 0 0 42
  1320. DEBUG 0 0 0
  1321. EOF 0 0 0
  1322. """, "DEBUG: 0 42")
  1323.  
  1324.     t_asm("""
  1325. STRING 0 0 1 "a"
  1326. NUMBER 1 0 0 41
  1327. GSET 0 1 0
  1328. STRING 2 0 1 "a"
  1329. GGET 1 2 0
  1330. NUMBER 2 0 0 1
  1331. ADD 1 1 2
  1332. GSET 0 1 0
  1333. STRING 2 0 5 "print"
  1334. GGET 1 2 0
  1335. STRING 3 0 1 "a"
  1336. GGET 2 3 0
  1337. PARAMS 0 2 1
  1338. CALL 0 1 0
  1339. EOF 0 0 0
  1340. """, "42")
  1341.  
  1342.     t_asm("""
  1343. STRING 0 0 4 "str1"
  1344. STRING 1 0 3 "foo"
  1345. GSET 0 1 0
  1346. STRING 0 0 4 "str2"
  1347. STRING 1 0 3 "bar"
  1348. GSET 0 1 0
  1349. STRING 2 0 5 "print"
  1350. GGET 1 2 0
  1351. STRING 3 0 4 "str1"
  1352. GGET 2 3 0
  1353. STRING 4 0 4 "str2"
  1354. GGET 3 4 0
  1355. ADD 2 2 3
  1356. PARAMS 0 2 1
  1357. CALL 0 1 0
  1358. EOF 0 0 0
  1359. """, "foobar")
  1360.  
  1361.     t_asm("""
  1362. STRING 0 0 3 "foo"
  1363. STRING 1 0 3 "bar"
  1364. NUMBER 2 0 0 1
  1365. IF 2 0 0
  1366. ADD 0 0 1
  1367. STRING 1 0 5 "print"
  1368. GGET 3 1 0
  1369. PARAMS 2 0 1
  1370. CALL 0 3 2
  1371. EOF 0 0 0
  1372. """, "foo");
  1373.  
  1374.     t_asm("""
  1375. NUMBER 0 0 0 1
  1376. NUMBER 1 0 0 2
  1377. JUMP 0 0 2
  1378. ADD 0 0 1
  1379. DEBUG 0 0 0
  1380. EOF 0 0 0
  1381. """, "DEBUG: 0 1");
  1382.  
  1383.     t_asm("""
  1384. STRING 0 0 3 "foo"
  1385. NUMBER 1 0 0 3
  1386. MUL 0 0 1
  1387. DEBUG 0 0 0
  1388. EOF 0 0 0
  1389. """, "DEBUG: 0 foofoofoo");
  1390.  
  1391.     t_asm("""
  1392. NUMBER 0 0 0 1
  1393. NUMBER 0 0 0 42
  1394. LIST 0 0 2
  1395. GET 0 0 1
  1396. DEBUG 0 0 0
  1397. EOF 0 0 0
  1398. """, "DEBUG: 0 42");
  1399.  
  1400.     t_asm("""
  1401. NUMBER 0 0 0 1
  1402. NUMBER 1 0 0 1
  1403. NUMBER 2 0 0 1
  1404. LIST 0 0 3
  1405. LEN 0 0 0
  1406. DEBUG 0 0 0
  1407. EOF 0 0 0
  1408. """, "DEBUG: 0 3");
  1409.  
  1410.     t_asm("""
  1411. DEF 0 0 13
  1412. STRING 1 0 3 "foo"
  1413. NAME 1 0 0
  1414. STRING 3 0 5 "print"
  1415. GGET 2 3 0
  1416. STRING 3 0 3 "foo"
  1417. PARAMS 1 3 1
  1418. CALL 1 2 1
  1419. EOF 0 0 0
  1420. PARAMS 1 0 0
  1421. CALL 1 0 1
  1422. EOF 0 0 0
  1423. """, "foo");
  1424.  
  1425.     #test that function definitions longer than the bytecode are properly sanitized
  1426.     if SANDBOX:
  1427.         t_asm("""
  1428. DEF 0 0 100
  1429. REGS 2 0 0
  1430. STRING 1 0 3 "foo"
  1431. NAME 1 0 0
  1432. PASS 0 0 0
  1433. EOF 0 0 0
  1434. """, "SandboxError", False)
  1435.  
  1436.     #test that negative out of bounds jumps are sanitized
  1437.     if SANDBOX:
  1438.         t_asm("""
  1439. JUMP 0 127 255
  1440. EOF 0 0 0
  1441. """, "SandboxError", False)
  1442.    
  1443.     #test that positive out of bounds jumps are sanitized
  1444.     if SANDBOX:
  1445.         t_asm("""
  1446. JUMP 0 128 0
  1447. EOF 0 0 0
  1448. """, "SandboxError", False)
  1449.  
  1450.     #test that strings with boundaries beyond the end of the bytecode are properly sanitized
  1451.     if SANDBOX:
  1452.         t_asm("""
  1453. STRING 1 0 100 "foobar"
  1454. EOF 0 0 0
  1455. """, "SandboxError", False,False)
  1456.  
  1457.