From ecfd5d864675984e59106b478e8698e4a6b5e614 Mon Sep 17 00:00:00 2001
From: Joseph Weston <joseph.weston08@gmail.com>
Date: Sat, 24 Oct 2015 18:25:14 +0200
Subject: [PATCH] parse the version file instead of importing

Python 3 no longer allows implicit relative imports:
https://docs.python.org/release/3.0.1/whatsnew/3.0.html#removed-syntax
Instead of importing the version file we just parse it. By keeping
the extension ".py" we make it so that it will be distributed along
with the rest of the package.
---
 kwant/_common.py        | 13 +++++++++++--
 kwant/_kwant_version.py |  2 +-
 setup.py                |  7 +------
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/kwant/_common.py b/kwant/_common.py
index b9ea7aec..0015050b 100644
--- a/kwant/_common.py
+++ b/kwant/_common.py
@@ -11,7 +11,9 @@ import os
 
 __all__ = ['version', 'KwantDeprecationWarning']
 
-distr_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+package_root = os.path.dirname(os.path.realpath(__file__))
+distr_root = os.path.dirname(package_root)
+version_file = '_kwant_version.py'
 
 def get_version_from_git():
     try:
@@ -66,7 +68,14 @@ def get_version_from_git():
 
 
 
-from _kwant_version import version
+# version file contains comments (lines starting with '#')
+# followed by the version string on its own on a single line
+with open(os.path.join(package_root, version_file), 'r') as f:
+    cruft="\"'\n\t\r "  # strip whitespace and quotes
+    line = next(f).strip(cruft)
+    while line.startswith('#'):
+        line = next(f).strip(cruft)
+    version = line
 version_is_from_git = (version == "__use_git__")
 if version_is_from_git:
     version = get_version_from_git()
diff --git a/kwant/_kwant_version.py b/kwant/_kwant_version.py
index 9530ac99..ddd1a398 100644
--- a/kwant/_kwant_version.py
+++ b/kwant/_kwant_version.py
@@ -1,4 +1,4 @@
 # This file will be overwritten by setup.py when a source or binary
 # distribution is made.  The magic value "__use_git__" is interpreted by
 # _common.py in this directory.
-version = "__use_git__"
+"__use_git__"
diff --git a/setup.py b/setup.py
index 9782a484..c1b3847d 100755
--- a/setup.py
+++ b/setup.py
@@ -47,11 +47,6 @@ TUT_HIDDEN_PREFIX = '#HIDDEN'
 # it is not built yet.
 _dont_write_bytecode_saved = sys.dont_write_bytecode
 sys.dont_write_bytecode = True
-try:
-    imp.load_source(STATIC_VERSION_PATH[-1].split('.')[0],
-                    os.path.join(*STATIC_VERSION_PATH))
-except IOError:
-    pass
 _common = imp.load_source('_common', 'kwant/_common.py')
 sys.dont_write_bytecode = _dont_write_bytecode_saved
 
@@ -228,7 +223,7 @@ def write_version(fname):
         pass
     with open(fname, 'w') as f:
         f.write("# This file has been created by setup.py.\n")
-        f.write("version = '{}'\n".format(version))
+        f.write(version)
 
 
 def long_description():
-- 
GitLab