Skip to content

Commit 23cce00

Browse files
authored
Fix Python build check script. (krahets#911)
* Fix Python build check script. * Update python code check workflow * Update python test script. * Compatible with Python >=3.9 * Compatible with Python >=3.10 * Fix errors in Windows * test * Add Python version in installation.md * Update test_all.py
1 parent 459697a commit 23cce00

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

.github/workflows/python.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
strategy:
2323
matrix:
2424
os: [ubuntu-latest, macos-latest, windows-latest]
25-
python-version: ["3.8", "3.10"]
25+
python-version: ["3.10", "3.11"]
2626
steps:
2727
- uses: actions/checkout@v4
2828
- name: Set up Python ${{ matrix.python-version }}
@@ -36,6 +36,6 @@ jobs:
3636
- name: Lint with black
3737
run: |
3838
black codes/python
39-
- name: Build with py_compile
39+
- name: Test python code
4040
run: |
41-
python codes/python/build.py
41+
python codes/python/test_all.py

codes/python/build.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

codes/python/test_all.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import glob
3+
import subprocess
4+
5+
env = os.environ.copy()
6+
env["PYTHONIOENCODING"] = "utf-8"
7+
8+
if __name__ == "__main__":
9+
# find source code files
10+
src_paths = sorted(glob.glob("codes/python/chapter_*/*.py"))
11+
errors = []
12+
13+
# run python code
14+
for src_path in src_paths:
15+
process = subprocess.Popen(
16+
["python", src_path],
17+
stdout=subprocess.PIPE,
18+
stderr=subprocess.PIPE,
19+
text=True,
20+
env=env,
21+
)
22+
# Wait for the process to complete, and get the output and error messages
23+
stdout, stderr = process.communicate()
24+
# Check the exit status
25+
exit_status = process.returncode
26+
if exit_status != 0:
27+
errors.append(stderr)
28+
29+
print(f"Tested {len(src_paths)} files")
30+
print(f"Found exception in {len(errors)} files")
31+
if len(errors) > 0:
32+
raise RuntimeError("\n\n".join(errors))

docs/chapter_appendix/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
### Python 环境
1919

20-
1. 下载并安装 [Miniconda3](https://docs.conda.io/en/latest/miniconda.html)
20+
1. 下载并安装 [Miniconda3](https://docs.conda.io/en/latest/miniconda.html) ,需要 Python 3.10 或更新版本
2121
2. 在 VSCode 的插件市场中搜索 `python` ,安装 Python Extension Pack 。
2222
3. (可选)在命令行输入 `pip install black` ,安装代码格式化工具。
2323

0 commit comments

Comments
 (0)