This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Python 3.6.3 command script wrapped in single quotes produces NameError: name 'A' is not defined
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Tim McDonough, benjamin.peterson, izbyshev, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2018-10-02 21:38 by Tim McDonough, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg326916 - (view) Author: Tim McDonough (Tim McDonough) Date: 2018-10-02 21:38
I found an odd behavior that seems to be limited to Python 3.6.3. 
Python 3.6.3 command scripts seem to prefer wrapping in double quotes instead of single quotes.

Here is an example of the error.  

$ echo -n '{"A":"a"}' | python3 -c 'import sys,json; j=json.load(sys.stdin); print(j["A"])'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'A' is not defined

# Swapping single and double quotes works as expected.
$ echo -n '{"A":"a"}' | python3 -c "import sys,json; j=json.load(sys.stdin); print(j['A'])"

# Executing the original, failing script against python 2.6.6 works.
$ echo -n '{"A":"a"}' | python2 -c 'import sys,json; j=json.load(sys.stdin); print(j["A"])'

The failing environment is:
$ bash --version
GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23)

These python versions also seem to work fine: 2.6.6, 2.7.9, 3.5.1, and 3.5.2.
Assigning the script to an environment variable follows the single/double quote issue. For example, this fails:

$ export python_script='import sys,json; j=json.load(sys.stdin); print(j["A"])'
$ echo -n '{"A": "a"}' | python3 -c "${python_script}"

This would be a good unit test candidate if not already present.
msg326934 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-10-03 05:05
I cannot reproduce this problem with exactly Python 3.6.3. This may be some strange situation specific to your configuration, which should be supported to RedHat.
msg326937 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-03 05:59
I can reproduce this problem when use double quotes as both outer and internal quotes.

$ echo -n '{"A":"a"}' | python3 -c "import sys,json; j=json.load(sys.stdin); print(j["A"])"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'A' is not defined

This has not relation to Python, this is how the quoting in the shell works. Perhaps something in your configuration makes single quotes be interpreted as double quotes.
msg327013 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-10-03 20:29
@Tim Is it possible that 'python3' in your command refers to some wrapper which forwards its arguments to real Python in a wrong way?
msg327023 - (view) Author: Tim McDonough (Tim McDonough) Date: 2018-10-04 00:34
Yes, there are wrapper scripts on my system.  My system was updated from 3.3 to 3.6.  The 3.3 and 3.6 wrappers are equivalent and similar to the python2.7 wrapper as shown:  

exec -a `dirname $realpath`/python2.7 `dirname $realpath`/python2.7.real "$@"
exec /usr/bin/scl enable python33 -- python "$@"
exec /usr/bin/scl enable rh-python36 -- python "$@"

This has to be something specific to the rh-python36 package or python36-scl that replaced python33 on my system.  Since Benjamin could not reproduce with 3.6.3 my bet that it is an in-house, self-inflicted problem.

Thank you.  I am closing this one as "not a bug" in python 3.6.
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79055
2018-10-04 00:34:53Tim McDonoughsetstatus: open -> closed
resolution: not a bug
messages: + msg327023

stage: resolved
2018-10-03 20:29:32izbyshevsetnosy: + izbyshev
messages: + msg327013
2018-10-03 05:59:18serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg326937
2018-10-03 05:05:55benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg326934
2018-10-03 00:20:03xtreaksetnosy: + xtreak
2018-10-02 21:38:40Tim McDonoughcreate