| --- a/Lib/packaging/metadata.py |
| +++ b/Lib/packaging/metadata.py |
| @@ -3,6 +3,7 @@ |
| Supports all metadata formats (1.0, 1.1, 1.2). |
| """ |
| +import codecs |
| import re |
| import logging |
| @@ -330,11 +331,16 @@ class Metadata: |
| def write(self, filepath): |
| """Write the metadata fields to filepath.""" |
| - with open(filepath, 'w') as fp: |
| + with open(filepath, 'w', encoding='utf-8') as fp: |
| self.write_file(fp) |
| def write_file(self, fileobject): |
| """Write the PKG-INFO format data to a file object.""" |
| + if not isinstance(fileobject, StringIO): |
| + encoding = codecs.lookup(fileobject.encoding).name |
| + if encoding != 'utf-8': |
| + raise ValueError("Output file should be a UTF-8 text file, not %s encoding" % encoding) |
| + |
| self._set_best_version() |
| for field in _version2fieldlist(self['Metadata-Version']): |
| values = self.get(field) |