| 20 | |
| 21 | @undoc |
| 22 | class IOStream: |
| 23 | |
| 24 | def __init__(self, stream, fallback=None): |
| 25 | warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead', |
| 26 | DeprecationWarning, stacklevel=2) |
| 27 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
| 28 | if fallback is not None: |
| 29 | stream = fallback |
| 30 | else: |
| 31 | raise ValueError("fallback required, but not specified") |
| 32 | self.stream = stream |
| 33 | self._swrite = stream.write |
| 34 | |
| 35 | # clone all methods not overridden: |
| 36 | def clone(meth): |
| 37 | return not hasattr(self, meth) and not meth.startswith('_') |
| 38 | for meth in filter(clone, dir(stream)): |
| 39 | try: |
| 40 | val = getattr(stream, meth) |
| 41 | except AttributeError: |
| 42 | pass |
| 43 | else: |
| 44 | setattr(self, meth, val) |
| 45 | |
| 46 | def __repr__(self): |
| 47 | cls = self.__class__ |
| 48 | tpl = '{mod}.{cls}({args})' |
| 49 | return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream) |
| 50 | |
| 51 | def write(self,data): |
| 52 | warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead', |
| 53 | DeprecationWarning, stacklevel=2) |
| 54 | try: |
| 55 | self._swrite(data) |
| 56 | except: |
| 57 | try: |
| 58 | # print handles some unicode issues which may trip a plain |
| 59 | # write() call. Emulate write() by using an empty end |
| 60 | # argument. |
| 61 | print(data, end='', file=self.stream) |
| 62 | except: |
| 63 | # if we get here, something is seriously broken. |
| 64 | print('ERROR - failed to write data to stream:', self.stream, |
| 65 | file=sys.stderr) |
| 66 | |
| 67 | def writelines(self, lines): |
| 68 | warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead', |
| 69 | DeprecationWarning, stacklevel=2) |
| 70 | if isinstance(lines, str): |
| 71 | lines = [lines] |
| 72 | for line in lines: |
| 73 | self.write(line) |
| 74 | |
| 75 | # This class used to have a writeln method, but regular files and streams |
| 76 | # in Python don't have this method. We need to keep this completely |
| 77 | # compatible so we removed it. |
| 78 | |
| 79 | @property |
no outgoing calls