alihalabyah / modwsgi

Automatically exported from code.google.com/p/modwsgi
0 stars 0 forks source link

modwsgi for python3 is not work well in Japanese. #246

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
・Debian squeeze
・Apache 2.2.16-6+squeeze3
・libapache2-mod-wsgi-py3 3.3-2

I set up apache2 and modwsgi in this environment.

And I try to display English "Hello world", and it works well.

Next, I tried to display Japanese like "こんにちは". But it not work. I 
can't display Japanese in my site.

The /var/log/apache2/error.log tell me next 2 line.

mod_wsgi (pid=8375): Exception occurred processing WSGI script 
'/home/www/test1/kesitene.py'.

sequence of byte string values expected, value containing non 'latin-1' 
characters found

I want to user unicode. But error messege show 'latin-1'.

I searched to solve this problem. And found this 
site(http://www.ohneta.net/wiki/index.php?python3%2Fmod_wsgi%E3%81%A7%E6%97%A5%E
6%9C%AC%E8%AA%9E%E8%A1%A8%E7%A4%BA), which tell me about this error.

which tell me what the code of mod_wsgi.c have next problem.

---------------
static PyObject *Adapter_write(AdapterObject *self, PyObject *args)
{
    PyObject *item = NULL;
    const char *data = NULL;
    int length = 0;

    if (!self->r) {
        PyErr_SetString(PyExc_RuntimeError, "request object has expired");
        return NULL;
    }

    if (!PyArg_ParseTuple(args, "O:write", &item))
        return NULL;

#if PY_MAJOR_VERSION >= 3
    if (PyUnicode_Check(item)) {
        PyObject *latin_item;
        latin_item = PyUnicode_AsLatin1String(item);
        if (!latin_item) {
            PyErr_Format(PyExc_TypeError, "byte string value expected, "             ←ここらへん
                         "value containing non 'latin-1' characters found");
            Py_DECREF(item);
            return NULL;
        }

        Py_DECREF(item);
        item = latin_item;
    }
#endif

    if (!PyString_Check(item)) {
        PyErr_Format(PyExc_TypeError, "byte string value expected, value "
                     "of type %.200s found", item->ob_type->tp_name);
        Py_DECREF(item);
        return NULL;
    }

    data = PyString_AsString(item);
    length = PyString_Size(item);

    if (!Adapter_output(self, data, length, 1))
        return NULL;

    Py_INCREF(Py_None);
    return Py_None;
}
-------------
when the python version3, if the display string is unicode, it convert to 
Latin-1. But of course Japanese can't convert to Latin-1. So it occured error.

This site solve this problem to change the code

"PyUnicode_AsLatin1String(item);"

to

"PyUnicode_AsUTF8String(item)";

But I'm not familiar with Linux programing. So I don't know how to apply this 
fix to my environment.

So if you can, please fix this code.

If you did it, maybe I can install in Debian by using aptitude.

Or Is there a reason you can not fix it?

Original issue reported on code.google.com by ink...@gmail.com on 19 Sep 2011 at 5:18

GoogleCodeExporter commented 9 years ago
Your WSGI application is wrong. Your WSGI application must return a byte string 
and not a Unicode string. You need to convert your Unicode string to using 
appropriate encoding, usually UTF-8, when returning them.

  output.encode('utf-8')

it is not the servers responsibility to do that.

Original comment by Graham.Dumpleton@gmail.com on 19 Sep 2011 at 5:53

GoogleCodeExporter commented 9 years ago
Thanks!! I'm python programing beginner. So I didn't now it. Your answer help 
me and I can display Japanese!! Thank you very much!!

Original comment by ink...@gmail.com on 19 Sep 2011 at 9:50