This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pitrou
Recipients pitrou, vstinner
Date 2010-09-08.23:04:51
SpamBayes Score 0.01056796
Marked as misclassified No
Message-id <1283987093.93.0.563531611442.issue9804@psf.upfronthosting.co.za>
In-reply-to
Content
How about the following solution:

>>> def a(s):
...    s = s.encode('unicode-escape').decode('ascii')
...    s = s.replace("'", r"\'")
...    return "'" + s + "'"
... 
>>> s = "'\0\"\n\r\t abcd\x85é\U00012fff\U0001D121xxx\uD800."
>>> print(ascii(s)); print(a(s)); print(repr(s))
'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud834\udd21xxx\ud800.'
'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\U0001d121xxx\ud800.'
'\'\x00"\n\r\t abcd\x85é\U00012fff𝄡xxx\ud800.'


(I think I've included everything:
- normal chars
- control chars
- one-byte non-ASCII
- two-byte non-ASCII (and lone surrogate)
- printable and non-printable surrogate pairs)
- single and double quotes)
History
Date User Action Args
2010-09-08 23:04:56pitrousetrecipients: + pitrou, vstinner
2010-09-08 23:04:53pitrousetmessageid: <1283987093.93.0.563531611442.issue9804@psf.upfronthosting.co.za>
2010-09-08 23:04:52pitroulinkissue9804 messages
2010-09-08 23:04:51pitroucreate