This reads at most "size" bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encounter
(self, size=-1)
| 442 | return exp.expect_loop(timeout) |
| 443 | |
| 444 | def read(self, size=-1): |
| 445 | '''This reads at most "size" bytes from the file (less if the read hits |
| 446 | EOF before obtaining size bytes). If the size argument is negative or |
| 447 | omitted, read all data until EOF is reached. The bytes are returned as |
| 448 | a string object. An empty string is returned when EOF is encountered |
| 449 | immediately. ''' |
| 450 | |
| 451 | if size == 0: |
| 452 | return self.string_type() |
| 453 | if size < 0: |
| 454 | # delimiter default is EOF |
| 455 | self.expect(self.delimiter) |
| 456 | return self.before |
| 457 | |
| 458 | # I could have done this more directly by not using expect(), but |
| 459 | # I deliberately decided to couple read() to expect() so that |
| 460 | # I would catch any bugs early and ensure consistent behavior. |
| 461 | # It's a little less efficient, but there is less for me to |
| 462 | # worry about if I have to later modify read() or expect(). |
| 463 | # Note, it's OK if size==-1 in the regex. That just means it |
| 464 | # will never match anything in which case we stop only on EOF. |
| 465 | cre = re.compile(self._coerce_expect_string('.{%d}' % size), re.DOTALL) |
| 466 | # delimiter default is EOF |
| 467 | index = self.expect([cre, self.delimiter]) |
| 468 | if index == 0: |
| 469 | ### FIXME self.before should be ''. Should I assert this? |
| 470 | return self.after |
| 471 | return self.before |
| 472 | |
| 473 | def readline(self, size=-1): |
| 474 | '''This reads and returns one entire line. The newline at the end of |