Skip to content

Commit 9653f31

Browse files
committed
Fix deprecated calls for Python 3.9
The array methods fromstring/tostring have been deprecated since Python 3.2. Python 3.9 removes them completely. This was discovered when trying to build gitdb package for Fedora 33. https://bugzilla.redhat.com/show_bug.cgi?id=1788660
1 parent 43e1631 commit 9653f31

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

gitdb/pack.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ def write(self, pack_sha, write):
247247
return sha
248248

249249

250+
def _frombytes(a, s):
251+
"""Since Python 3.2 fromstring() is just an alias for frombytes() and is
252+
deprecated. Python 3.9 drops the alias."""
253+
if sys.version_info.major < 3:
254+
a.fromstring(s)
255+
else:
256+
a.frombytes(s)
257+
258+
250259
class PackIndexFile(LazyMixin):
251260

252261
"""A pack index provides offsets into the corresponding pack, allowing to find
@@ -410,7 +419,7 @@ def offsets(self):
410419
if self._version == 2:
411420
# read stream to array, convert to tuple
412421
a = array.array('I') # 4 byte unsigned int, long are 8 byte on 64 bit it appears
413-
a.fromstring(buffer(self._cursor.map(), self._pack_offset, self._pack_64_offset - self._pack_offset))
422+
_frombytes(a, buffer(self._cursor.map(), self._pack_offset, self._pack_64_offset - self._pack_offset))
414423

415424
# networkbyteorder to something array likes more
416425
if sys.byteorder == 'little':

gitdb/test/lib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ def copy_files_globbed(source_glob, target_dir, hard_link_ok=False):
147147
# END for each file to copy
148148

149149

150+
def _tobytes(a):
151+
"""Since Python 3.2 tostring() is deprecated and an alias for tobytes().
152+
Python 3.9 drops the alias."""
153+
if sys.version_info.major < 3:
154+
return a.tostring()
155+
else:
156+
return a.tobytes()
157+
158+
150159
def make_bytes(size_in_bytes, randomize=False):
151160
""":return: string with given size in bytes
152161
:param randomize: try to produce a very random stream"""
@@ -157,7 +166,7 @@ def make_bytes(size_in_bytes, randomize=False):
157166
random.shuffle(producer)
158167
# END randomize
159168
a = array('i', producer)
160-
return a.tostring()
169+
return _tobytes(a)
161170

162171

163172
def make_object(type, data):

0 commit comments

Comments
 (0)