]> granicus.if.org Git - cgit/commitdiff
filter: set environment variable PYTHONIOENCODING to utf-8
authorRoy Marples <roy@marples.name>
Thu, 9 Mar 2017 00:18:10 +0000 (00:18 +0000)
committerLukas Fleischer <lfleischer@lfos.de>
Thu, 10 Aug 2017 13:15:55 +0000 (15:15 +0200)
This allows different versions of Python to be used rather than
forcing version specific encoding in each script.

Signed-off-by: Roy Marples <roy@marples.name>
Signed-off-by: John Keeping <john@keeping.me.uk>
filter.c
filters/email-gravatar.py
filters/syntax-highlighting.py

index 949c931af1f67cb48df4554d6b1d7ca87d897ce2..84dd0ed9a428653da108008ca2c8d5262ce3b202 100644 (file)
--- 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);
        }
index d70440ea543cc30fd6de6512d03431a6f2dc97e3..8b98471c03f1e820f48152422fa75262546ff569 100755 (executable)
@@ -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()
 
index 5888b507863f020dddf7f5f7606c6bfcb304869e..936fdb7a391323fb4141fce7327faddda1c7bcc8 100755 (executable)
@@ -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)