Text I/O implementation using an in-memory buffer. The initial_value argument sets the value of object. The newline argument is like the one of TextIOWrapper's constructor.
| 2728 | |
| 2729 | |
| 2730 | class StringIO(TextIOWrapper): |
| 2731 | """Text I/O implementation using an in-memory buffer. |
| 2732 | |
| 2733 | The initial_value argument sets the value of object. The newline |
| 2734 | argument is like the one of TextIOWrapper's constructor. |
| 2735 | """ |
| 2736 | |
| 2737 | def __init__(self, initial_value="", newline="\n"): |
| 2738 | super(StringIO, self).__init__(BytesIO(), |
| 2739 | encoding="utf-8", |
| 2740 | errors="surrogatepass", |
| 2741 | newline=newline) |
| 2742 | # Issue #5645: make universal newlines semantics the same as in the |
| 2743 | # C version, even under Windows. |
| 2744 | if newline is None: |
| 2745 | self._writetranslate = False |
| 2746 | if initial_value is not None: |
| 2747 | if not isinstance(initial_value, str): |
| 2748 | raise TypeError("initial_value must be str or None, not {0}" |
| 2749 | .format(type(initial_value).__name__)) |
| 2750 | self.write(initial_value) |
| 2751 | self.seek(0) |
| 2752 | |
| 2753 | def getvalue(self): |
| 2754 | self.flush() |
| 2755 | decoder = self._decoder or self._get_decoder() |
| 2756 | old_state = decoder.getstate() |
| 2757 | decoder.reset() |
| 2758 | try: |
| 2759 | return decoder.decode(self.buffer.getvalue(), final=True) |
| 2760 | finally: |
| 2761 | decoder.setstate(old_state) |
| 2762 | |
| 2763 | def __repr__(self): |
| 2764 | # TextIOWrapper tells the encoding in its repr. In StringIO, |
| 2765 | # that's an implementation detail. |
| 2766 | return object.__repr__(self) |
| 2767 | |
| 2768 | @property |
| 2769 | def errors(self): |
| 2770 | return None |
| 2771 | |
| 2772 | @property |
| 2773 | def encoding(self): |
| 2774 | return None |
| 2775 | |
| 2776 | def detach(self): |
| 2777 | # This doesn't make sense on StringIO. |
| 2778 | self._unsupported("detach") |
no outgoing calls
searching dependent graphs…