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

Method readline

Lib/_pyio.py:2628–2719  ·  view source on GitHub ↗
(self, size=None)

Source from the content-addressed store, hash-verified

2626 return line
2627
2628 def readline(self, size=None):
2629 if self.closed:
2630 raise ValueError("read from closed file")
2631 if size is None:
2632 size = -1
2633 else:
2634 try:
2635 size_index = size.__index__
2636 except AttributeError:
2637 raise TypeError(f"{size!r} is not an integer")
2638 else:
2639 size = size_index()
2640
2641 # Grab all the decoded text (we will rewind any extra bits later).
2642 line = self._get_decoded_chars()
2643
2644 start = 0
2645 # Make the decoder if it doesn't already exist.
2646 if not self._decoder:
2647 self._get_decoder()
2648
2649 pos = endpos = None
2650 while True:
2651 if self._readtranslate:
2652 # Newlines are already translated, only search for \n
2653 pos = line.find('\n', start)
2654 if pos >= 0:
2655 endpos = pos + 1
2656 break
2657 else:
2658 start = len(line)
2659
2660 elif self._readuniversal:
2661 # Universal newline search. Find any of \r, \r\n, \n
2662 # The decoder ensures that \r\n are not split in two pieces
2663
2664 # In C we'd look for these in parallel of course.
2665 nlpos = line.find("\n", start)
2666 crpos = line.find("\r", start)
2667 if crpos == -1:
2668 if nlpos == -1:
2669 # Nothing found
2670 start = len(line)
2671 else:
2672 # Found \n
2673 endpos = nlpos + 1
2674 break
2675 elif nlpos == -1:
2676 # Found lone \r
2677 endpos = crpos + 1
2678 break
2679 elif nlpos < crpos:
2680 # Found \n
2681 endpos = nlpos + 1
2682 break
2683 elif nlpos == crpos + 1:
2684 # Found \r\n
2685 endpos = crpos + 2

Callers 8

__next__Method · 0.95
test_constructorMethod · 0.95
test_newlinesMethod · 0.95
test_issue1395_3Method · 0.95
test_rawioMethod · 0.95
test_issue35928Method · 0.95

Calls 6

_get_decoded_charsMethod · 0.95
_get_decoderMethod · 0.95
_read_chunkMethod · 0.95
_set_decoded_charsMethod · 0.95
_rewind_decoded_charsMethod · 0.95
findMethod · 0.45

Tested by 7

test_constructorMethod · 0.76
test_newlinesMethod · 0.76
test_issue1395_3Method · 0.76
test_rawioMethod · 0.76
test_issue35928Method · 0.76