-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unit 1.29 unexpected returns getfilesystemencoding is ascii #817
Comments
Hi @darkbarker We are currently investigating an issue that was introduced with 1.29 as we added support for Python 3.11. See the thread on our mailing list for more details. https://mailman.nginx.org/pipermail/unit/2022-December/GMDLF7ZJEPRNZWXVEQ3G6NKBKLGKL2PC.html We will share a fix for this as soon as possible. |
Please try reverting 491d0f7 $ git revert -n 491d0f70 Ignore the warnings... |
Using this simple test {
"listeners": {
"[::1]:8080": {
"pass": "applications/gh817"
}
},
"applications": {
"gh817": {
"type": "python",
"path": "/home/andrew/src/python/",
"module": "gh817"
}
}
} $ cat ~/src/python/gh817.py import sys
def application(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
return sys.getfilesystemencoding().encode('utf-8') + b'\n' Produces $ curl http://localhost:8080/
utf-8 That was with current unit master on Fedora 37 (Python 3.11) Are you able to confirm if that works or fails for you? Or if you have a minimal reproducer? |
The above works for me on Debian 11 There must be something else tickling this bug. |
Hi @darkbarker does that happen with unit.nginx.org-provided debian 11 packages? |
yes, installed from packages.nginx.org, with apt (as the documentation says).
|
@ac000 build unit from source is not so quick to do for me 😅 a little later (if not fixed yet) |
@darkbarker Are you able to test the simple program above? |
So far the only way I've been able to reproduce this is to run unit under a locale of say, 'C' $ export LC_ALL=C
$ locale
LANG=en_GB.UTF-8
LANGUAGE=en_GB:en
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C
$ /tmp/unit/sbin/unitd --no-daemon
2022/12/23 21:04:50 [warn] 10858#10858 Unit is running unprivileged, then it cannot use arbitrary user and group.
2022/12/23 21:04:50 [info] 10858#10858 unit 1.30.0 started $ curl http://localhost:8080/
ascii Put it back LC_ALL=en_GB.UTF-8
$ /tmp/unit/sbin/unitd --no-daemon
2022/12/23 21:11:12 [warn] 10896#10896 Unit is running unprivileged, then it cannot use arbitrary user and group.
2022/12/23 21:11:12 [info] 10896#10896 unit 1.30.0 started $ curl http://localhost:8080/
utf-8 |
on a simple application (similar as above), the error could not be repeated. on django it repeats again. I need some time.... |
->
but (+ "home")
->
/home/slb/app_test_encoding/env is a pure vanilla virtualenv
|
|
So it seems virtualenv is what tickles this particular bug. Don't know much about it but this looks like a crucial piece of information. Thanks!. |
inside the virtualenv, the encodings are correct, too
|
So for Python 3.8+ when you have the When this happens There's a simple patch you could try to verify this... diff --git a/src/python/nxt_python.c b/src/python/nxt_python.c
index bdb04579..0f9b373e 100644
--- a/src/python/nxt_python.c
+++ b/src/python/nxt_python.c
@@ -94,6 +94,8 @@ nxt_python3_init_config(nxt_int_t pep405)
}
}
+ PyConfig_SetString(&config, &config.filesystem_encoding, L"utf-8");
+
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
goto pyinit_exception; When we don't go this route things seem to work out as before. Sill some work to be done... |
So while the above changes the output of |
This patch seems to do the right thing and will enable UTF-8 depending on the setting of the LC_CTYPE environment variable (this is the default behaviour when using the non-isolated python config). So if LC_CTYPE is either: C, POSIX or some specific UTF-8 locale then you will get UTF-8 support. diff --git a/src/python/nxt_python.c b/src/python/nxt_python.c
index bdb04579..ce497d33 100644
--- a/src/python/nxt_python.c
+++ b/src/python/nxt_python.c
@@ -75,8 +75,17 @@ static nxt_python_proto_t nxt_py_proto;
static nxt_int_t
nxt_python3_init_config(nxt_int_t pep405)
{
- PyStatus status;
- PyConfig config;
+ PyConfig config;
+ PyStatus status;
+ PyPreConfig preconfig;
+
+ PyPreConfig_InitIsolatedConfig(&preconfig);
+ preconfig.utf8_mode = -1;
+
+ status = Py_PreInitialize(&preconfig);
+ if (PyStatus_Exception(status)) {
+ return NXT_ERROR;
+ }
PyConfig_InitIsolatedConfig(&config);
|
Just compiled unit with this patch. No longer see any ascii related issues logged at all |
There was a couple of reports of Python applications failing due to the following type of error File "/opt/netbox/netbox/netbox/configuration.py", line 25, in _import print(f"\U0001f9ec loaded config '{path}'") UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f9ec' in position 0: ordinal not in range(128) due to the use of Unicode text in the print() statement. This only happened for python 3.8+ when using the "home" configuration option as this meant we were going through the new PyConfig configuration. When using this new configuration method with the 'isolated' specific API (for embedded Python) UTF-8 is disabled by default, PyPreConfig->utf8_mode = 0. To fix this we need to setup the Python pre config and enable utf-8 mode. However rather than enable utf-8 unconditionally we can set to it to -1 so that it will use the LC_CTYPE environment variable to determine whether to enable utf-8 mode or not. utf-8 mode will be enabled if LC_CTYPE is either: C, POSIX or some specific UTF-8 locale. This is the default utf8_mode setting when using the non-isolated PyPreConfig API. Reported-by: Tobias Genannt <tobias.genannt@kappa-velorum.net> Tested-by: Tobias Genannt <tobias.genannt@kappa-velorum.net> Link: <https://peps.python.org/pep-0587/> Link: <https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.utf8_mode> Fixes: 491d0f7 ("Python: Added support for Python 3.11.") Closes: <#817> Reviewed-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
This fix has been merged. |
There was a couple of reports of Python applications failing due to the following type of error File "/opt/netbox/netbox/netbox/configuration.py", line 25, in _import print(f"\U0001f9ec loaded config '{path}'") UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f9ec' in position 0: ordinal not in range(128) due to the use of Unicode text in the print() statement. This only happened for python 3.8+ when using the "home" configuration option as this meant we were going through the new PyConfig configuration. When using this new configuration method with the 'isolated' specific API (for embedded Python) UTF-8 is disabled by default, PyPreConfig->utf8_mode = 0. To fix this we need to setup the Python pre config and enable utf-8 mode. However rather than enable utf-8 unconditionally we can set to it to -1 so that it will use the LC_CTYPE environment variable to determine whether to enable utf-8 mode or not. utf-8 mode will be enabled if LC_CTYPE is either: C, POSIX or some specific UTF-8 locale. This is the default utf8_mode setting when using the non-isolated PyPreConfig API. Reported-by: Tobias Genannt <tobias.genannt@kappa-velorum.net> Tested-by: Tobias Genannt <tobias.genannt@kappa-velorum.net> Link: <https://peps.python.org/pep-0587/> Link: <https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.utf8_mode> Fixes: 491d0f7 ("Python: Added support for Python 3.11.") Closes: <#817> Reviewed-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
debian 11, all locale settings *.uft-8
system (as before):
but in application (django) launched on unit + unit-python3.9 sys.getfilesystemencoding() returns after update (from 1.28 to 1.29) 'ascii' (with appropriate ancient spell "UnicodeEncodeError: 'ascii' codec can't encode characters in position" etc)
maybe it"s related to this?:
(nothing more suitable)
after updates-rollback is everything works.
i read docs, issues, maybe i missed something?
The text was updated successfully, but these errors were encountered: