You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
repr() should not escape non-ASCII characters #46882
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2008-06-11.18:38:55.202>created_at=<Date2008-04-14.09:54:22.958>labels= ['type-feature']
title='repr() should not escape non-ASCII characters'updated_at=<Date2008-06-12.02:44:45.865>user='https://github.com/atsuoishimoto'
In py3k, repr() escapes non-ASCII characters in Unicode to \uXXXX as
Python 2. This is unpleasant feature if you are working with non-latin
characters. This issue was once discussed by Hye-Shik Chang[1], but was
rejected. Here's a new challenge for Python 3 to fix issue.
In this patch, repr() converts special ascii characters such as "\t",
"\r", "\n", but doesn't convert non-ASCII characters to \uXXXX form.
Non-ASCII characters are converted by TextIOWrapper on printing. I set
'errors' attribute of sys.stdout and sys.stderr to 'backslashreplace', so
un-printable characters are converted to '\uXXXX' if your console
cannot print such characters.
This patch breaks five regr tests on my environment.
I'll fix these tests if this patch is acceptable.
I think this has potential, but it is too liberal. There are many more
characters that cannot be assumed printable, e.g. many of the Latin-1
characters in the range 0x80 through 0x9F. Isn't there some Unicode
data table that shows code points that are safely printable?
OTOH there are other potential use cases where it would be nice to see
the \u escapes, e.g. when one is concerned about sequences that print
the same but don't have the same content (e.g. pre-normalization).
The backslashreplace trick is nice, I didn't even know about that. :-)
What if we turn on the backslashreplace trick for some operations only?
For example: sys_displayhook and sys_excepthook.
atsuoishimoto commented on Apr 15, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
I think this has potential, but it is too liberal. There are many more
characters that cannot be assumed printable, e.g. many of the Latin-1
characters in the range 0x80 through 0x9F. Isn't there some Unicode
data table that shows code points that are safely printable?
As Michael Urman pointed out, we can use Unicode properties.
Or we can define a set of non-printable characters (e.g.
sys.nonprintablechars).
OTOH there are other potential use cases where it would be nice to see
the \u escapes, e.g. when one is concerned about sequences that print
the same but don't have the same content (e.g. pre-normalization).
For such cases, print(s.encode("ascii", "backslashreplace")) might work.
atsuoishimoto commented on Apr 15, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
What if we turn on the backslashreplace trick for some operations only?
For example: sys_displayhook and sys_excepthook.
It would be difficult, since *_repr() API don't know who is the caller.
Atsuo: I missed Michael Urman's comment. Can you copy it here, or
(better :-) write a patch that uses it?
Amaury: I think it would be okay to use backslashreplace as the default
error handler for sys.stderr. Probably not for sys.stdout or other
files, since I'm sure many users prefer the errors when their data
cannot be printed rather than silently writing \u escapes that might
cause other code reading their output to choke. For sys.stderr though I
think not having exceptions raised when attempting to print errors is
very valuable.
atsuoishimoto commented on Apr 15, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
Okay, I'll revise a patch later today.
atsuoishimoto commented on Apr 15, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
I revised a patch against Python 3.0a4.
As-per suggestion from Michael Urman, unicode_repr()
refers unicode database to determine characters to be
hex-encoded.
sys.stdout doesn't use 'backslashreplace'.
atsuoishimoto commented on Apr 16, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
I think sys.stdout need to have backslashreplace error handler.
Without backslashreplace, print(listOfJapaneseString) prints nothing,
but raises an exception. This is worse than Python2.
I don't think this is a good idea; I've explained why earlier on this issue.
atsuoishimoto commented on Apr 16, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
Sorry, I missed to write "for interactive session".
I agree for sys.stdout and other files should not have default
backslashescape, but for iteractive session, I think sys.stdout can
have backslasespape handler to avoid exceptions.
While it may be desirable to to have repr(unicode) return a non-ASCII
string, the suggested approach is not suitable to solve the problem.
repr() is usually used in logging and applications/users/tools don't
expect to suddenly find non-ASCII or even mixed encodings in a log file.
If you do want to have this more flexible, then make the encoding used
by unicode_repr() adjustable, turn the existing code into a codec (e.g.
"unicode-repr") and leave it setup as default.
Users who wish to see non-ASCII repr(unicode) data can then adjust the
used encoding to their liking.
This is both more flexible and backwards compatible with 2.x.
Also note that the separation of the Unicode database from the
interpreter core was done to keep the interpreter footprint manageable.
It's not a good idea to just dump the complete table set into
unicodeobject.c via an #include. If you need to reference APIs from
modules in C, the usual approach is to create a PyCObject which is then
exported by the module (see e.g. the datetime module) and imported by
code needing it.
BTW: "printable" is not a defined term in Unicode. What is or is not
printable really depends on the use case, e.g. there are quite a few
code points in Unicode that don't result in any glyph being "printed" to
the screen. A Unicode string could then look as if it had fewer code
points than it actually does - which is not really what you want when
debugging code or sifting through log files.
atsuoishimoto commented on Apr 17, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
If you do want to have this more flexible, then make the encoding used
by unicode_repr() adjustable, turn the existing code into a codec (e.g.
"unicode-repr") and leave it setup as default.
Turning code in unicode_repr() into a codec is good idea. I'll write two
codecs(existing repr and new Unicode friendly codec) and post a revised
patch later.
atsuoishimoto commented on Apr 18, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
Is a codec which encode() returns an Unicode allowed in Python3? I
started to think codec is not nessesary, but python function is enough.
16 remaining items
atsuoishimoto commented on Jun 3, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
This patch contains following changes.
Added the new C API PyObject_ASCII() for consistency.
Added the new string formatting operater for str.format() and
PyUnicode_FromFormat.
atsuoishimoto commented on Jun 3, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
Thank you for your review!
I filed a new patch just before I see your comments.
Why is an empty string not printable? In any case, the empty string
should be among the test cases for isprintable().
Well, my intuition came from str.islower() was wrong. An empty string is
printable, of cource.
Why not use PyUnicode_DecodeASCII instead of
PyUnicode_FromEncodedObject? It should be a bit faster.
Okay, thank you.
If old-style string formatting gets "%a", .format() must get a "!a"
specifier.
I added the format string in my latest patch.
The ascii() and repr() tests should be expanded so that both test the
same set of objects, and the expected differences. Are there tests for
failing cases?
Okay, thank you.
This is just "return ascii" (in builtin_ascii):
if (ascii == NULL)
return NULL;
return ascii;
Fixed in my latest patch.
For PyBool_FromLong(1) and PyBool_FromLong(0) there is Py_RETURN_TRUE
and Py_RETURN_FALSE. (You're not to blame, the rest of unicodeobject.c
seems to use them too, probably a legacy.)
Okay, thank you.
There appear to be some space indentations in tab-indented files like
bltinmodule.c and vice versa (unicodeobject.c).
I think bltinmodule.c is fixed with latest patch, but I don't know what
is correct indentation for unicodeobject.c. I guess latest patch is
acceptable.
C docs/isprintable() docs: The spec
Characters defined in the Unicode character database as "Other"
or "Separator" other than ASCII space(0x20) are not considered
printable.
is unclear, better say "All character except those ... are considered
printable".
ascii() docs:
the non-ASCII
characters in the string returned by :func:`ascii`() are hex-escaped
to generate a same string as :func:`repr` in Python 2.
should be
"the non-ASCII characters in the string returned by :func:`repr` are
backslash-escaped (with ``\x``, ``\u`` or ``\U``) to generate ...".
Okay, thank you.
makeunicodedata: len(list(n for n in names if n is not None)) could
better be expressed as sum(1 for n in names if n is not None).
I don't want to change here, because this is reversion of rev 63378.
One more thing: with r63891 the encoding and errors arguments for the
creation of sys.stderr were made configurable; you'll have to adapt the
patch so that it defaults to backslashescape but can be overridden by
PYTHONIOENCODING.
I think sys.stderr should be default to 'backslashreplace' always. I'll
post a messege to Py3k-list later.
Otherwise, the patch is fine IMO. (I'm surprised that only so few tests
needed adaptation, that's a sign that we're not testing Unicode enough.)
Thank you very much! I'll file new patch soon.
atsuoishimoto commented on Jun 3, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
BTW, are new C APIs and functions should be ported to Python 2.6 for
compatibility, without modifing repr() itself? If so, I'll prepare a
patch for Python 2.6.
Whether the C API stuff and .isprintable() should be backported to 2.6
is something for Guido to decide.
No way -- while all of this makes sense in Py3k, where all strings are
Unicode, it would cause no end of problems in 2.6, and it would break
backward compatibility badly.
atsuoishimoto commented on Jun 4, 2008
atsuoishimotomannequin
MannequinAuthor
More actions
stringlib can be compiled for Python 2.6 now, but the '!a' converter is
disabled by #ifdef for now.
Activity
atsuoishimoto commentedon Apr 14, 2008
In py3k, repr() escapes non-ASCII characters in Unicode to \uXXXX as
Python 2. This is unpleasant feature if you are working with non-latin
characters. This issue was once discussed by Hye-Shik Chang[1], but was
rejected. Here's a new challenge for Python 3 to fix issue.
In this patch, repr() converts special ascii characters such as "\t",
"\r", "\n", but doesn't convert non-ASCII characters to \uXXXX form.
Non-ASCII characters are converted by TextIOWrapper on printing. I set
'errors' attribute of sys.stdout and sys.stderr to 'backslashreplace', so
un-printable characters are converted to '\uXXXX' if your console
cannot print such characters.
This patch breaks five regr tests on my environment.
I'll fix these tests if this patch is acceptable.
[1] http://mail.python.org/pipermail/python-dev/2002-October/029443.html
http://bugs.python.org/issue479898
gvanrossum commentedon Apr 14, 2008
I think this has potential, but it is too liberal. There are many more
characters that cannot be assumed printable, e.g. many of the Latin-1
characters in the range 0x80 through 0x9F. Isn't there some Unicode
data table that shows code points that are safely printable?
OTOH there are other potential use cases where it would be nice to see
the \u escapes, e.g. when one is concerned about sequences that print
the same but don't have the same content (e.g. pre-normalization).
The backslashreplace trick is nice, I didn't even know about that. :-)
amauryfa commentedon Apr 14, 2008
What if we turn on the backslashreplace trick for some operations only?
For example: sys_displayhook and sys_excepthook.
atsuoishimoto commentedon Apr 15, 2008
As Michael Urman pointed out, we can use Unicode properties.
Or we can define a set of non-printable characters (e.g.
sys.nonprintablechars).
For such cases, print(s.encode("ascii", "backslashreplace")) might work.
atsuoishimoto commentedon Apr 15, 2008
It would be difficult, since *_repr() API don't know who is the caller.
gvanrossum commentedon Apr 15, 2008
Atsuo: I missed Michael Urman's comment. Can you copy it here, or
(better :-) write a patch that uses it?
Amaury: I think it would be okay to use backslashreplace as the default
error handler for sys.stderr. Probably not for sys.stdout or other
files, since I'm sure many users prefer the errors when their data
cannot be printed rather than silently writing \u escapes that might
cause other code reading their output to choke. For sys.stderr though I
think not having exceptions raised when attempting to print errors is
very valuable.
atsuoishimoto commentedon Apr 15, 2008
Okay, I'll revise a patch later today.
atsuoishimoto commentedon Apr 15, 2008
I revised a patch against Python 3.0a4.
As-per suggestion from Michael Urman, unicode_repr()
refers unicode database to determine characters to be
hex-encoded.
sys.stdout doesn't use 'backslashreplace'.
atsuoishimoto commentedon Apr 16, 2008
I think sys.stdout need to have backslashreplace error handler.
Without backslashreplace, print(listOfJapaneseString) prints nothing,
but raises an exception. This is worse than Python2.
gvanrossum commentedon Apr 16, 2008
I don't think this is a good idea; I've explained why earlier on this issue.
atsuoishimoto commentedon Apr 16, 2008
Sorry, I missed to write "for interactive session".
I agree for sys.stdout and other files should not have default
backslashescape, but for iteractive session, I think sys.stdout can
have backslasespape handler to avoid exceptions.
malemburg commentedon Apr 16, 2008
While it may be desirable to to have repr(unicode) return a non-ASCII
string, the suggested approach is not suitable to solve the problem.
repr() is usually used in logging and applications/users/tools don't
expect to suddenly find non-ASCII or even mixed encodings in a log file.
If you do want to have this more flexible, then make the encoding used
by unicode_repr() adjustable, turn the existing code into a codec (e.g.
"unicode-repr") and leave it setup as default.
Users who wish to see non-ASCII repr(unicode) data can then adjust the
used encoding to their liking.
This is both more flexible and backwards compatible with 2.x.
Also note that the separation of the Unicode database from the
interpreter core was done to keep the interpreter footprint manageable.
It's not a good idea to just dump the complete table set into
unicodeobject.c via an #include. If you need to reference APIs from
modules in C, the usual approach is to create a PyCObject which is then
exported by the module (see e.g. the datetime module) and imported by
code needing it.
BTW: "printable" is not a defined term in Unicode. What is or is not
printable really depends on the use case, e.g. there are quite a few
code points in Unicode that don't result in any glyph being "printed" to
the screen. A Unicode string could then look as if it had fewer code
points than it actually does - which is not really what you want when
debugging code or sifting through log files.
atsuoishimoto commentedon Apr 17, 2008
Turning code in unicode_repr() into a codec is good idea. I'll write two
codecs(existing repr and new Unicode friendly codec) and post a revised
patch later.
atsuoishimoto commentedon Apr 18, 2008
Is a codec which encode() returns an Unicode allowed in Python3? I
started to think codec is not nessesary, but python function is enough.
16 remaining items
atsuoishimoto commentedon Jun 3, 2008
This patch contains following changes.
PyUnicode_FromFormat.
atsuoishimoto commentedon Jun 3, 2008
Thank you for your review!
I filed a new patch just before I see your comments.
On Tue, Jun 3, 2008 at 7:13 PM, Georg Brandl <report@bugs.python.org> wrote:
Well, my intuition came from str.islower() was wrong. An empty string is
printable, of cource.
Okay, thank you.
I added the format string in my latest patch.
Okay, thank you.
Fixed in my latest patch.
Okay, thank you.
I think bltinmodule.c is fixed with latest patch, but I don't know what
is correct indentation for unicodeobject.c. I guess latest patch is
acceptable.
Okay, thank you.
I don't want to change here, because this is reversion of rev 63378.
I think sys.stderr should be default to 'backslashreplace' always. I'll
post a messege to Py3k-list later.
Thank you very much! I'll file new patch soon.
atsuoishimoto commentedon Jun 3, 2008
BTW, are new C APIs and functions should be ported to Python 2.6 for
compatibility, without modifing repr() itself? If so, I'll prepare a
patch for Python 2.6.
birkenfeld commentedon Jun 3, 2008
ascii() should probably be in future_builtins.
Whether the C API stuff and .isprintable() should be backported to 2.6
is something for Guido to decide.
atsuoishimoto commentedon Jun 3, 2008
I updated the patch as per Georg's advice.
atsuoishimoto commentedon Jun 3, 2008
I'm sorry, I missed a file to be uploaded. diff7_1.txt is correct file.
gvanrossum commentedon Jun 3, 2008
No way -- while all of this makes sense in Py3k, where all strings are
Unicode, it would cause no end of problems in 2.6, and it would break
backward compatibility badly.
atsuoishimoto commentedon Jun 4, 2008
stringlib can be compiled for Python 2.6 now, but the '!a' converter is
disabled by #ifdef for now.
pitrou commentedon Jun 4, 2008
Shall the method be called isprintable() or simply printable()? For the
record, in the io classes, the writable()/readable() convention was chosen.
birkenfeld commentedon Jun 4, 2008
I would expect "abc".isprintable() give me a bool and "abc".printable()
to return a printable string, as with "abc".lower() and "abc".islower().
pitrou commentedon Jun 4, 2008
You are right, I had forgotton about lower()/islower().
birkenfeld commentedon Jun 11, 2008
Patch committed to Py3k branch in r64138. Thanks all!
atsuoishimoto commentedon Jun 12, 2008
Great, thank you!