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

Side by Side Diff: Lib/_pyio.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months 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/pydoc.py ('k') | Lib/random.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 """ 1 """
2 Python implementation of the io module. 2 Python implementation of the io module.
3 """ 3 """
4 4
5 import os 5 import os
6 import abc 6 import abc
7 import codecs 7 import codecs
8 import warnings 8 import warnings
9 # Import _thread instead of threading to reduce startup cost 9 # Import _thread instead of threading to reduce startup cost
10 try: 10 try:
(...skipping 1460 matching lines...) Expand 10 before | Expand all | Expand 10 after
1471 and it is returned untranslated. On output, '\n' is converted to the 1471 and it is returned untranslated. On output, '\n' is converted to the
1472 newline. 1472 newline.
1473 1473
1474 If line_buffering is True, a call to flush is implied when a call to 1474 If line_buffering is True, a call to flush is implied when a call to
1475 write contains a newline character. 1475 write contains a newline character.
1476 """ 1476 """
1477 1477
1478 _CHUNK_SIZE = 2048 1478 _CHUNK_SIZE = 2048
1479 1479
1480 def __init__(self, buffer, encoding=None, errors=None, newline=None, 1480 def __init__(self, buffer, encoding=None, errors=None, newline=None,
1481 line_buffering=False, write_through=False): 1481 line_buffering=False):
1482 if newline is not None and not isinstance(newline, str): 1482 if newline is not None and not isinstance(newline, str):
1483 raise TypeError("illegal newline type: %r" % (type(newline),)) 1483 raise TypeError("illegal newline type: %r" % (type(newline),))
1484 if newline not in (None, "", "\n", "\r", "\r\n"): 1484 if newline not in (None, "", "\n", "\r", "\r\n"):
1485 raise ValueError("illegal newline value: %r" % (newline,)) 1485 raise ValueError("illegal newline value: %r" % (newline,))
1486 if encoding is None: 1486 if encoding is None:
1487 try: 1487 try:
1488 encoding = os.device_encoding(buffer.fileno()) 1488 encoding = os.device_encoding(buffer.fileno())
1489 except (AttributeError, UnsupportedOperation): 1489 except (AttributeError, UnsupportedOperation):
1490 pass 1490 pass
1491 if encoding is None: 1491 if encoding is None:
(...skipping 22 matching lines...) Expand all
1514 self._readtranslate = newline is None 1514 self._readtranslate = newline is None
1515 self._readnl = newline 1515 self._readnl = newline
1516 self._writetranslate = newline != '' 1516 self._writetranslate = newline != ''
1517 self._writenl = newline or os.linesep 1517 self._writenl = newline or os.linesep
1518 self._encoder = None 1518 self._encoder = None
1519 self._decoder = None 1519 self._decoder = None
1520 self._decoded_chars = '' # buffer for text returned from decoder 1520 self._decoded_chars = '' # buffer for text returned from decoder
1521 self._decoded_chars_used = 0 # offset into _decoded_chars for read() 1521 self._decoded_chars_used = 0 # offset into _decoded_chars for read()
1522 self._snapshot = None # info for reconstructing decoder state 1522 self._snapshot = None # info for reconstructing decoder state
1523 self._seekable = self._telling = self.buffer.seekable() 1523 self._seekable = self._telling = self.buffer.seekable()
1524 self._has_read1 = hasattr(self.buffer, 'read1')
1525 self._b2cratio = 0.0 1524 self._b2cratio = 0.0
1526 1525
1527 if self._seekable and self.writable(): 1526 if self._seekable and self.writable():
1528 position = self.buffer.tell() 1527 position = self.buffer.tell()
1529 if position != 0: 1528 if position != 0:
1530 try: 1529 try:
1531 self._get_encoder().setstate(0) 1530 self._get_encoder().setstate(0)
1532 except LookupError: 1531 except LookupError:
1533 # Sometimes the encoder doesn't exist 1532 # Sometimes the encoder doesn't exist
1534 pass 1533 pass
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1681 1680
1682 if self._telling: 1681 if self._telling:
1683 # To prepare for tell(), we need to snapshot a point in the 1682 # To prepare for tell(), we need to snapshot a point in the
1684 # file where the decoder's input buffer is empty. 1683 # file where the decoder's input buffer is empty.
1685 1684
1686 dec_buffer, dec_flags = self._decoder.getstate() 1685 dec_buffer, dec_flags = self._decoder.getstate()
1687 # Given this, we know there was a valid snapshot point 1686 # Given this, we know there was a valid snapshot point
1688 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags). 1687 # len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1689 1688
1690 # Read a chunk, decode it, and put the result in self._decoded_chars. 1689 # Read a chunk, decode it, and put the result in self._decoded_chars.
1691 if self._has_read1: 1690 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1692 input_chunk = self.buffer.read1(self._CHUNK_SIZE)
1693 else:
1694 input_chunk = self.buffer.read(self._CHUNK_SIZE)
1695 eof = not input_chunk 1691 eof = not input_chunk
1696 decoded_chars = self._decoder.decode(input_chunk, eof) 1692 decoded_chars = self._decoder.decode(input_chunk, eof)
1697 self._set_decoded_chars(decoded_chars) 1693 self._set_decoded_chars(decoded_chars)
1698 if decoded_chars: 1694 if decoded_chars:
1699 self._b2cratio = len(input_chunk) / len(self._decoded_chars) 1695 self._b2cratio = len(input_chunk) / len(self._decoded_chars)
1700 else: 1696 else:
1701 self._b2cratio = 0.0 1697 self._b2cratio = 0.0
1702 1698
1703 if self._telling: 1699 if self._telling:
1704 # At the snapshot point, len(dec_buffer) bytes before the read, 1700 # At the snapshot point, len(dec_buffer) bytes before the read,
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
2073 def errors(self): 2069 def errors(self):
2074 return None 2070 return None
2075 2071
2076 @property 2072 @property
2077 def encoding(self): 2073 def encoding(self):
2078 return None 2074 return None
2079 2075
2080 def detach(self): 2076 def detach(self):
2081 # This doesn't make sense on StringIO. 2077 # This doesn't make sense on StringIO.
2082 self._unsupported("detach") 2078 self._unsupported("detach")
OLDNEW
« no previous file with comments | « Lib/pydoc.py ('k') | Lib/random.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+