MCPcopy Index your code
hub / github.com/RustPython/RustPython / itn

Function itn

Lib/tarfile.py:193–221  ·  view source on GitHub ↗

Convert a python number to a number field.

(n, digits=8, format=DEFAULT_FORMAT)

Source from the content-addressed store, hash-verified

191 return n
192
193def itn(n, digits=8, format=DEFAULT_FORMAT):
194 """Convert a python number to a number field.
195 """
196 # POSIX 1003.1-1988 requires numbers to be encoded as a string of
197 # octal digits followed by a null-byte, this allows values up to
198 # (8**(digits-1))-1. GNU tar allows storing numbers greater than
199 # that if necessary. A leading 0o200 or 0o377 byte indicate this
200 # particular encoding, the following digits-1 bytes are a big-endian
201 # base-256 representation. This allows values up to (256**(digits-1))-1.
202 # A 0o200 byte indicates a positive number, a 0o377 byte a negative
203 # number.
204 original_n = n
205 n = int(n)
206 if 0 <= n < 8 ** (digits - 1):
207 s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
208 elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
209 if n >= 0:
210 s = bytearray([0o200])
211 else:
212 s = bytearray([0o377])
213 n = 256 ** digits + n
214
215 for i in range(digits - 1):
216 s.insert(1, n & 0o377)
217 n >>= 8
218 else:
219 raise ValueError("overflow in number field")
220
221 return s
222
223def calc_chksums(buf):
224 """Calculate the checksum for a member&#x27;s header by summing up all

Callers 1

_create_headerMethod · 0.85

Calls 1

insertMethod · 0.45

Tested by

no test coverage detected