Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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?
Quote
#3
no. it's in a PDF. what does your documentation say?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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?
Quote
#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') >>>
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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().
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Skaperen likes this post
Quote
#10
i knew those words, just not the acronym.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)