[sonic-py-common] Add 'universal_newlines=True' arg to all Popen calls (#5919)
The behavior of `subprocess.Popen()` changed in Python 3 such that stdin, stdout and stderr are treated as bytes by default. Adding the `universal_newlines=True` argument changes this behavior to return strings, matching the behavior of Python 2. The change is backward-compatible with Python 2, as well.
This commit is contained in:
parent
261a81d379
commit
ced11615a4
@ -407,7 +407,7 @@ def get_system_mac(namespace=None):
|
|||||||
hw_mac_entry_cmds = [mac_address_cmd]
|
hw_mac_entry_cmds = [mac_address_cmd]
|
||||||
|
|
||||||
for get_mac_cmd in hw_mac_entry_cmds:
|
for get_mac_cmd in hw_mac_entry_cmds:
|
||||||
proc = subprocess.Popen(get_mac_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
proc = subprocess.Popen(get_mac_cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
(mac, err) = proc.communicate()
|
(mac, err) = proc.communicate()
|
||||||
if err:
|
if err:
|
||||||
continue
|
continue
|
||||||
@ -439,6 +439,7 @@ def get_system_routing_stack():
|
|||||||
proc = subprocess.Popen(command,
|
proc = subprocess.Popen(command,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
shell=True,
|
shell=True,
|
||||||
|
universal_newlines=True,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
stdout = proc.communicate()[0]
|
stdout = proc.communicate()[0]
|
||||||
proc.wait()
|
proc.wait()
|
||||||
@ -473,7 +474,7 @@ def is_warm_restart_enabled(container_name):
|
|||||||
def is_fast_reboot_enabled():
|
def is_fast_reboot_enabled():
|
||||||
fb_system_state = 0
|
fb_system_state = 0
|
||||||
cmd = 'sonic-db-cli STATE_DB get "FAST_REBOOT|system"'
|
cmd = 'sonic-db-cli STATE_DB get "FAST_REBOOT|system"'
|
||||||
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
|
proc = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE)
|
||||||
(stdout, stderr) = proc.communicate()
|
(stdout, stderr) = proc.communicate()
|
||||||
|
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
|
@ -149,6 +149,7 @@ def get_current_namespace():
|
|||||||
proc = subprocess.Popen(command,
|
proc = subprocess.Popen(command,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
shell=True,
|
shell=True,
|
||||||
|
universal_newlines=True,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
try:
|
try:
|
||||||
stdout, stderr = proc.communicate()
|
stdout, stderr = proc.communicate()
|
||||||
|
Reference in New Issue
Block a user