pypy有時候執行python程式錯誤

lt發表於2016-09-19
#py15.py
import sys

def r(a):
  i = a.find('0')
  if i == -1:
    sys.exit(a)

  excluded_numbers = set()

  for j in range(i/9*9,i/9*9+9): #same row
    if j!=i:
      excluded_numbers.add(a[j])

  for j in range(0*9+i%9,9*9+i%9,9): #same col
    if j!=i:
      excluded_numbers.add(a[j])

  r0=i/27*3  #blk 1st row
  c0=i%9/3*3 #blk 1st col

  for j in range(9): #same blk
    if (r0+j/3)*9+c0+j%3!=i:
      excluded_numbers.add(a[(r0+j/3)*9+c0+j%3])

  for m in '123456789':
    if m not in excluded_numbers:
      r(a[:i]+m+a[i+1:])

if __name__ == '__main__':
  if len(sys.argv) == 2 and len(sys.argv[1]) == 81:
    r(sys.argv[1])
  else:
    print ("Usage: python sudoku.py puzzle")

同樣的程式在python 2.7中能正確完成。

D:\pypy>timer pypy py15.py 800000000003600000070090200050007000000045700000100030001000068008500010090000400

Timer 3.01  Copyright (c) 2002-2003 Igor Pavlov  2003-07-10
debug: OperationError:
debug:  operror-type: OverflowError
debug:  operror-value: long int too large to convert to int

Kernel Time  =     0.093 = 00:00:00.093 =   0%
User Time    =     0.187 = 00:00:00.187 =   1%
Process Time =     0.280 = 00:00:00.280 =   2%
Global Time  =    11.169 = 00:00:11.169 = 100%

D:\pypy>timer python py15.py 800000000003600000070090200050007000000045700000100030001000068008500010090000400

Timer 3.01  Copyright (c) 2002-2003 Igor Pavlov  2003-07-10
812753649943682175675491283154237896369845721287169534521974368438526917796318452

Kernel Time  =     0.078 = 00:00:00.078 =   5%
User Time    =     0.452 = 00:00:00.452 =  32%
Process Time =     0.530 = 00:00:00.530 =  38%
Global Time  =     1.389 = 00:00:01.389 = 100%

知道錯在哪裡了。根據文件
sys.exit([arg])
Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

python的sys.exit()可以輸出錯誤資訊,貌似pypy的這個函式只能填寫短整數。把上述程式的

  if i == -1:
    sys.exit(a)

改為:

  if i == -1:
    print(a)
    sys.exit(-1)

就能正常執行了。

D:\pypy2>\timer pypy  sd0v2p.py 800000000003600000070090200050007000000045700000100030001000068008500010090000400
Timer 9.01 : Igor Pavlov : Public domain : 2009-05-31
812753649943682175675491283154237896369845721287169534521974368438526917796318452

Kernel Time  =     0.031 =    6%
User Time    =     0.436 =   85%
Process Time =     0.468 =   91%
Global Time  =     0.511 =  100%

相關文章