Skip to content

Commit 66854ea

Browse files
committed
Merge branch 'master' into release/4.6.0
2 parents 7e4a18a + 61a3fa2 commit 66854ea

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

setup.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
_VERSION_FILENAME = os.path.join(PKG_PATHNAME, '_version.py')
2323

2424

25+
def read_version_py(path):
26+
""" Read a _version.py file in a safe way. """
27+
with open(path, 'r') as fp:
28+
code = compile(fp.read(), 'chaco._version', 'exec')
29+
context = {}
30+
exec(code, context)
31+
return context['git_revision'], context['full_version']
32+
33+
2534
def git_version():
2635
""" Parse version information from the current git commit.
2736
@@ -85,17 +94,19 @@ def write_version_py(filename=_VERSION_FILENAME):
8594
# write_version_py(), otherwise the import of _version messes
8695
# up the build under Python 3.
8796
fullversion = VERSION
97+
chaco_version_path = os.path.join(
98+
os.path.dirname(__file__), 'chaco', '_version.py')
8899
if os.path.exists('.git'):
89100
git_rev, dev_num = git_version()
90101
elif os.path.exists(filename):
91102
# must be a source distribution, use existing version file
92103
try:
93-
from chaco._version import git_revision as git_rev
94-
from chaco._version import full_version as full_v
95-
except ImportError:
96-
msg = ("Unable to import 'git_revision' or 'full_revision'. "
97-
"Try removing {} and the build directory before building.")
98-
raise ImportError(msg.format(_VERSION_FILENAME))
104+
git_revision, full_version = read_version_py(chaco_version_path)
105+
except (SyntaxError, KeyError):
106+
raise RuntimeError("Unable to read git_revision. Try removing "
107+
"chaco/_version.py and the build directory "
108+
"before building.")
109+
99110

100111
match = re.match(r'.*?\.dev(?P<dev_num>\d+)', full_v)
101112
if match is None:

0 commit comments

Comments
 (0)