ベスパリブ

プログラミングを主とした日記・備忘録です。ベスパ持ってないです。

Python3.7のeval()関数は長すぎる文字列を処理させようとするとエラーを吐かずに落ちることがある

TL;DR

Python3.7系だと、エラーを吐かずに終了することがあります。

現象

a = "1*"*100000
ans = eval(a[:-1])

print(ans)

print("end.")

自宅のPC(Windows10, 64bit, Python3.6.5)でこれを実行してみると、

>python hoge.py
Traceback (most recent call last):
  File "141/hoge.py", line 5, in <module>
    ans = eval(a[:-1])
RecursionError: maximum recursion depth exceeded during compilation

RecursionErrorが発生しました。

これをノートPC(Windows10, 64bit, Python3.7.1)で実行してみると、

>python hoge.py

>

エラーも吐かずに終了しました。「end.」も出力してないので、途中で落ちているっぽいです。

考えられる原因

  • 私のノートPCが原因
  • Pythonのバージョン違いによる挙動
  • 搭載メモリ容量の違いによる挙動
  • その他

ノートPCがPython3.7.1なので、とりあえず自宅デスクトップPCにPython3.7.1環境を構築して実験してみます。

> conda activate
(base) > conda create -n py371 python=3.7.1
Collecting package metadata: done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.6.8
  latest version: 4.7.11

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: C:\Users\takey\Anaconda3\envs\py371

  added / updated specs:
    - python=3.7.1


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2019.5.15  |                1         166 KB
    certifi-2019.6.16          |           py37_1         156 KB
    openssl-1.1.1d             |       he774522_0         5.7 MB
    pip-19.2.2                 |           py37_0         1.9 MB
    python-3.7.1               |       h8c8aaf0_6        17.7 MB
    setuptools-41.0.1          |           py37_0         680 KB
    sqlite-3.29.0              |       he774522_0         962 KB
    vs2015_runtime-14.16.27012 |       hf0eaf9b_0         2.4 MB
    wheel-0.33.4               |           py37_0          57 KB
    ------------------------------------------------------------
                                           Total:        29.7 MB

The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/win-64::ca-certificates-2019.5.15-1
  certifi            pkgs/main/win-64::certifi-2019.6.16-py37_1
  openssl            pkgs/main/win-64::openssl-1.1.1d-he774522_0
  pip                pkgs/main/win-64::pip-19.2.2-py37_0
  python             pkgs/main/win-64::python-3.7.1-h8c8aaf0_6
  setuptools         pkgs/main/win-64::setuptools-41.0.1-py37_0
  sqlite             pkgs/main/win-64::sqlite-3.29.0-he774522_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_0
  wheel              pkgs/main/win-64::wheel-0.33.4-py37_0
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py37_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
setuptools-41.0.1    | 680 KB    | ######################################################################## | 100%  
python-3.7.1         | 17.7 MB   | ######################################################################## | 100%  
vs2015_runtime-14.16 | 2.4 MB    | ######################################################################## | 100% 
wheel-0.33.4         | 57 KB     | ######################################################################## | 100% 
pip-19.2.2           | 1.9 MB    | ######################################################################## | 100%  
openssl-1.1.1d       | 5.7 MB    | ######################################################################## | 100%  
ca-certificates-2019 | 166 KB    | ######################################################################## | 100%  
certifi-2019.6.16    | 156 KB    | ######################################################################## | 100% 
sqlite-3.29.0        | 962 KB    | ######################################################################## | 100%  
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate py371
#
# To deactivate an active environment, use
#
#     $ conda deactivate

(base) > conda activate py371
(py371) > python --version
Python 3.7.1

これでコードを実行してみます。

(py371) >python hoge.py

(py371) >

再現しました。原因はPythonのバージョンっぽいです。

Pythonのバージョンを色々変更して試した結果、Python3.6.9はRecursionErrorを吐き出し、Python3.7.0からエラーを吐かずに落ちました。

Python3.8系では直ってるのかわからないですけど、Python3.7系でeval()関数を使うときは注意しましょう。

参考