Skip to content

repr() should not escape non-ASCII characters #46882

Closed

Description

@atsuoishimoto
BPO 2630
Nosy @malemburg, @gvanrossum, @birkenfeld, @atsuoishimoto, @amauryfa, @pitrou, @ericvsmith
Files
  • diff.txt
  • diff2.txt
  • diff3.txt
  • diff4.txt
  • docdiff1.txt
  • diff5.txt
  • diff6.txt
  • diff7_1.txt
  • diff8.patch
  • 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 = None
    closed_at = <Date 2008-06-11.18:38:55.202>
    created_at = <Date 2008-04-14.09:54:22.958>
    labels = ['type-feature']
    title = 'repr() should not escape non-ASCII characters'
    updated_at = <Date 2008-06-12.02:44:45.865>
    user = 'https://github.com/atsuoishimoto'

    bugs.python.org fields:

    activity = <Date 2008-06-12.02:44:45.865>
    actor = 'ishimoto'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-06-11.18:38:55.202>
    closer = 'georg.brandl'
    components = ['None']
    creation = <Date 2008-04-14.09:54:22.958>
    creator = 'ishimoto'
    dependencies = []
    files = ['10028', '10034', '10193', '10447', '10456', '10491', '10507', '10512', '10518']
    hgrepos = []
    issue_num = 2630
    keywords = ['patch']
    message_count = 43.0
    messages = ['65461', '65470', '65483', '65490', '65491', '65493', '65494', '65514', '65535', '65536', '65542', '65564', '65573', '65601', '65606', '66216', '66298', '66299', '66302', '66303', '66307', '66310', '66320', '66424', '66425', '67409', '67439', '67591', '67651', '67653', '67654', '67655', '67656', '67657', '67665', '67667', '67670', '67692', '67702', '67704', '67705', '68008', '68047']
    nosy_count = 7.0
    nosy_names = ['lemburg', 'gvanrossum', 'georg.brandl', 'ishimoto', 'amaury.forgeotdarc', 'pitrou', 'eric.smith']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue2630'
    versions = ['Python 3.0']

    Activity

    1. atsuoishimoto commented on Apr 14, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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

    2. added
      type-feature
      on Apr 14, 2008
    3. gvanrossum commented on Apr 14, 2008

      @gvanrossum
      Member

      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. :-)

    4. amauryfa commented on Apr 14, 2008

      @amauryfa
      Contributor

      What if we turn on the backslashreplace trick for some operations only?
      For example: sys_displayhook and sys_excepthook.

    5. atsuoishimoto commented on Apr 15, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.

    6. atsuoishimoto commented on Apr 15, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.

    7. gvanrossum commented on Apr 15, 2008

      @gvanrossum
      Member

      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.

    8. atsuoishimoto commented on Apr 15, 2008

      atsuoishimotomannequin
      MannequinAuthor

      Okay, I'll revise a patch later today.

    9. atsuoishimoto commented on Apr 15, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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'.

    10. atsuoishimoto commented on Apr 16, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.

    11. gvanrossum commented on Apr 16, 2008

      @gvanrossum
      Member

      I don't think this is a good idea; I've explained why earlier on this issue.

    12. atsuoishimoto commented on Apr 16, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.

    13. malemburg commented on Apr 16, 2008

      @malemburg
      Member

      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.

    14. atsuoishimoto commented on Apr 17, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.

    15. atsuoishimoto commented on Apr 18, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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. 16 remaining items

    17. atsuoishimoto commented on Jun 3, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.
    18. atsuoishimoto commented on Jun 3, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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:

      Georg Brandl <georg@python.org> added the comment:

      Review:

      • 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.

    19. atsuoishimoto commented on Jun 3, 2008

      atsuoishimotomannequin
      MannequinAuthor

      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.

    20. birkenfeld commented on Jun 3, 2008

      @birkenfeld
      Member

      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.

    21. atsuoishimoto commented on Jun 3, 2008

      atsuoishimotomannequin
      MannequinAuthor

      I updated the patch as per Georg's advice.

    22. atsuoishimoto commented on Jun 3, 2008

      atsuoishimotomannequin
      MannequinAuthor

      I'm sorry, I missed a file to be uploaded. diff7_1.txt is correct file.

    23. gvanrossum commented on Jun 3, 2008

      @gvanrossum
      Member

      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.

    24. atsuoishimoto commented on Jun 4, 2008

      atsuoishimotomannequin
      MannequinAuthor

      stringlib can be compiled for Python 2.6 now, but the '!a' converter is
      disabled by #ifdef for now.

    25. pitrou commented on Jun 4, 2008

      @pitrou
      Member

      Shall the method be called isprintable() or simply printable()? For the
      record, in the io classes, the writable()/readable() convention was chosen.

    26. birkenfeld commented on Jun 4, 2008

      @birkenfeld
      Member

      I would expect "abc".isprintable() give me a bool and "abc".printable()
      to return a printable string, as with "abc".lower() and "abc".islower().

    27. pitrou commented on Jun 4, 2008

      @pitrou
      Member

      You are right, I had forgotton about lower()/islower().

    28. birkenfeld commented on Jun 11, 2008

      @birkenfeld
      Member

      Patch committed to Py3k branch in r64138. Thanks all!

    29. atsuoishimoto commented on Jun 12, 2008

      atsuoishimotomannequin
      MannequinAuthor

      Great, thank you!

    30. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      type-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions