Python Forum
documentation is wrong for exec() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: documentation is wrong for exec() (/thread-20089.html)



documentation is wrong for exec() - Skaperen - Jul-27-2019

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?


RE: documentation is wrong for exec() - Gribouillis - Jul-27-2019

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?


RE: documentation is wrong for exec() - Skaperen - Jul-27-2019

no. it's in a PDF. what does your documentation say?


RE: documentation is wrong for exec() - perfringo - Jul-27-2019

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.



RE: documentation is wrong for exec() - Gribouillis - Jul-27-2019

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?


RE: documentation is wrong for exec() - Skaperen - Jul-27-2019

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



RE: documentation is wrong for exec() - Gribouillis - Jul-28-2019

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.


RE: documentation is wrong for exec() - Skaperen - Jul-28-2019

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


RE: documentation is wrong for exec() - buran - Jul-28-2019

(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-reproducible-example


RE: documentation is wrong for exec() - Skaperen - Jul-28-2019

i knew those words, just not the acronym.