Return a JSON representation of a Python string
(s, _PY3=PY3, _q=u'"')
| 51 | _dict_types = dict |
| 52 | |
| 53 | def py_encode_basestring(s, _PY3=PY3, _q=u'"'): |
| 54 | """Return a JSON representation of a Python string |
| 55 | |
| 56 | """ |
| 57 | if _PY3: |
| 58 | if isinstance(s, bytes): |
| 59 | s = str(s, 'utf-8') |
| 60 | elif type(s) is not str: |
| 61 | # convert an str subclass instance to exact str |
| 62 | # raise a TypeError otherwise |
| 63 | s = str.__str__(s) |
| 64 | else: |
| 65 | if isinstance(s, str) and HAS_UTF8.search(s) is not None: |
| 66 | s = unicode(s, 'utf-8') |
| 67 | elif type(s) not in (str, unicode): |
| 68 | # convert an str subclass instance to exact str |
| 69 | # convert a unicode subclass instance to exact unicode |
| 70 | # raise a TypeError otherwise |
| 71 | if isinstance(s, str): |
| 72 | s = str.__str__(s) |
| 73 | else: |
| 74 | s = unicode.__getnewargs__(s)[0] |
| 75 | def replace(match): |
| 76 | return ESCAPE_DCT[match.group(0)] |
| 77 | return _q + ESCAPE.sub(replace, s) + _q |
| 78 | |
| 79 | |
| 80 | def py_encode_basestring_ascii(s, _PY3=PY3): |
nothing calls this directly
no test coverage detected
searching dependent graphs…