MCPcopy Index your code
hub / github.com/python/cpython / nti

Function nti

Lib/tarfile.py:172–190  ·  view source on GitHub ↗

Convert a number field to a python number.

(s)

Source from the content-addressed store, hash-verified

170 return s.decode(encoding, errors)
171
172def nti(s):
173 """Convert a number field to a python number.
174 """
175 # There are two possible encodings for a number field, see
176 # itn() below.
177 if s[0] in (0o200, 0o377):
178 n = 0
179 for i in range(len(s) - 1):
180 n <<= 8
181 n += s[i + 1]
182 if s[0] == 0o377:
183 n = -(256 ** (len(s) - 1) - n)
184 else:
185 try:
186 s = nts(s, "ascii", "strict")
187 n = int(s.strip() or "0", 8)
188 except ValueError:
189 raise InvalidHeaderError("invalid header")
190 return n
191
192def itn(n, digits=8, format=DEFAULT_FORMAT):
193 """Convert a python number to a number field.

Callers 2

_frombufMethod · 0.85
_proc_sparseMethod · 0.85

Calls 3

ntsFunction · 0.85
InvalidHeaderErrorClass · 0.85
stripMethod · 0.45

Tested by

no test coverage detected