Skip to content

Commit ec6acea

Browse files
committed
Replace use of exec with runpy
`setup.py` uses `exec` to read the version number from the version file. It's generally considered best practice to avoid `exec` because of risks it carries. In this particular case, we can borrow the technique used in Cirq's `setup.py`, and use the Python `runpy` package.
1 parent 7932f78 commit ec6acea

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616
import re
17+
import runpy
1718
import sys
1819
import shutil
1920
import platform
@@ -131,8 +132,9 @@ def build_extension(self, ext):
131132
# README file as long_description.
132133
long_description = open("README.md", encoding="utf-8").read()
133134

134-
__version__ = ""
135-
exec(open("qsimcirq/_version.py").read())
135+
__version__ = runpy.run_path("qsimcirq/_version.py")["__version__"]
136+
if not __version__:
137+
raise ValueError("Version string cannot be empty")
136138

137139
setup(
138140
name="qsimcirq",

0 commit comments

Comments
 (0)