From 4f8045806cce589858f88a6dbec928f1ae6e176b Mon Sep 17 00:00:00 2001 From: Nickolai Korshunov Date: Sat, 18 Feb 2023 22:27:15 +0300 Subject: set default encoding of written files to UTF-8, added ability to pass custom file enconding --- CHANGES.txt | 5 +++++ SCons/Tool/textfile.py | 7 ++++++- SCons/Tool/textfile.xml | 11 ++++++++++- test/textfile/textfile.py | 3 ++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 5a3651ad8..27be9f83d 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 7fdc8b7be..c960e67cf 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 f2e8bb89e..a40fef295 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. @@ -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 .txt, 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: @@ -259,4 +260,12 @@ The suffix used for &b-link-Textfile; file names; + + + +File encoding. "utf-8" by default. + + + + diff --git a/test/textfile/textfile.py b/test/textfile/textfile.py index a2d005cfb..f614dfc94 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 -- cgit v1.2.1