import tarfile def patch_tarfile(): """Fix low-level functions in the standard tarfile module to support tarfiles with numeric fields that only contain spaces, such as those written by Apache Maven/plexus-archiver. See http://bugs.python.org/issue15858 for more information. """ def nti(s): """Convert a number field to a python number. """ # There are two possible encodings for a number field, see # tarfile.itn(). if s[0] in (0o200, 0o377): n = 0 for i in range(len(s) - 1): n <<= 8 n += s[i + 1] if s[0] == 0o377: n = -(256 ** (len(s) - 1) - n) else: try: # *** PATCH *** # Inlined nts() (since its arguments differ across # Python versions) and then added .lstrip(' ') and # treating space as a terminator after that. p = s.find(b"\0") if p != -1: s = s[:p] u = s.decode('ascii', 'strict') u = u.lstrip(' ') if ' ' in u: u = u.split(' ', 1)[0] n = int(u or "0", 8) except ValueError: if hasattr(tarfile, 'InvalidHeaderError'): raise tarfile.InvalidHeaderError("invalid header") else: raise tarfile.HeaderError("invalid header") return n tarfile.nti = nti