`$COLUMNS` environment variable present in subprocess only when `env=None`

Open
#100,516 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
python

Research direction

Start with the minimal subprocess.check_output example and reproduce all four combinations of interactive versus non-interactive execution and env=None versus env=os.environ. Trace the subprocess environment handling from that entry point; done means the differing COLUMNS behavior is explained and either corrected or clearly documented with a corresponding test.

Written by the indexing model from the issue text.

Description

interpreter-core topic-subprocess type-bug

Bug report

for some reason which I cannot determine (perhaps a subtle difference between execvp and execvpe?) the $COLUMNS variable is mysteriously present in subprocesses when using env=None but not when using env=os.environ (which I expect to be identical in behaviour). this also seems to only happen with an interactive session?

here's a minimal case:

import sys, subprocess, os

subprocess.check_output(
    (sys.executable, '-c', 'import os; print(os.environ["COLUMNS"])'),
    # env=os.environ,
)
$ python3.11 -i t4.py
>>> 
$ python3.11 t4.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen os>", line 679, in __getitem__
KeyError: 'COLUMNS'
Traceback (most recent call last):
  File "/home/asottile/workspace/cpython/t4.py", line 3, in <module>
    subprocess.check_output(
  File "/usr/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '('/usr/bin/python3.11', '-c', 'import os; print(os.environ["COLUMNS"])')' returned non-zero exit status 1.

if I un-comment the line in the example script above -- it fails in both places:

$ sed -i 's/#//g' -- t4.py
$ python3.11 -i t4.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen os>", line 679, in __getitem__
KeyError: 'COLUMNS'
Traceback (most recent call last):
  File "/home/asottile/workspace/cpython/t4.py", line 3, in <module>
    subprocess.check_output(
  File "/usr/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '('/usr/bin/python3.11', '-c', 'import os; print(os.environ["COLUMNS"])')' returned non-zero exit status 1.
>>> 
$ python3.11 t4.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen os>", line 679, in __getitem__
KeyError: 'COLUMNS'
Traceback (most recent call last):
  File "/home/asottile/workspace/cpython/t4.py", line 3, in <module>
    subprocess.check_output(
  File "/usr/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '('/usr/bin/python3.11', '-c', 'import os; print(os.environ["COLUMNS"])')' returned non-zero exit status 1.

normally $COLUMNS is a shell variable and not an environment variable, but that doesn't really explain where it's coming from and why it's different depending both on interactiveness and on env= parameter:

env=None env=os.environ
-i

Your environment

  • CPython versions tested on: 3.12a3, 3.11.1, 3.10.6
  • Operating system and architecture: ubuntu 22.04, x86_64 -- I also tried in docker and it reproduces there as well -- kernel is 5.15.0-56-generic
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.