Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24)

Side by Side Diff: Lib/packaging/metadata.py

Issue 12112: The new packaging module should not use the locale encoding
Patch Set: Created 2 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/packaging/create.py ('k') | Lib/packaging/tests/support.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Implementation of the Metadata for Python packages PEPs. 1 """Implementation of the Metadata for Python packages PEPs.
2 2
3 Supports all metadata formats (1.0, 1.1, 1.2). 3 Supports all metadata formats (1.0, 1.1, 1.2).
4 """ 4 """
5 5
6 import codecs
6 import re 7 import re
7 import logging 8 import logging
8 9
9 from io import StringIO 10 from io import StringIO
10 from email import message_from_file 11 from email import message_from_file
11 from packaging import logger 12 from packaging import logger
12 from packaging.markers import interpret 13 from packaging.markers import interpret
13 from packaging.version import (is_valid_predicate, is_valid_version, 14 from packaging.version import (is_valid_predicate, is_valid_version,
14 is_valid_versions) 15 is_valid_versions)
15 from packaging.errors import (MetadataMissingError, 16 from packaging.errors import (MetadataMissingError,
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 values = [tuple(value.split(',')) for value in values] 324 values = [tuple(value.split(',')) for value in values]
324 self.set(field, values) 325 self.set(field, values)
325 else: 326 else:
326 # single line 327 # single line
327 value = msg[field] 328 value = msg[field]
328 if value is not None and value != 'UNKNOWN': 329 if value is not None and value != 'UNKNOWN':
329 self.set(field, value) 330 self.set(field, value)
330 331
331 def write(self, filepath): 332 def write(self, filepath):
332 """Write the metadata fields to filepath.""" 333 """Write the metadata fields to filepath."""
333 with open(filepath, 'w') as fp: 334 with open(filepath, 'w', encoding='utf-8') as fp:
334 self.write_file(fp) 335 self.write_file(fp)
335 336
336 def write_file(self, fileobject): 337 def write_file(self, fileobject):
337 """Write the PKG-INFO format data to a file object.""" 338 """Write the PKG-INFO format data to a file object."""
339 if not isinstance(fileobject, StringIO):
340 encoding = codecs.lookup(fileobject.encoding).name
341 if encoding != 'utf-8':
342 raise ValueError("Output file should be a UTF-8 text file, not % s encoding" % encoding)
343
338 self._set_best_version() 344 self._set_best_version()
339 for field in _version2fieldlist(self['Metadata-Version']): 345 for field in _version2fieldlist(self['Metadata-Version']):
340 values = self.get(field) 346 values = self.get(field)
341 if field in _ELEMENTSFIELD: 347 if field in _ELEMENTSFIELD:
342 self._write_field(fileobject, field, ','.join(values)) 348 self._write_field(fileobject, field, ','.join(values))
343 continue 349 continue
344 if field not in _LISTFIELDS: 350 if field not in _LISTFIELDS:
345 if field == 'Description': 351 if field == 'Description':
346 values = values.replace('\n', '\n |') 352 values = values.replace('\n', '\n |')
347 values = [values] 353 values = [values]
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 549
544 def __iter__(self): 550 def __iter__(self):
545 for key in self.keys(): 551 for key in self.keys():
546 yield key 552 yield key
547 553
548 def values(self): 554 def values(self):
549 return [self[key] for key in list(self.keys())] 555 return [self[key] for key in list(self.keys())]
550 556
551 def items(self): 557 def items(self):
552 return [(key, self[key]) for key in list(self.keys())] 558 return [(key, self[key]) for key in list(self.keys())]
OLDNEW
« no previous file with comments | « Lib/packaging/create.py ('k') | Lib/packaging/tests/support.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7