summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNickolai Korshunov <n.korshunov@corp.mail.ru>2023-02-18 22:27:15 +0300
committerNickolai Korshunov <n.korshunov@corp.mail.ru>2023-02-18 22:50:14 +0300
commit4f8045806cce589858f88a6dbec928f1ae6e176b (patch)
tree128ccf5ccfcec71a691cb63a3e381bb92b70cd30
parent440728dd1d9fee6a8e010b4d9871737686cb3afb (diff)
downloadscons-git-4f8045806cce589858f88a6dbec928f1ae6e176b.tar.gz
set default encoding of written files to UTF-8, added ability to pass
custom file enconding
-rw-r--r--CHANGES.txt5
-rw-r--r--SCons/Tool/textfile.py7
-rw-r--r--SCons/Tool/textfile.xml11
-rw-r--r--test/textfile/textfile.py3
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.
</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 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