| 1950 | self.pendingcr = False |
| 1951 | |
| 1952 | def decode(self, input, final=False): |
| 1953 | # decode input (with the eventual \r from a previous pass) |
| 1954 | if self.decoder is None: |
| 1955 | output = input |
| 1956 | else: |
| 1957 | output = self.decoder.decode(input, final=final) |
| 1958 | if self.pendingcr and (output or final): |
| 1959 | output = "\r" + output |
| 1960 | self.pendingcr = False |
| 1961 | |
| 1962 | # retain last \r even when not translating data: |
| 1963 | # then readline() is sure to get \r\n in one pass |
| 1964 | if output.endswith("\r") and not final: |
| 1965 | output = output[:-1] |
| 1966 | self.pendingcr = True |
| 1967 | |
| 1968 | # Record which newlines are read |
| 1969 | crlf = output.count('\r\n') |
| 1970 | cr = output.count('\r') - crlf |
| 1971 | lf = output.count('\n') - crlf |
| 1972 | self.seennl |= (lf and self._LF) | (cr and self._CR) \ |
| 1973 | | (crlf and self._CRLF) |
| 1974 | |
| 1975 | if self.translate: |
| 1976 | if crlf: |
| 1977 | output = output.replace("\r\n", "\n") |
| 1978 | if cr: |
| 1979 | output = output.replace("\r", "\n") |
| 1980 | |
| 1981 | return output |
| 1982 | |
| 1983 | def getstate(self): |
| 1984 | if self.decoder is None: |