diff options
-rw-r--r-- | CHANGES.txt | 5 | ||||
-rw-r--r-- | SCons/Tool/textfile.py | 7 | ||||
-rw-r--r-- | SCons/Tool/textfile.xml | 11 | ||||
-rw-r--r-- | test/textfile/textfile.py | 3 |
4 files changed, 23 insertions, 3 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 5a3651a..27be9f8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -134,6 +134,11 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER or not if it is already in the $LIBS construction var in the configure context. (issue #2768). + From Nickolai Korshunov + - Added explicit file encoding in env.Textfile. All written files by default + will have utf-8 encoding. User can specify encoding manually with + $FILE_ENCODING env var. + RELEASE 4.4.0 - Sat, 30 Jul 2022 14:08:29 -0700 diff --git a/SCons/Tool/textfile.py b/SCons/Tool/textfile.py index 7fdc8b7..c960e67 100644 --- a/SCons/Tool/textfile.py +++ b/SCons/Tool/textfile.py @@ -117,9 +117,14 @@ def _action(target, source, env): value = str(value) subs.append((k, value)) + if 'FILE_ENCODING' not in env: + file_encoding = 'utf-8' + else: + file_encoding = env['FILE_ENCODING'] + # write the file try: - target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newline='') + target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newline='', encoding=file_encoding) except (OSError, IOError) as e: raise SCons.Errors.UserError("Can't write target file %s [%s]" % (target[0],e)) diff --git a/SCons/Tool/textfile.xml b/SCons/Tool/textfile.xml index f2e8bb8..a40fef2 100644 --- a/SCons/Tool/textfile.xml +++ b/SCons/Tool/textfile.xml @@ -56,7 +56,7 @@ Nested lists of source strings are flattened. Source strings need not literally be Python strings: they can be Nodes or Python objects that convert cleanly -to &f-link-Value; nodes +to &f-link-Value; nodes. </para> <para> @@ -64,6 +64,7 @@ The prefix and suffix specified by the &cv-link-TEXTFILEPREFIX; and &cv-link-TEXTFILESUFFIX; &consvars; (by default an empty string and <filename>.txt</filename>, respectively) are automatically added to the target if they are not already present. +By default file encoding is "utf-8" and can be changed by &cv-link-FILE_ENCODING; Examples: </para> @@ -259,4 +260,12 @@ The suffix used for &b-link-Textfile; file names; </summary> </cvar> +<cvar name="FILE_ENCODING"> +<summary> +<para> +File encoding. "utf-8" by default. +</para> +</summary> +</cvar> + </sconsdoc> diff --git a/test/textfile/textfile.py b/test/textfile/textfile.py index a2d005c..f614dfc 100644 --- a/test/textfile/textfile.py +++ b/test/textfile/textfile.py @@ -44,7 +44,8 @@ linesep = '\n' textparts = ['lalala', '42', 'Goethe', 'Schiller', - 'tanteratei'] + 'tanteratei', + '×'] # <-- this is unicode /xd7 symbol foo1Text = linesep.join(textparts) foo2Text = '|*'.join(textparts) foo1aText = foo1Text + linesep |