Skip to content

Commit 651c84a

Browse files
committed
REL: make setup.py compatible with Python 2 and 3.
Use version-independent import of RawConfigParser.
1 parent 666a876 commit 651c84a

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

setup.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
"""
99

1010
import os
11+
import re
12+
import sys
1113
from setuptools import setup, find_packages
1214

1315
# Use this version when git data are not available, like in git zip archive.
1416
# Update when tagging a new release.
1517
FALLBACK_VERSION = '1.3.post0'
1618

19+
# determine if we run with Python 3.
20+
PY3 = (sys.version_info[0] == 3)
21+
1722
# versioncfgfile holds version data for git commit hash and date.
1823
# It must reside in the same directory as version.py.
1924
MYDIR = os.path.dirname(os.path.abspath(__file__))
@@ -22,7 +27,7 @@
2227

2328
def gitinfo():
2429
from subprocess import Popen, PIPE
25-
kw = dict(stdout=PIPE, cwd=MYDIR)
30+
kw = dict(stdout=PIPE, cwd=MYDIR, universal_newlines=True)
2631
proc = Popen(['git', 'describe', '--match=v[[:digit:]]*'], **kw)
2732
desc = proc.stdout.read()
2833
proc = Popen(['git', 'log', '-1', '--format=%H %ct %ci'], **kw)
@@ -34,8 +39,10 @@ def gitinfo():
3439

3540

3641
def getversioncfg():
37-
import re
38-
from ConfigParser import RawConfigParser
42+
if PY3:
43+
from configparser import RawConfigParser
44+
else:
45+
from ConfigParser import RawConfigParser
3946
vd0 = dict(version=FALLBACK_VERSION, commit='', date='', timestamp=0)
4047
# first fetch data from gitarchivecfgfile, ignore if it is unexpanded
4148
g = vd0.copy()
@@ -64,7 +71,8 @@ def getversioncfg():
6471
cp.set('DEFAULT', 'commit', g['commit'])
6572
cp.set('DEFAULT', 'date', g['date'])
6673
cp.set('DEFAULT', 'timestamp', g['timestamp'])
67-
cp.write(open(versioncfgfile, 'w'))
74+
with open(versioncfgfile, 'w') as fp:
75+
cp.write(fp)
6876
return cp
6977

7078
versiondata = getversioncfg()

0 commit comments

Comments
 (0)