• backslash in triple quoted string

    From Bob van der Poel@21:1/5 to All on Wed May 7 20:20:27 2025
    Did something change in python buggering up my use of a "\ " sequence in a triple quoted string?

    I have yet to go through my archive on the program, but I tried to run it
    today and it crashed quite spectacularly when it hit a """ .... """ line
    being used as a comment at the top of a function. I changed the "\" to a
    "/" and all is well now.


    --

    **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
    Bob van der Poel ** Wynndel, British Columbia, CANADA **
    EMAIL: bob@mellowood.ca
    WWW: http://www.mellowood.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Thu May 8 16:09:35 2025
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    A "more formal specification" can be found in, maybe (if I found
    the right place), the function definition "_PyBytes_DecodeEscape"
    in the source file "Objects\bytesobject.c":

    See also:

    Grammar/python.gram
    Grammar/Tokens

    in the CPython sources, which does not seem to speak about
    escapes in string literals, though.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Left Right on Thu May 8 15:46:08 2025
    Left Right <olegsivokon@gmail.com> wrote or quoted:
    Also, it appears that the change linked above is a lie:

    The Python documentation is a bit, "informal".

    A "more formal specification" can be found in, maybe (if I found
    the right place), the function definition "_PyBytes_DecodeEscape"
    in the source file "Objects\bytesobject.c":

    |switch (*s++) {
    |/* XXX This assumes ASCII! */
    |case '\n': break;
    |case '\\': *p++ = '\\'; break;
    |case '\'': *p++ = '\''; break;
    |case '\"': *p++ = '\"'; break;
    . . .

    . In 2015, Jakub Przywoski started to write a new specification:

    |Python is such a well-designed, clean and enjoyable to code in
    |language so it sure deserves to have a decent syntax reference.

    . It seems, however, that he was not yet able to finish his project
    or to update it to newer versions of Python. It would be a full-time
    project I think!

    Effectively, Python is what CPython does.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Left Right via Python-list on Sun May 11 11:58:58 2025
    On 2025-05-08 08:05:54 +0200, Left Right via Python-list wrote:
    Also, it appears that the change linked above is a lie:

    Such strong words ...


    https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-python-grammar-longstringitem

    According to the grammar, any character can follow backslash in a
    valid Python program. The warning / error raised by this code should
    not be a syntax error / warning because the syntax is correct.

    Warnings are about technically correct but probably unintended usage.

    The documentation you linked to describes (a bit further down) which
    escape sequences are recognized and what happens if you use an
    unrecognized escape sequence. It also mentions that using an
    unrecognized escape sequence *will* be an error in future versions of
    Python.

    A warning is appropriate here. It gives the programmer a chance to fix
    the program now before it breaks.

    One could argue that it should say 'unrecognized escape sequence'
    instead of 'invalid escape sequence', since it isn't invalid yet, but
    that's nitpicking.

    hjp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmggdNsACgkQ8g5IURL+ KF3CiQ/+M1YsR/nTAzRyauQ2RiUTR2hw3J3DFSpXSQhsvKvS7S1uPL1+kkBsoUCC 623bVF+5yrPsjAOJVm0S2ieT0465zvnBzDIqcTvKoNmHbAxc3HNEEUyORZ3PGLzN yG7TyUqTLgoHaMGbxEpPfZLQ3DLrslXVKX9JPpMoeDu5jaGah2fv1Y4opCU62Akj QlQP/9rj9k67CGPSr2ndnDu9fqL6v79p57IUC9/SVKOCCaYTG9Wz+LFIyvybP8qp nEfwU+qZw2tKKEn8r78JlP7WkpqBFV2vtmuvFCFsTYLPD7JHihyq9J8fiG1OKy5V t2EPCcFiXshcIUxgiWoEwhBD8NsuqLsvg9Z3z96BzJ8MDB6S6dbp0sLFOdpQiSPT xL7QRB3LvG8oyW63JNCkhuDhKhAf2yPCfezVAiZfcF6Ef3s+o+dv+L0alEXFTpJ1 YFVMlRv5+n/NLAZP7zdpEiNoRlZceTC955MznS40eK7Zc5QK99tzX1BYEBcTNeNR VmNmYma/a4FRo2mFiPFeAyTlDOOCw7G7a3NvRmchHrqWXTZ+WoPmDq/e1DeuDXgi 69Piv0YvwJ2BrN7Dj30R9DmC1Y8K5swg9aUJJNQw3Hamvzybwk94rhBFn1piViQv YAKAEH7NdGYssMgxEbMcqmCpavTVVen7ZxH4WNQ
  • From Richard Damon@21:1/5 to All on Sun May 11 07:16:05 2025
    On 05/11/2025 6:36 AM EDT Left Right via Python-list
    <[1]python-list@python.org> wrote:


    Then it just means that the grammar lies. The two claims are mutually
    exclusive, so either one is a lie or the other or both.



    No, it more points out that not all errors are grammatical. The grammar
    does not (and can not) fully define what is a legal program. Some forms of
    error are semantic, like undefined symbols.

    It appears that rather than try to make the grammar complicated enough to
    describe what is a valid string, that operation was moved into the
    semantics of a string, which simplifies the rules quite a bit.

    References

    Visible links
    1. mailto:python-list@python.org

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Left Right via Python-list on Sun May 11 17:13:34 2025
    On 2025-05-11 12:36:31 +0200, Left Right via Python-list wrote:
    Then it just means that the grammar lies.

    No, because the parser accepts the sequence. And it produces exactly
    what the description says.

    The program

    #!/usr/bin/python3

    print("start")
    for i in range(3):
    print("\copy")
    print("end")

    is valid and prints

    start
    \copy
    \copy
    \copy
    end

    to stdout, as you would expect from reading the docs.

    But in addition to that it also prints the *warning* (not error, that's
    a difference)

    /home/hjp/tmp/./foo:5: SyntaxWarning: invalid escape sequence '\c'
    print("\copy")

    to stderr (this is also documented).

    One can quibble over the exact wording of the warning ("unrecognized"
    would be more in line with the documentation than "invalid"), but there
    are reasons for the warning.


    The two claims are mutually exclusive, so either one is a lie or the
    other or both.

    No they are not. The grammar says that the program will compile and run,
    which it does. The warning tells you that this probably wasn't a good
    idea.

    Warnings about stuff that is technically correct aren't unusual.
    For example, in C something like

    #v+
    int a = 5, b = 3;
    if (a = b) {
    printf("a = %d\n", a);
    }
    #v-

    is totally legal (and will print "a = 3"). However, most compilers will
    warn about this because it is very likely that the programmer wanted to
    write ½if (a == b) ...╗.

    My comment was more of an irony really. It's plenty obvious that the
    grammar is a lie. The reason is that it's tedious to put the actual
    intender rules into the grammar, and so whoever wrote the grammar
    decided to cut corners. But, the grammar is supposed to be the
    authoritative source for how the language is parsed,

    Which it is.

    that's why even though it's clear that the grammar is a lie, blaming
    whoever doesn't follow it makes it ironic.

    In other words, the grammar author didn't put enough effort into
    making grammar actually work,

    But the grammar *does* describe how it actually works. You *can* write
    any character after a backslash, and your programm will compile and run.

    It is likely that in some future version of Python that will not be the
    case any more. *Then* the grammar must be changed. Until then the
    documentation should match the current implementation and not something
    which may or may not be implemented at some point in the future.

    but seeing how many other things are done in Python, this is not an exception. It would've been strange to have it done properly when
    "properly" means doing copious amounts of tedious work.

    As usual you're making up your lack of knowledge with an abundance of
    opinion.

    hjp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmggvpUACgkQ8g5IURL+ KF3XhA//WCebtFiblmx70chiX8tPHbM6bGbkHGK53Gejjn/QCmObeyorApVjMZRN bKuRQrbCnffEXFKC9A6mKV2LqFwqB7aEUQVSZZC9/hyTSUgsYWwowAgPrDzkvNHm vgqLQHFKwHTs+6ysvbf7PiiVTT6w9//3YWhDh30KT/gbppLzXnXWPsLLM111CheF j5Q+vlMZJSKgxdZQjPxBUN7079j2ajuDrwoCgspnGF9MlcnG+37sQ/jDFjlP1gMU Omr9HncQZ3gjjSytU2nvAcn406aufnawy3UamC67DYlUXJotu3UgLYJ/8DwFKDCZ 5XJzd86KxVxKvZXjlNkpOdF2lpnj40IB49ALcfUkiI/cnmf7DBvLbGT346awGfYo vCJPFCWbQgA/D1P8i0fHe65/sXpq5SDtEXADksmGpIh28MBgGCPHjjDobqMwiZ13 xsVYIY+Lr4hKTtzsv3R4OxbKpQFg1CRPr9hZd4rWlQMcXuw1h3To0uaGaxP6NnM6 JcOhU7odDs314E1taG189hgBYHwPjP+DTrEfIiMky59IQcKQfSvoE60vc2JK/ncg eHD+hED4ADJlNDdH9EQCqAw4xeTMYr760MoOmZICXsDQsDmLlhTqxWZRC403I+Wx KzkLYNHvLBMOlIZLjK0LoHShbrnNrvZQyhx1Jml
  • From Greg Ewing@21:1/5 to Left Right on Mon May 12 14:32:29 2025
    On 12/05/25 3:21 am, Left Right wrote:
    The point is that the error is wrong.
    It cannot be a syntax error and at the same time the program compiles.

    But the message doesn't say it's an error. It uses the word "warning",
    not "error". You're tilting at a straw horse here.

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ethan Furman@21:1/5 to All on Mon May 12 10:58:27 2025
    Chris and Oleg (sp?), please control your tempers; your latter posts added nothing useful to the conversation.

    (Apologies for the late reply, I was out of town.)

    --
    ~Ethan~
    Moderator

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to Left Right on Tue May 13 12:11:13 2025
    On 13/05/25 6:28 am, Left Right wrote:
    Read the associate release note.

    I take it you're referring to this:

    In a future Python version, SyntaxError will
    eventually be raised, instead of SyntaxWarning. (Contributed by Victor Stinner in gh-98401.)

    That doesn't contradict what I said. Currently it's a warning. If and
    when it becomes an error, presumably the grammar documentation will be
    updated to reflect that. If it isn't, you'll have cause to complain, but
    not before.

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Left Right via Python-list on Tue May 13 09:34:39 2025
    On 5/8/2025 2:05 AM, Left Right via Python-list wrote:
    Also, it appears that the change linked above is a lie:

    https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-python-grammar-longstringitem

    According to the grammar, any character can follow backslash in a
    valid Python program. The warning / error raised by this code should
    not be a syntax error / warning because the syntax is correct.

    "Changed in version 3.12: Unrecognized escape sequences produce a SyntaxWarning. In a future Python version they will be eventually a SyntaxError."

    See https://docs.python.org/3/reference/lexical_analysis.html#strings

    Test case for "\" in triple-quoted string:

    [code]
    def f():
    ... """\hello"""
    <stdin>:2: SyntaxWarning: invalid escape sequence '\h'
    ... print('hi')
    ...

    f()
    hi
    [/code]

    This example fits the documentation exactly. A SyntaxWarning is emitted
    for the unrecognized escape sequence "\h". The program runs as intended.

    Please let's drop all the OT back-and-forth.

    On Thu, May 8, 2025 at 7:54 AM Left Right <olegsivokon@gmail.com> wrote:

    I think it could be this:

    A backslash-character pair that is not a valid escape sequence now
    generates a SyntaxWarning, instead of DeprecationWarning. For example,
    re.compile("\d+\.\d+") now emits a SyntaxWarning ("\d" is an invalid
    escape sequence, use raw strings for regular expression:
    re.compile(r"\d+\.\d+")). In a future Python version, SyntaxError will
    eventually be raised, instead of SyntaxWarning. (Contributed by Victor
    Stinner in gh-98401.)

    Found in:
    https://docs.python.org/3/whatsnew/3.12.html#other-language-changes

    It's not supposed to crash your program though. If the program crashes
    because of it, it's a bug in Python.

    On Thu, May 8, 2025 at 7:00 AM Bob van der Poel via Python-list
    <python-list@python.org> wrote:

    Did something change in python buggering up my use of a "\ " sequence in a >>> triple quoted string?

    I have yet to go through my archive on the program, but I tried to run it >>> today and it crashed quite spectacularly when it hit a """ .... """ line >>> being used as a comment at the top of a function. I changed the "\" to a >>> "/" and all is well now.


    --

    **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
    Bob van der Poel ** Wynndel, British Columbia, CANADA **
    EMAIL: bob@mellowood.ca
    WWW: http://www.mellowood.ca
    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)