[issue36426] exec() issue when used inside function

2019-03-25 Thread Domen Jurkovič

New submission from Domen Jurkovič :

I've reported a stack overflow question and no reasonable explation was 
offered. Here is what I've discovered:
.. code-block:: python
def func():
varName = 'bar'
varValue = 42
localVarToEvaluate = varName + ' = varValue'

try:
exec(localVarToEvaluate)
except Exception as err:
print(str(err))

if 'bar' in locals():
# print(locals()['bar']) # (1) OK
# print(bar)  # (2) ERR
#print("'bar' OK:", bar)  # (3) ERR
pass # uncomment any line above

func()

After ``exec()`` is executed, ``bar`` can be seen in ``locals()``, but not 
accessible by intereter. Also, It can be accessed by directly calling 
``print(locals()['bar'](``, but not ``print(bar)``.

This is the problem as long as the code is wrapped in function. If the same 
code is placed in the module body, works as expected. 

Is there any exaplanation for such behaviour, or is this a bug?

--
components: Interpreter Core, Windows
messages: 338812
nosy: paul.moore, schperplata, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: exec() issue when used inside function
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7

___
Python tracker 
<https://bugs.python.org/issue36426>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36426] exec() issue when used inside function

2019-03-25 Thread Domen Jurkovič

Domen Jurkovič  added the comment:

Seems like I don't know how to write a code here. 
Anyway, issue created on stack overflow can be found on:
https://stackoverflow.com/questions/55239875/python-exec-function-broken-in-versions-above-2-7-error-name-not-defined/5524?noredirect=1#comment97362021_5524

Works on 2.7, fails on everything above 3.x

--

___
Python tracker 
<https://bugs.python.org/issue36426>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36426] exec() issue when used inside function

2019-03-30 Thread Domen Jurkovič

Domen Jurkovič  added the comment:

Are there any workarounds - how can we use exec() and on-the-fly created 
variables?
Also, why 'locals()['bar']' is seen by the interpretter, but not ony 'bar'?

--

___
Python tracker 
<https://bugs.python.org/issue36426>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com