Python Forum

Full Version: documentation is wrong for exec()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
the documentation for exec() says the first argument must be a string or code object and that only the unix style end-of-line is accept. but i have found that it also works with bytes and works with both '\r\n' and '\r' style end of lines.

anyone know why?
Skaperen Wrote:the documentation for exec() says the first argument must be a string or code object and that only the unix style end-of-line is accept
Do you have a link to this part of the documentation?
no. it's in a PDF. what does your documentation say?
From python.org:

Quote:This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).

/.../

Note that the parser only accepts the Unix-style end of line convention. If you are reading the code from a file, make sure to use newline conversion mode to convert Windows or Mac-style newlines.
Oh I see, I missed the footnote. Well, Skaperen, can you send us an MCVE where it works unexpectedly with Windows or Mac end of lines?
MCVE? i don't know what that means.

here is a simple demo:
Output:
lt2a/forums /home/forums 45> py3 Python 3.6.8 (default, Jan 14 2019, 11:02:34) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a='print(repr((a,b,c,d,e,f)))\n' >>> b='print(repr((a,b,c,d,e,f)))\r' >>> c='print(repr((a,b,c,d,e,f)))\r\n' >>> d=b'print(repr((a,b,c,d,e,f)))\n' >>> e=b'print(repr((a,b,c,d,e,f)))\r' >>> f=b'print(repr((a,b,c,d,e,f)))\r\n' >>> exec(a) ('print(repr((a,b,c,d,e,f)))\n', 'print(repr((a,b,c,d,e,f)))\r', 'print(repr((a,b,c,d,e,f)))\r\n', b'print(repr((a,b,c,d,e,f)))\n', b'print(repr((a,b,c,d,e,f)))\r', b'print(repr((a,b,c,d,e,f)))\r\n') >>> exec(b) ('print(repr((a,b,c,d,e,f)))\n', 'print(repr((a,b,c,d,e,f)))\r', 'print(repr((a,b,c,d,e,f)))\r\n', b'print(repr((a,b,c,d,e,f)))\n', b'print(repr((a,b,c,d,e,f)))\r', b'print(repr((a,b,c,d,e,f)))\r\n') >>> exec(c) ('print(repr((a,b,c,d,e,f)))\n', 'print(repr((a,b,c,d,e,f)))\r', 'print(repr((a,b,c,d,e,f)))\r\n', b'print(repr((a,b,c,d,e,f)))\n', b'print(repr((a,b,c,d,e,f)))\r', b'print(repr((a,b,c,d,e,f)))\r\n') >>> exec(d) ('print(repr((a,b,c,d,e,f)))\n', 'print(repr((a,b,c,d,e,f)))\r', 'print(repr((a,b,c,d,e,f)))\r\n', b'print(repr((a,b,c,d,e,f)))\n', b'print(repr((a,b,c,d,e,f)))\r', b'print(repr((a,b,c,d,e,f)))\r\n') >>> exec(e) ('print(repr((a,b,c,d,e,f)))\n', 'print(repr((a,b,c,d,e,f)))\r', 'print(repr((a,b,c,d,e,f)))\r\n', b'print(repr((a,b,c,d,e,f)))\n', b'print(repr((a,b,c,d,e,f)))\r', b'print(repr((a,b,c,d,e,f)))\r\n') >>> exec(f) ('print(repr((a,b,c,d,e,f)))\n', 'print(repr((a,b,c,d,e,f)))\r', 'print(repr((a,b,c,d,e,f)))\r\n', b'print(repr((a,b,c,d,e,f)))\n', b'print(repr((a,b,c,d,e,f)))\r', b'print(repr((a,b,c,d,e,f)))\r\n') >>>
My idea is that the footnote is obsolete. You may have discovered a flaw in the official documentation. Now I think it's not our job to find an example of the footnote's claim. It should be included in the documentation.
i discovered this because i wanted to see what exception it would actually raise. but the fact that it can just do these things lets me simplify my code. i don't need to decode bytes to strings and i don't need to do '\n'.join(whatever.splitlines()) to be sure i have consistent line endings for code snippets to run in exec().
(Jul-27-2019, 10:56 PM)Skaperen Wrote: [ -> ]MCVE? i don't know what that means.
mcve = a minimal, complete and verifiable example
https://stackoverflow.com/help/minimal-r...le-example
i knew those words, just not the acronym.