Python Forum
documentation is wrong for exec()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
documentation is wrong for exec()
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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?
Reply
#3
no. it's in a PDF. what does your documentation say?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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?
Reply
#6
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') >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
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.
Reply
#8
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().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
i knew those words, just not the acronym.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  exec() instead of functions Skaperen 5 2,452 Aug-03-2020, 11:12 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020