@@ -6226,7 +6226,15 @@ def blockreplace(
62266226 return ret
62276227
62286228
6229- def comment (name , regex , char = "#" , backup = ".bak" , ignore_missing = False ):
6229+ def comment (
6230+ name ,
6231+ regex ,
6232+ char = "#" ,
6233+ backup = ".bak" ,
6234+ ignore_missing = False ,
6235+ encoding = None ,
6236+ encoding_errors = "strict" ,
6237+ ):
62306238 """
62316239 .. versionadded:: 0.9.5
62326240 .. versionchanged:: 3005
@@ -6262,6 +6270,28 @@ def comment(name, regex, char="#", backup=".bak", ignore_missing=False):
62626270
62636271 .. versionadded:: 3005
62646272
6273+ encoding
6274+ If specified, this encoding is used to decode the file when building
6275+ the diff used for change detection. Otherwise the system locale
6276+ encoding (usually UTF-8) is used. This does not affect the file's
6277+ contents, which are modified as raw bytes by the underlying execution
6278+ module. See
6279+ https://docs.python.org/3/library/codecs.html#standard-encodings for
6280+ the list of available encodings.
6281+
6282+ .. versionadded:: 3006.28
6283+
6284+ encoding_errors
6285+ Error handling scheme used when decoding the file for the diff.
6286+ Default is ``'strict'``, matching Python's default, which raises a
6287+ ``UnicodeDecodeError`` if the file contains bytes that are not valid in
6288+ the chosen encoding. Set to ``'replace'`` / ``'ignore'`` (or supply the
6289+ correct ``encoding``) to handle such files. See
6290+ https://docs.python.org/3/library/codecs.html#error-handlers for the
6291+ list of available schemes.
6292+
6293+ .. versionadded:: 3006.28
6294+
62656295 Usage:
62666296
62676297 .. code-block:: yaml
@@ -6308,15 +6338,19 @@ def comment(name, regex, char="#", backup=".bak", ignore_missing=False):
63086338
63096339 with salt .utils .files .fopen (name , "rb" ) as fp_ :
63106340 slines = fp_ .read ()
6311- slines = slines .decode (__salt_system_encoding__ , errors = "replace" )
6341+ slines = slines .decode (
6342+ encoding or __salt_system_encoding__ , errors = encoding_errors
6343+ )
63126344 slines = slines .splitlines (True )
63136345
63146346 # Perform the edit
63156347 __salt__ ["file.comment_line" ](name , regex , char , True , backup )
63166348
63176349 with salt .utils .files .fopen (name , "rb" ) as fp_ :
63186350 nlines = fp_ .read ()
6319- nlines = nlines .decode (__salt_system_encoding__ , errors = "replace" )
6351+ nlines = nlines .decode (
6352+ encoding or __salt_system_encoding__ , errors = encoding_errors
6353+ )
63206354 nlines = nlines .splitlines (True )
63216355
63226356 # Check the result
@@ -6445,6 +6479,8 @@ def append(
64456479 defaults = None ,
64466480 context = None ,
64476481 ignore_whitespace = True ,
6482+ encoding = None ,
6483+ encoding_errors = "strict" ,
64486484):
64496485 """
64506486 Ensure that some text appears at the end of a file.
@@ -6536,6 +6572,28 @@ def append(
65366572 appending content, one space or multiple tabs are the same for salt.
65376573 Set this option to ``False`` if you want to change this behavior.
65386574
6575+ encoding
6576+ If specified, this encoding is used to decode the file when building
6577+ the diff used for change detection. Otherwise the system locale
6578+ encoding (usually UTF-8) is used. This does not affect the file's
6579+ contents, which are modified as raw bytes by the underlying execution
6580+ module. See
6581+ https://docs.python.org/3/library/codecs.html#standard-encodings for
6582+ the list of available encodings.
6583+
6584+ .. versionadded:: 3006.28
6585+
6586+ encoding_errors
6587+ Error handling scheme used when decoding the file for the diff.
6588+ Default is ``'strict'``, matching Python's default, which raises a
6589+ ``UnicodeDecodeError`` if the file contains bytes that are not valid in
6590+ the chosen encoding. Set to ``'replace'`` / ``'ignore'`` (or supply the
6591+ correct ``encoding``) to handle such files. See
6592+ https://docs.python.org/3/library/codecs.html#error-handlers for the
6593+ list of available schemes.
6594+
6595+ .. versionadded:: 3006.28
6596+
65396597 Multi-line example:
65406598
65416599 .. code-block:: yaml
@@ -6645,7 +6703,9 @@ def append(
66456703
66466704 with salt .utils .files .fopen (name , "rb" ) as fp_ :
66476705 slines = fp_ .read ()
6648- slines = slines .decode (__salt_system_encoding__ , errors = "replace" )
6706+ slines = slines .decode (
6707+ encoding or __salt_system_encoding__ , errors = encoding_errors
6708+ )
66496709 slines = slines .splitlines ()
66506710
66516711 append_lines = []
@@ -6691,7 +6751,9 @@ def append(
66916751
66926752 with salt .utils .files .fopen (name , "rb" ) as fp_ :
66936753 nlines = fp_ .read ()
6694- nlines = nlines .decode (__salt_system_encoding__ , errors = "replace" )
6754+ nlines = nlines .decode (
6755+ encoding or __salt_system_encoding__ , errors = encoding_errors
6756+ )
66956757 nlines = nlines .splitlines ()
66966758
66976759 if slines != nlines :
@@ -6718,6 +6780,8 @@ def prepend(
67186780 defaults = None ,
67196781 context = None ,
67206782 header = None ,
6783+ encoding = None ,
6784+ encoding_errors = "strict" ,
67216785):
67226786 """
67236787 Ensure that some text appears at the beginning of a file
@@ -6809,6 +6873,28 @@ def prepend(
68096873 appending content, one space or multiple tabs are the same for salt.
68106874 Set this option to ``False`` if you want to change this behavior.
68116875
6876+ encoding
6877+ If specified, this encoding is used to decode the file when building
6878+ the diff used for change detection. Otherwise the system locale
6879+ encoding (usually UTF-8) is used. This does not affect the file's
6880+ contents, which are modified as raw bytes by the underlying execution
6881+ module. See
6882+ https://docs.python.org/3/library/codecs.html#standard-encodings for
6883+ the list of available encodings.
6884+
6885+ .. versionadded:: 3006.28
6886+
6887+ encoding_errors
6888+ Error handling scheme used when decoding the file for the diff.
6889+ Default is ``'strict'``, matching Python's default, which raises a
6890+ ``UnicodeDecodeError`` if the file contains bytes that are not valid in
6891+ the chosen encoding. Set to ``'replace'`` / ``'ignore'`` (or supply the
6892+ correct ``encoding``) to handle such files. See
6893+ https://docs.python.org/3/library/codecs.html#error-handlers for the
6894+ list of available schemes.
6895+
6896+ .. versionadded:: 3006.28
6897+
68126898 Multi-line example:
68136899
68146900 .. code-block:: yaml
@@ -6930,7 +7016,9 @@ def prepend(
69307016
69317017 with salt .utils .files .fopen (name , "rb" ) as fp_ :
69327018 slines = fp_ .read ()
6933- slines = slines .decode (__salt_system_encoding__ , errors = "replace" )
7019+ slines = slines .decode (
7020+ encoding or __salt_system_encoding__ , errors = encoding_errors
7021+ )
69347022 slines = slines .splitlines (True )
69357023
69367024 count = 0
@@ -6978,7 +7066,9 @@ def prepend(
69787066 with salt .utils .files .fopen (name , "rb" ) as fp_ :
69797067 # read as many lines of target file as length of user input
69807068 contents = fp_ .read ()
6981- contents = contents .decode (__salt_system_encoding__ , errors = "replace" )
7069+ contents = contents .decode (
7070+ encoding or __salt_system_encoding__ , errors = encoding_errors
7071+ )
69827072 contents = contents .splitlines (True )
69837073 target_head = contents [0 : len (preface )]
69847074 target_lines = []
@@ -6997,7 +7087,9 @@ def prepend(
69977087
69987088 with salt .utils .files .fopen (name , "rb" ) as fp_ :
69997089 nlines = fp_ .read ()
7000- nlines = nlines .decode (__salt_system_encoding__ , errors = "replace" )
7090+ nlines = nlines .decode (
7091+ encoding or __salt_system_encoding__ , errors = encoding_errors
7092+ )
70017093 nlines = nlines .splitlines (True )
70027094
70037095 if slines != nlines :
0 commit comments