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

Method set_boundary

Lib/email/message.py:879–923  ·  view source on GitHub ↗

Set the boundary parameter in Content-Type to 'boundary'. This is subtly different than deleting the Content-Type header and adding a new one with a new boundary parameter via add_header(). The main difference is that using the set_boundary() method preserves the or

(self, boundary)

Source from the content-addressed store, hash-verified

877 return utils.collapse_rfc2231_value(boundary).rstrip()
878
879 def set_boundary(self, boundary):
880 """Set the boundary parameter in Content-Type to 'boundary'.
881
882 This is subtly different than deleting the Content-Type header and
883 adding a new one with a new boundary parameter via add_header(). The
884 main difference is that using the set_boundary() method preserves the
885 order of the Content-Type header in the original message.
886
887 HeaderParseError is raised if the message has no Content-Type header.
888 """
889 missing = object()
890 params = self._get_params_preserve(missing, 'content-type')
891 if params is missing:
892 # There was no Content-Type header, and we don't know what type
893 # to set it to, so raise an exception.
894 raise errors.HeaderParseError('No Content-Type header found')
895 newparams = []
896 foundp = False
897 for pk, pv in params:
898 if pk.lower() == 'boundary':
899 newparams.append(('boundary', '"%s"' % boundary))
900 foundp = True
901 else:
902 newparams.append((pk, pv))
903 if not foundp:
904 # The original Content-Type header had no boundary attribute.
905 # Tack one on the end. BAW: should we raise an exception
906 # instead???
907 newparams.append(('boundary', '"%s"' % boundary))
908 # Replace the existing Content-Type header with the new value
909 newheaders = []
910 for h, v in self._headers:
911 if h.lower() == 'content-type':
912 parts = []
913 for k, v in newparams:
914 if v == '':
915 parts.append(k)
916 else:
917 parts.append('%s=%s' % (k, v))
918 val = SEMISPACE.join(parts)
919 newheaders.append(self.policy.header_store_parse(h, val))
920
921 else:
922 newheaders.append((h, v))
923 self._headers = newheaders
924
925 def get_content_charset(self, failobj=None):
926 """Return the charset parameter of the Content-Type header.

Calls 5

_get_params_preserveMethod · 0.95
lowerMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45
header_store_parseMethod · 0.45