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

Method _create_pax_generic_header

Lib/tarfile.py:1228–1276  ·  view source on GitHub ↗

Return a POSIX.1-2008 extended or global header sequence that contains a list of keyword, value pairs. The values must be strings.

(cls, pax_headers, type, encoding)

Source from the content-addressed store, hash-verified

1226
1227 @classmethod
1228 def _create_pax_generic_header(cls, pax_headers, type, encoding):
1229 """Return a POSIX.1-2008 extended or global header sequence
1230 that contains a list of keyword, value pairs. The values
1231 must be strings.
1232 """
1233 # Check if one of the fields contains surrogate characters and thereby
1234 # forces hdrcharset=BINARY, see _proc_pax() for more information.
1235 binary = False
1236 for keyword, value in pax_headers.items():
1237 try:
1238 value.encode("utf-8", "strict")
1239 except UnicodeEncodeError:
1240 binary = True
1241 break
1242
1243 records = b""
1244 if binary:
1245 # Put the hdrcharset field at the beginning of the header.
1246 records += b"21 hdrcharset=BINARY\n"
1247
1248 for keyword, value in pax_headers.items():
1249 keyword = keyword.encode("utf-8")
1250 if binary:
1251 # Try to restore the original byte representation of 'value'.
1252 # Needless to say, that the encoding must match the string.
1253 value = value.encode(encoding, "surrogateescape")
1254 else:
1255 value = value.encode("utf-8")
1256
1257 l = len(keyword) + len(value) + 3 # ' ' + '=' + '\n'
1258 n = p = 0
1259 while True:
1260 n = l + len(str(p))
1261 if n == p:
1262 break
1263 p = n
1264 records += bytes(str(p), "ascii") + b" " + keyword + b"=" + value + b"\n"
1265
1266 # We use a hardcoded "././@PaxHeader" name like star does
1267 # instead of the one that POSIX recommends.
1268 info = {}
1269 info["name"] = "././@PaxHeader"
1270 info["type"] = type
1271 info["size"] = len(records)
1272 info["magic"] = POSIX_MAGIC
1273
1274 # Create pax header + record blocks.
1275 return cls._create_header(info, USTAR_FORMAT, "ascii", "replace") + \
1276 cls._create_payload(records)
1277
1278 @classmethod
1279 def frombuf(cls, buf, encoding, errors):

Callers 2

create_pax_headerMethod · 0.95

Calls 6

lenFunction · 0.85
strFunction · 0.85
_create_headerMethod · 0.80
_create_payloadMethod · 0.80
itemsMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected