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

Method readline

Lib/_pyio.py:509–549  ·  view source on GitHub ↗

r"""Read and return a line of bytes from the stream. If size is specified, at most size bytes will be read. Size should be an int. The line terminator is always b'\n' for binary files; for text files, the newlines argument to open can be used to select the line

(self, size=-1)

Source from the content-addressed store, hash-verified

507 ### Readline[s] and writelines ###
508
509 def readline(self, size=-1):
510 r"""Read and return a line of bytes from the stream.
511
512 If size is specified, at most size bytes will be read.
513 Size should be an int.
514
515 The line terminator is always b'\n' for binary files; for text
516 files, the newlines argument to open can be used to select the line
517 terminator(s) recognized.
518 """
519 # For backwards compatibility, a (slowish) readline().
520 if hasattr(self, "peek"):
521 def nreadahead():
522 readahead = self.peek(1)
523 if not readahead:
524 return 1
525 n = (readahead.find(b"\n") + 1) or len(readahead)
526 if size >= 0:
527 n = min(n, size)
528 return n
529 else:
530 def nreadahead():
531 return 1
532 if size is None:
533 size = -1
534 else:
535 try:
536 size_index = size.__index__
537 except AttributeError:
538 raise TypeError(f"{size!r} is not an integer")
539 else:
540 size = size_index()
541 res = bytearray()
542 while size < 0 or len(res) < size:
543 b = self.read(nreadahead())
544 if not b:
545 break
546 res += b
547 if res.endswith(b"\n"):
548 break
549 return res.take_bytes()
550
551 def __iter__(self):
552 self._checkClosed()

Callers 1

__next__Method · 0.95

Calls 3

take_bytesMethod · 0.80
readMethod · 0.45
endswithMethod · 0.45

Tested by

no test coverage detected