r""" >>> ascii('Hello') 'Hello' >>> ascii('\N{TRADE MARK SIGN}') #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... UnicodeEncodeError: ...
(s)
| 45 | unicode = str |
| 46 | |
| 47 | def ascii(s): |
| 48 | r""" |
| 49 | >>> ascii('Hello') |
| 50 | 'Hello' |
| 51 | >>> ascii('\N{TRADE MARK SIGN}') #doctest: +IGNORE_EXCEPTION_DETAIL |
| 52 | Traceback (most recent call last): |
| 53 | ... |
| 54 | UnicodeEncodeError: ... |
| 55 | """ |
| 56 | if type(s) == bytes: |
| 57 | s = s.decode('ASCII') |
| 58 | else: |
| 59 | s.encode('ASCII') # Raise an exception if not ASCII |
| 60 | return s # But the string - not a byte string. |
| 61 | |
| 62 | else: # Python 2.x |
| 63 |