Replace the use of the function string.replace with the method str.replace (#68793)

* Replace the use of the function string.replace() with the method str.replace()
because that's what works for both Python 2 and 3.

* Cleanup the unused string import.

Added a changelog fragment.

* The documentation for os.write() seems a bit iffy, but in Python 3 we
definitely cannot pass it a string and we need to encode it into
bytes.

The Python documentation at
https://docs.python.org/3/howto/pyporting.html#text-versus-binary-data
says that this code will work in Python 2 as well.

Co-authored-by: Lloyd Parkes <lloyd@must-have-coffee.gen.nz>
This commit is contained in:
Lloyd Parkes 2021-01-06 05:20:44 +13:00 committed by GitHub
parent a469a4455a
commit e0c75a6756
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- service - Fix for the BSD rcconf code using a Python 2 specific string replace function

View file

@ -135,7 +135,6 @@ import platform
import re import re
import select import select
import shlex import shlex
import string
import subprocess import subprocess
import tempfile import tempfile
import time import time
@ -419,7 +418,7 @@ class Service(object):
# Write out the contents of the list into our temporary file. # Write out the contents of the list into our temporary file.
for rcline in new_rc_conf: for rcline in new_rc_conf:
os.write(TMP_RCCONF, rcline) os.write(TMP_RCCONF, rcline.encode())
# Close temporary file. # Close temporary file.
os.close(TMP_RCCONF) os.close(TMP_RCCONF)
@ -1116,7 +1115,7 @@ class DragonFlyBsdService(FreeBsdService):
if os.path.isfile(rcfile): if os.path.isfile(rcfile):
self.rcconf_file = rcfile self.rcconf_file = rcfile
self.rcconf_key = "%s" % string.replace(self.name, "-", "_") self.rcconf_key = "%s" % self.name.replace("-", "_")
return self.service_enable_rcconf() return self.service_enable_rcconf()
@ -1274,7 +1273,7 @@ class NetBsdService(Service):
""" """
This is the NetBSD Service manipulation class - it uses the /etc/rc.conf This is the NetBSD Service manipulation class - it uses the /etc/rc.conf
file for controlling services started at boot, check status and perform file for controlling services started at boot, check status and perform
direct service manipulation. Init scripts in /etc/rcd are used for direct service manipulation. Init scripts in /etc/rc.d are used for
controlling services (start/stop) as well as for controlling the current controlling services (start/stop) as well as for controlling the current
state. state.
""" """
@ -1304,7 +1303,7 @@ class NetBsdService(Service):
if os.path.isfile(rcfile): if os.path.isfile(rcfile):
self.rcconf_file = rcfile self.rcconf_file = rcfile
self.rcconf_key = "%s" % string.replace(self.name, "-", "_") self.rcconf_key = "%s" % self.name.replace("-", "_")
return self.service_enable_rcconf() return self.service_enable_rcconf()