Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subprocess.run: add an extra_env kwarg to complement existing env kwarg #84038

Closed
vapier mannequin opened this issue Mar 5, 2020 · 7 comments
Closed

subprocess.run: add an extra_env kwarg to complement existing env kwarg #84038

vapier mannequin opened this issue Mar 5, 2020 · 7 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@vapier
Copy link
Mannequin

vapier mannequin commented Mar 5, 2020

BPO 39857
Nosy @gpshead, @vapier, @cjrh, @brandtbucher

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-03-10.20:32:27.088>
created_at = <Date 2020-03-05.02:10:18.152>
labels = ['type-feature', 'library', '3.9']
title = 'subprocess.run: add an extra_env kwarg to complement existing env kwarg'
updated_at = <Date 2020-03-10.20:32:27.087>
user = 'https://github.com/vapier'

bugs.python.org fields:

activity = <Date 2020-03-10.20:32:27.087>
actor = 'gregory.p.smith'
assignee = 'none'
closed = True
closed_date = <Date 2020-03-10.20:32:27.088>
closer = 'gregory.p.smith'
components = ['Library (Lib)']
creation = <Date 2020-03-05.02:10:18.152>
creator = 'vapier'
dependencies = []
files = []
hgrepos = []
issue_num = 39857
keywords = []
message_count = 7.0
messages = ['363413', '363621', '363624', '363627', '363805', '363818', '363843']
nosy_count = 4.0
nosy_names = ['gregory.p.smith', 'vapier', 'cjrh', 'brandtbucher']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue39857'
versions = ['Python 3.9']

@vapier
Copy link
Mannequin Author

vapier mannequin commented Mar 5, 2020

a common idiom i run into is wanting to add/set one or two env vars when running a command via subprocess. the only thing the API allows currently is inherting the current environment, or specifying the complete environment. this means a lot of copying & pasting of the pattern:

 env = os.environ.copy()
 env['FOO'] = ...
 env['BAR'] = ...
 subprocess.run(..., env=env, ...)

it would nice if we could simply express this incremental behavior:

 subprocess.run(..., extra_env={'FOO': ..., 'BAR': ...}, ...)

then the subprocess API would take care of copying & merging.

 if extra_env:
   assert env is None
   env = os.environ.copy()
   env.update(extra_env)

this is akin to subprocess.run's capture_output shortcut.

it's unclear to me whether this would be in both subprocess.Popen & subprocess.run, or only subprocess.run. it seems like subprocess.Popen elides convenience APIs.

@vapier vapier mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Mar 5, 2020
@cjrh
Copy link
Mannequin

cjrh mannequin commented Mar 8, 2020

dict syntax tools make it fairy easy to compose new dicts from old ones with overrides:

    subprocess.run(..., env={**os.environ, 'FOO': ..., 'BAR', ...}, ...)

Would this be sufficient to avoid the copy/pasting boilerplate?

@brandtbucher
Copy link
Member

Caleb's answer, using PEP-584's merge operator:

    newenv = os.environ | {'FOO': ..., 'BAR': ...}
    subprocess.run(..., env=new_env, ...)

@brandtbucher
Copy link
Member

Ah, I didn't realize that os.environ and os.environ b aren't dict subclasses. I've added a ticket to update them with the new operators!

@vapier
Copy link
Mannequin Author

vapier mannequin commented Mar 10, 2020

personally i still like having the extra_env setting explicitly broken out, but i agree that those operators help ease the majority of the pain. i hadn't come across them before as they aren't in Python 2.

i wouldn't be upset if people declined the FR considering those options will be available in Python 3.9+. thx for the tips.

@cjrh
Copy link
Mannequin

cjrh mannequin commented Mar 10, 2020

The dict unpacking generalizations that I posted were added in Python 3.5, which is pretty old by now. (But, true, is in Python 3 and not Python 2). This is the PEP: https://www.python.org/dev/peps/pep-0448/

The new syntax that Brandt posted will indeed only be available from 3.9 on.

@gpshead
Copy link
Member

gpshead commented Mar 10, 2020

I think those dict unpacking and merging options are sufficient that adding an extra_env= parameter would merely complicate the already over complex API.

Thanks for the suggestions folks!

@gpshead gpshead closed this as completed Mar 10, 2020
@gpshead gpshead closed this as completed Mar 10, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants