[issue46153] function fails in exec when locals is given
Quentin Peter added the comment: Maybe a note could be added to https://docs.python.org/3/library/functions.html#exec Something along the lines of: Note: If exec gets two separate objects as `globals` and `locals`, the code will not be executed as if it were embedded in a function definition. For example, any function or comprehension defined at the top level will not have access to the `locals` scope. PS: It would be nice for my usecase to have a way around this, maybe a flag in `compile` or `exec` that would produce "function code" instead of "module code". My workaround for this problem consist in wrapping my code in a function definition. I think this means https://bugs.python.org/issue41918 should be closed as well? -- ___ Python tracker <https://bugs.python.org/issue46153> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue46153] function fails in exec when locals is given
Quentin Peter added the comment: Thank you for your explaination. Just to be sure, it is expected that: exec("a = 1\ndef f(): return a\nprint(f())", {}) Runs successfully but exec("a = 1\ndef f(): return a\nprint(f())", {}, {}) Doesn't? -- ___ Python tracker <https://bugs.python.org/issue46153> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue46153] function fails in exec when locals is given
Quentin Peter added the comment: The reason I am asking is that I am working on a debugger. The debugger stops on a frame which is inside a function. Let's say the locals is: locals() == {"a": 1} I now want to define a closure with exec. I might want to do something like: exec("def f(): return a", globals(), locals()) But this doesn't work because of the issue I describe.I would expect f() to look for a in the locals(). Even more surprising is that if I use the second argument of exec, the code in the above comment starts to fail. -- ___ Python tracker <https://bugs.python.org/issue46153> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue46153] closure fails in exec when locals is given
Quentin Peter added the comment: This might be related to https://bugs.python.org/issue41918 -- ___ Python tracker <https://bugs.python.org/issue46153> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue46153] closure fails in exec when locals is given
New submission from Quentin Peter : When both namespace arguments are given to exec, function definitions fail to capture closure. See below: ``` Python 3.8.6 (default, Oct 8 2020, 14:06:32) [Clang 12.0.0 (clang-1200.0.32.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> exec("a = 1\ndef f(): return a\nprint(f())") 1 >>> exec("a = 1\ndef f(): return a\nprint(f())", {}) 1 >>> exec("a = 1\ndef f(): return a\nprint(f())", {}, {}) Traceback (most recent call last): File "", line 1, in File "", line 3, in File "", line 2, in f NameError: name 'a' is not defined >>> ``` -- messages: 409038 nosy: qpeter priority: normal severity: normal status: open title: closure fails in exec when locals is given type: crash versions: Python 3.8 ___ Python tracker <https://bugs.python.org/issue46153> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue42170] splitdrive fails for UNC path with the "\\?\UNC\" prefix.
Change by Quentin Peter : -- keywords: +patch pull_requests: +21916 stage: -> patch review pull_request: https://github.com/python/cpython/pull/23001 ___ Python tracker <https://bugs.python.org/issue42170> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue42170] splitdrive fails for UNC path with the "\\?\UNC\" prefix.
New submission from Quentin Peter : Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 7.18.1 -- An enhanced Interactive Python. In [1]: import os.path In [2]: os.path.splitdrive(r"\\machine\mountpoint\directory") Out[2]: ('machine\\mountpoint', '\\directory') In [3]: os.path.splitdrive(r"\\?\UNC\machine\mountpoint\directory") Out[3]: ('?\\UNC', '\\machine\\mountpoint\\directory') In [4]: -- ___ Python tracker <https://bugs.python.org/issue42170> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue42170] splitdrive fails for UNC path with the "\\?\UNC\" prefix.
Change by Quentin Peter : -- components: Library (Lib) nosy: qpeter priority: normal severity: normal status: open title: splitdrive fails for UNC path with the "\\?\UNC\" prefix. type: behavior versions: Python 3.7 ___ Python tracker <https://bugs.python.org/issue42170> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue41918] exec fails to take locals into account when running list comprehensions or functions
Quentin Peter added the comment: Fails for functions as well: ``` In [4]: exec(compile('print(my_var)\ndef a():\n print(my_var)\na()', '', 'exec'), globals(), {"my_var": 0}) 0 Traceback (most recent call last): File "", line 1, in exec(compile('print(my_var)\ndef a():\n print(my_var)\na()', '', 'exec'), globals(), {"my_var": 0}) File "", line 4, in File "", line 3, in a NameError: name 'my_var' is not defined ``` -- title: exec fails to take locals into account when running list comprehensions -> exec fails to take locals into account when running list comprehensions or functions ___ Python tracker <https://bugs.python.org/issue41918> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue41918] exec fails to take locals into account when running list comprehensions
New submission from Quentin Peter : The exec function fails to take locals into account when executing a list comprehension: ``` Python 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> exec(compile('[my_var for i in range(1)]\n', '', 'single'), >>> {**globals(), "my_var": 0}, None) [0] >>> exec(compile('[my_var for i in range(1)]\n', '', 'single'), >>> globals(), {"my_var": 0}) Traceback (most recent call last): File "", line 1, in File "", line 1, in File "", line 1, in NameError: name 'my_var' is not defined >>> ``` This is the cause of https://bugs.python.org/issue21161 -- messages: 377862 nosy: qpeter priority: normal severity: normal status: open title: exec fails to take locals into account when running list comprehensions type: enhancement versions: Python 3.7 ___ Python tracker <https://bugs.python.org/issue41918> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue31466] No easy way to change float formatting when subclassing encoder.JSONEncoder
Changes by Quentin Peter : -- keywords: +patch pull_requests: +3561 stage: -> patch review ___ Python tracker <https://bugs.python.org/issue31466> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue31466] No easy way to change float formatting when subclassing encoder.JSONEncoder
New submission from Quentin Peter: I want to output floats in ENG format. Working with distance in micrometers, it is a bit annoying to see: 2.5e-5 .0003 instead of 25e-6 300e-6 The solution I found was to redefine `iterencode` but that doesn't feel right. I would like to see something similar to the "default" function that is easely modified. -- messages: 302162 nosy: qpeter priority: normal severity: normal status: open title: No easy way to change float formatting when subclassing encoder.JSONEncoder type: enhancement versions: Python 3.6 ___ Python tracker <https://bugs.python.org/issue31466> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com