8000 Fixed unicode/bytes/str problems. · drinkingjava/python-versioneer@bb191ea · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit bb191ea

Browse files
committed
Fixed unicode/bytes/str problems.
Implemented context-manager (with) statements for every open().
1 parent fa7b8d4 commit bb191ea

File tree

3 files changed

+64
-58
lines changed
< 10000 div class="d-flex flex-items-center flex-justify-between gap-2 pt-3 pt-lg-4 pb-2 position-sticky top-0 color-bg-default" style="z-index:2">

3 files changed

+64
-58
lines changed

setup.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
import os, base64, tempfile, io
3+
import os, base64, tempfile, io, sys
44

55
from distutils.core import setup, Command
66
from distutils.command.build_scripts import build_scripts
@@ -16,7 +16,18 @@
1616
VERSION = "0.10+"
1717

1818
def get(fn):
19-
return open(fn, "r").read()
19+
with open(fn) as f:
20+
text = f.read()
21+
22+
# If we're in Python <3 and have a separate Unicode type, we would've read
23+
# a non-unicode string. Else, all strings will be unicode strings.
24+
try:
25+
__builtins__.unicode
26+
except AttributeError:
27+
return text
28+
else:
29+
return text.decode('ASCII')
30+
2031
def unquote(s):
2132
return s.replace("%", "%%")
2233
def ver(s):
@@ -37,14 +48,14 @@ def generate_versioneer():
3748
s.write(get("src/subprocess_helper.py"))
3849

3950
for VCS in get_vcs_list():
40-
s.write("LONG_VERSION_PY['%s'] = '''\n" % VCS)
51+
s.write(u"LONG_VERSION_PY['%s'] = '''\n" % VCS)
4152
s.write(ver(get("src/%s/long_header.py" % VCS)))
4253
s.write(unquote(get("src/subprocess_helper.py")))
4354
s.write(unquote(get("src/from_parentdir.py")))
4455
s.write(unquote(get("src/%s/from_keywords.py" % VCS)))
4556
s.write(unquote(get("src/%s/from_vcs.py" % VCS)))
4657
s.write(unquote(get("src/%s/long_get_versions.py" % VCS)))
47-
s.write("'''\n")
58+
s.write(u"'''\n")
4859
s.write(get("src/%s/from_keywords.py" % VCS))
4960
s.write(get("src/%s/from_vcs.py" % VCS))
5061
s.write(get("src/%s/install.py" % VCS))

src/cmdclass.py

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ def run(self):
2121
target_versionfile = os.path.join(self.build_lib, versionfile_build)
2222
print("UPDATING %s" % target_versionfile)
2323
os.unlink(target_versionfile)
24-
f = open(target_versionfile, "w")
25-
f.write(SHORT_VERSION_PY % versions)
26-
f.close()
24+
with open(target_versionfile, "w") as f:
25+
f.write(SHORT_VERSION_PY % versions)
2726

2827
if 'cx_Freeze' in sys.modules: # cx_freeze enabled?
2928
from cx_Freeze.dist import build_exe as _build_exe
@@ -34,20 +33,19 @@ def run(self):
3433
target_versionfile = versionfile_source
3534
print("UPDATING %s" % target_versionfile)
3635
os.unlink(target_versionfile)
37-
f = open(target_versionfile, "w")
38-
f.write(SHORT_VERSION_PY % versions)
39-
f.close()
36+
with open(target_versionfile, "w") as f:
37+
f.write(SHORT_VERSION_PY % versions)
38+
4039
_build_exe.run(self)
4140
os.unlink(target_versionfile)
42-
f = open(versionfile_source, "w")
43-
assert VCS is not None, "please set versioneer.VCS"
44-
LONG = LONG_VERSION_PY[VCS]
45-
f.write(LONG % {"DOLLAR": "$",
46-
"TAG_PREFIX": tag_prefix,
47-
"PARENTDIR_PREFIX": parentdir_prefix,
48-
"VERSIONFILE_SOURCE": versionfile_source,
49-
})
50-
f.close()
41+
with open(versionfile_source, "w") as f:
42+
assert VCS is not None, "please set versioneer.VCS"
43+
LONG = LONG_VERSION_PY[VCS]
44+
f.write(LONG % {"DOLLAR": "$",
45+
"TAG_PREFIX": tag_prefix,
46+
"PARENTDIR_PREFIX": parentdir_prefix,
47+
"VERSIONFILE_SOURCE": versionfile_source,
48+
})
5149

5250
class cmd_sdist(_sdist):
5351
def run(self):
@@ -64,9 +62,8 @@ def make_release_tree(self, base_dir, files):
6462
target_versionfile = os.path.join(base_dir, versionfile_source)
6563
print("UPDATING %s" % target_versionfile)
6664
os.unlink(target_versionfile)
67-
f = open(target_versionfile, "w")
68-
f.write(SHORT_VERSION_PY % self._versioneer_generated_versions)
69-
f.close()
65+
with open(target_versionfile, "w") as f:
66+
f.write(SHORT_VERSION_PY % self._versioneer_generated_versions)
7067

