Changeset 391 for python/trunk/Lib/telnetlib.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/telnetlib.py
r2 r391 35 35 36 36 # Imported modules 37 import errno 37 38 import sys 38 39 import socket … … 206 207 self.sbdataq = '' 207 208 self.option_callback = None 209 self._has_poll = hasattr(select, 'poll') 208 210 if host is not None: 209 211 self.open(host, port, timeout) … … 237 239 """ 238 240 if self.debuglevel > 0: 239 print 'Telnet(%s,% d):' % (self.host, self.port),241 print 'Telnet(%s,%s):' % (self.host, self.port), 240 242 if args: 241 243 print msg % args … … 287 289 is closed and no cooked data is available. 288 290 291 """ 292 if self._has_poll: 293 return self._read_until_with_poll(match, timeout) 294 else: 295 return self._read_until_with_select(match, timeout) 296 297 def _read_until_with_poll(self, match, timeout): 298 """Read until a given string is encountered or until timeout. 299 300 This method uses select.poll() to implement the timeout. 301 """ 302 n = len(match) 303 call_timeout = timeout 304 if timeout is not None: 305 from time import time 306 time_start = time() 307 self.process_rawq() 308 i = self.cookedq.find(match) 309 if i < 0: 310 poller = select.poll() 311 poll_in_or_priority_flags = select.POLLIN | select.POLLPRI 312 poller.register(self, poll_in_or_priority_flags) 313 while i < 0 and not self.eof: 314 try: 315 ready = poller.poll(call_timeout) 316 except select.error as e: 317 if e.errno == errno.EINTR: 318 if timeout is not None: 319 elapsed = time() - time_start 320 call_timeout = timeout-elapsed 321 continue 322 raise 323 for fd, mode in ready: 324 if mode & poll_in_or_priority_flags: 325 i = max(0, len(self.cookedq)-n) 326 self.fill_rawq() 327 self.process_rawq() 328 i = self.cookedq.find(match, i) 329 if timeout is not None: 330 elapsed = time() - time_start 331 if elapsed >= timeout: 332 break 333 call_timeout = timeout-elapsed 334 poller.unregister(self) 335 if i >= 0: 336 i = i + n 337 buf = self.cookedq[:i] 338 self.cookedq = self.cookedq[i:] 339 return buf 340 return self.read_very_lazy() 341 342 def _read_until_with_select(self, match, timeout=None): 343 """Read until a given string is encountered or until timeout. 344 345 The timeout is implemented using select.select(). 289 346 """ 290 347 n = len(match) … … 590 647 591 648 """ 649 if self._has_poll: 650 return self._expect_with_poll(list, timeout) 651 else: 652 return self._expect_with_select(list, timeout) 653 654 def _expect_with_poll(self, expect_list, timeout=None): 655 """Read until one from a list of a regular expressions matches. 656 657 This method uses select.poll() to implement the timeout. 658 """ 659 re = None 660 expect_list = expect_list[:] 661 indices = range(len(expect_list)) 662 for i in indices: 663 if not hasattr(expect_list[i], "search"): 664 if not re: import re 665 expect_list[i] = re.compile(expect_list[i]) 666 call_timeout = timeout 667 if timeout is not None: 668 from time import time 669 time_start = time() 670 self.process_rawq() 671 m = None 672 for i in indices: 673 m = expect_list[i].search(self.cookedq) 674 if m: 675 e = m.end() 676 text = self.cookedq[:e] 677 self.cookedq = self.cookedq[e:] 678 break 679 if not m: 680 poller = select.poll() 681 poll_in_or_priority_flags = select.POLLIN | select.POLLPRI 682 poller.register(self, poll_in_or_priority_flags) 683 while not m and not self.eof: 684 try: 685 ready = poller.poll(call_timeout) 686 except select.error as e: 687 if e.errno == errno.EINTR: 688 if timeout is not None: 689 elapsed = time() - time_start 690 call_timeout = timeout-elapsed 691 continue 692 raise 693 for fd, mode in ready: 694 if mode & poll_in_or_priority_flags: 695 self.fill_rawq() 696 self.process_rawq() 697 for i in indices: 698 m = expect_list[i].search(self.cookedq) 699 if m: 700 e = m.end() 701 text = self.cookedq[:e] 702 self.cookedq = self.cookedq[e:] 703 break 704 if timeout is not None: 705 elapsed = time() - time_start 706 if elapsed >= timeout: 707 break 708 call_timeout = timeout-elapsed 709 poller.unregister(self) 710 if m: 711 return (i, m, text) 712 text = self.read_very_lazy() 713 if not text and self.eof: 714 raise EOFError 715 return (-1, None, text) 716 717 def _expect_with_select(self, list, timeout=None): 718 """Read until one from a list of a regular expressions matches. 719 720 The timeout is implemented using select.select(). 721 """ 592 722 re = None 593 723 list = list[:]
Note:
See TracChangeset
for help on using the changeset viewer.