List for cgit developers and users
 help / color / mirror / Atom feed
* [PATCH] filter: set environment variable PYTHONIOENCODING to utf-8
@ 2017-02-23 15:48 roy
  2017-03-04 12:35 ` john
  0 siblings, 1 reply; 13+ messages in thread
From: roy @ 2017-02-23 15:48 UTC (permalink / raw)


This allows different versions of Python to be used rather than
forcing version specific encoding in each script.

Signed-off-by: Roy Marples <roy at marples.name>
---
 filter.c                       | 8 ++++++++
 filters/email-gravatar.py      | 3 ---
 filters/syntax-highlighting.py | 5 +----
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/filter.c b/filter.c
index 949c931..3c0f978 100644
--- a/filter.c
+++ b/filter.c
@@ -15,6 +15,8 @@
 #include <lauxlib.h>
 #endif
 
+static bool filter_env_set;
+
 static inline void reap_filter(struct cgit_filter *filter)
 {
 	if (filter && filter->cleanup)
@@ -44,6 +46,12 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap)
 	struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
 	int i;
 
+	if (!filter_env_set) {
+		/* Always input/output utf-8 for a Python filter. */
+		setenv("PYTHONIOENCODING", "utf-8", 1);
+		filter_env_set = true;
+	}
+
 	for (i = 0; i < filter->base.argument_count; i++)
 		filter->argv[i + 1] = va_arg(ap, char *);
 
diff --git a/filters/email-gravatar.py b/filters/email-gravatar.py
index d70440e..8b98471 100755
--- a/filters/email-gravatar.py
+++ b/filters/email-gravatar.py
@@ -30,9 +30,6 @@ if email[-1] == '>':
 
 page = sys.argv[2]
 
-sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach())
-sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
-
 md5 = hashlib.md5(email.encode()).hexdigest()
 text = sys.stdin.read().strip()
 
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py
index 5888b50..936fdb7 100755
--- a/filters/syntax-highlighting.py
+++ b/filters/syntax-highlighting.py
@@ -29,9 +29,6 @@ from pygments.lexers import guess_lexer
 from pygments.lexers import guess_lexer_for_filename
 from pygments.formatters import HtmlFormatter
 
-
-sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
-sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
 data = sys.stdin.read()
 filename = sys.argv[1]
 formatter = HtmlFormatter(style='pastie')
@@ -52,4 +49,4 @@ except TypeError:
 sys.stdout.write('<style>')
 sys.stdout.write(formatter.get_style_defs('.highlight'))
 sys.stdout.write('</style>')
-sys.stdout.write(highlight(data, lexer, formatter, outfile=None))
+highlight(data, lexer, formatter, outfile=sys.stdout)
-- 
2.11.1



^ permalink raw reply	[flat|nested] 13+ messages in thread
* [PATCH] filter: set environment variable PYTHONIOENCODING to utf-8
@ 2017-03-09  0:18 roy
  2017-03-10 15:28 ` john
  0 siblings, 1 reply; 13+ messages in thread
From: roy @ 2017-03-09  0:18 UTC (permalink / raw)


This allows different versions of Python to be used rather than
forcing version specific encoding in each script.

Signed-off-by: Roy Marples <roy at marples.name>
---
 filter.c                       | 2 ++
 filters/email-gravatar.py      | 3 ---
 filters/syntax-highlighting.py | 5 +----
 3 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/filter.c b/filter.c
index 949c931..84dd0ed 100644
--- a/filter.c
+++ b/filter.c
@@ -55,6 +55,8 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap)
 		close(filter->pipe_fh[1]);
 		chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
 			"Unable to use pipe as STDIN");
+		/* Always input/output utf-8 for a Python filter. */
+		setenv("PYTHONIOENCODING", "utf-8", 1);
 		execvp(filter->cmd, filter->argv);
 		die_errno("Unable to exec subprocess %s", filter->cmd);
 	}
diff --git a/filters/email-gravatar.py b/filters/email-gravatar.py
index d70440e..8b98471 100755
--- a/filters/email-gravatar.py
+++ b/filters/email-gravatar.py
@@ -30,9 +30,6 @@ if email[-1] == '>':
 
 page = sys.argv[2]
 
-sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach())
-sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
-
 md5 = hashlib.md5(email.encode()).hexdigest()
 text = sys.stdin.read().strip()
 
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py
index 5888b50..936fdb7 100755
--- a/filters/syntax-highlighting.py
+++ b/filters/syntax-highlighting.py
@@ -29,9 +29,6 @@ from pygments.lexers import guess_lexer
 from pygments.lexers import guess_lexer_for_filename
 from pygments.formatters import HtmlFormatter
 
-
-sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
-sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
 data = sys.stdin.read()
 filename = sys.argv[1]
 formatter = HtmlFormatter(style='pastie')
@@ -52,4 +49,4 @@ except TypeError:
 sys.stdout.write('<style>')
 sys.stdout.write(formatter.get_style_defs('.highlight'))
 sys.stdout.write('</style>')
-sys.stdout.write(highlight(data, lexer, formatter, outfile=None))
+highlight(data, lexer, formatter, outfile=sys.stdout)
-- 
2.11.1



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-08-10 14:04 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-23 15:48 [PATCH] filter: set environment variable PYTHONIOENCODING to utf-8 roy
2017-03-04 12:35 ` john
2017-03-06  9:14   ` roy
2017-03-08 19:01     ` roy
2017-03-09  0:10       ` john
2017-03-12 17:01         ` Jason
2017-03-12 17:51           ` john
2017-03-17 18:07             ` Jason
2017-03-17 20:04               ` john
2017-03-17 20:13                 ` roy
2017-08-10 14:04                   ` Jason
2017-03-09  0:18 roy
2017-03-10 15:28 ` john

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).