7168
INIT_PY_SNIPPET = """
7269
from ._version import get_versions
@@ -84,26 +81,25 @@ def finalize_options(self):
8481
pass
8582
def run(self):
8683
print(" creating %s" % versionfile_source)
87-
f = open(versionfile_source, "w")
88-
assert VCS is not None, "please set versioneer.VCS"
89-
LONG = LONG_VERSION_PY[VCS]
90-
f.write(LONG % {"DOLLAR": "$",
91-
"TAG_PREFIX": tag_prefix,
92-
"PARENTDIR_PREFIX": parentdir_prefix,
93-
"VERSIONFILE_SOURCE": versionfile_source,
94-
})
95-
f.close()
84+
with open(versionfile_source, "w") as f:
85+
assert VCS is not None, "please set versioneer.VCS"
86+
LONG = LONG_VERSION_PY[VCS]
87+
f.write(LONG % {"DOLLAR": "$",
88+
"TAG_PREFIX": tag_prefix,
89+
"PARENTDIR_PREFIX": parentdir_prefix,
90+
"VERSIONFILE_SOURCE": versionfile_source,
91+
})
9692

9793
ipy = os.path.join(os.path.dirname(versionfile_source), "__init__.py")
9894
try:
99-
old = open(ipy, "r").read()
95+
with open(ipy, "r") as f:
96+
old = f.read()
10097
except EnvironmentError:
10198
old = ""
10299
if INIT_PY_SNIPPET not in old:
103100
print(" appending to %s" % ipy)
104-
f = open(ipy, "a")
105-
f.write(INIT_PY_SNIPPET)
106-
f.close()
101+
with open(ipy, "a") as f:
102+
f.write(INIT_PY_SNIPPET)
107103
else:
108104
print(" %s unmodified" % ipy)
109105

@@ -114,10 +110,11 @@ def run(self):
114110
manifest_in = os.path.join(get_root(), "MANIFEST.in")
115111
simple_includes = set()
116112
try:
117-
for line in open(manifest_in, "r").readlines():
118-
if line.startswith("include "):
119-
for include in line.split()[1:]:
120-
simple_includes.add(include)
113+
with open(manifest_in, "r") as f:
114+
for line in f:
115+
if line.startswith("include "):
116+
for include in line.split()[1:]:
117+
simple_includes.add(include)
121118
except EnvironmentError:
122119
pass
123120
# That doesn't cover everything MANIFEST.in can do
@@ -126,17 +123,15 @@ def run(self):
126123
# lines is safe, though.
127124
if "versioneer.py" not in simple_includes:
128125
print(" appending 'versioneer.py' to MANIFEST.in")
129-
f = open(manifest_in, "a")
130-
f.write("include versioneer.py\n")
131-
f.close()
126+
with open(manifest_in, "a") as f:
127+
f.write("include versioneer.py\n")
132128
else:
133129
print(" 'versioneer.py' already in MANIFEST.in")
134130
if versionfile_source not in simple_includes:
135131
print(" appending versionfile_source ('%s') to MANIFEST.in" %
136132
versionfile_source)
137-
f = open(manifest_in, "a")
138-
f.write("include %s\n" % versionfile_source)
139-
f.close()
133+
with open(manifest_in, "a") as f:
134+
f.write("include %s\n" % versionfile_source)
140135
else:
141136
print(" versionfile_source already in MANIFEST.in")
142137

src/from_file.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ def get_versions(default={}, verbose=False):
1717
def versions_from_file(filename):
1818
versions = {}
1919
try:
20-
f = open(filename)
20+
with open(filename) as f:
21+
for line in f.readlines():
22+
mo = re.match("version_version = '([^']+)'", line)
23+
if mo:
24+
versions["version"] = mo.group(1)
25+
mo = re.match("version_full = '([^']+)'", line)
26+
if mo:
27+
versions["full"] = mo.group(1)
2128
except EnvironmentError:
22-
return versions
23-
for line in f.readlines():
24-
mo = re.match("version_version = '([^']+)'", line)
25-
if mo:
26-
versions["version"] = mo.group(1)
27-
mo = re.match("version_full = '([^']+)'", line)
28-
if mo:
29-
versions["full"] = mo.group(1)
30-
f.close()
29+
return {}
30+
3131
return versions
3232

3333
def write_to_version_file(filename, versions):
34-
f = open(filename, "w")
35-
f.write(SHORT_VERSION_PY % versions)
36-
f.close()
34+
with open(filename, "w") as f:
35+
f.write(SHORT_VERSION_PY % versions)
36+
3737
print("set %s to '%s'" % (filename, versions["version"]))
3838

0 commit comments

Comments
 (0)
0