Проблемы рендеринга вида с помощью tidy (Django + Apache mod_wsgi) - PullRequest
1 голос
/ 26 октября 2010

При работе под встроенным сервером разработки все работает нормально.

Однако при запуске под mod_wsgi в Apache следующий код (метод замены для django.shortcuts.render_to_response) вызывает внутреннюю ошибку сервера:

# Tidy and render
def render_tidy_response(*args, **kwargs):    
    httpresponse_kwargs = {
                           'mimetype': kwargs.pop('mimetype', None)
                           }
    raw_template = loader.render_to_string(*args, **kwargs)    
    request = kwargs.get('request', None)
    output_options = dict(
                          output_xhtml=1,
                          tidy_mark=0, 
                          numeric_entities=1,
                          output_encoding='utf8',
                          drop_empty_paras=1,
                          escape_cdata=1,
                          clean=1,
                          wrap=0,
                          merge_divs=0,
                          output_bom=0
                          )
    if (request != None):
        request.session['current_user'] = request.user        
    tidy_output = str(tidy.parseString(raw_template, **output_options))        
    return HttpResponse(tidy_output, **httpresponse_kwargs)

Похоже, проблема связана свызов библиотеки tidy, поэтому следующее изменение работает нормально:

.. as above
#tidy_output = str(tidy.parseString(raw_template, **output_options))        
return HttpResponse(raw_template, **httpresponse_kwargs)

Кто-нибудь имеет представление, в чем может быть проблема?

[ДОБАВЛЕНО] Вот записи журнала, относящиеся кошибочный запрос с использованием журнала отладки, но я не могу выяснить, что является причиной ошибки сегмента:

Основной журнал (фрагмент)

[Tue Oct 26 21:35:14 2010] [info] Shared memory session cache initialised
[Tue Oct 26 21:35:14 2010] [info] Init: Initializing (virtual) servers for SSL
[Tue Oct 26 21:35:14 2010] [info] mod_ssl/2.2.14 compiled against Server: Apache/2.2.14, Library: OpenSSL/0.9.8k
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3110): Initializing Python.
[Tue Oct 26 21:35:14 2010] [debug] mod_wsgi.c(8104): mod_wsgi (pid=3110): Socket for 'test' is '/var/run/apache2/wsgi.3110.3.1.sock'.
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3948): Starting process 'test' with uid=33, gid=33 and threads=25.
[Tue Oct 26 21:35:14 2010] [notice] Apache/2.2.14 (Ubuntu) mod_ssl/2.2.14 OpenSSL/0.9.8k mod_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations
[Tue Oct 26 21:35:14 2010] [info] Server built: Sep 28 2010 12:54:21
[Tue Oct 26 21:35:14 2010] [debug] worker.c(1757): AcceptMutex: sysvsem (default: sysvsem)
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3949): Attach interpreter ''.
[Tue Oct 26 21:35:14 2010] [info] mod_wsgi (pid=3950): Attach interpreter ''.
[Tue Oct 26 21:35:50 2010] [notice] child pid 3948 exit signal Segmentation fault (11)
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=3948): Process 'test' has died, restarting.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Starting process 'test' with uid=33, gid=33 and threads=25.

Журнал Vhost (фрагмент)

[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Attach interpreter ''.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8702): mod_wsgi (pid=4041): Starting 25 threads in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 1 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 2 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 3 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 4 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 5 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 6 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 7 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 8 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 9 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 10 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 11 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 12 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 13 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 14 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 15 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 16 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 17 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 18 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 19 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 20 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 21 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 22 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 23 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 24 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8708): mod_wsgi (pid=4041): Starting thread 25 in daemon process 'test'.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Enable deadlock thread in process 'test'.
[Tue Oct 26 21:35:50 2010] [info] mod_wsgi (pid=4041): Enable monitor thread in process 'test'.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8542): mod_wsgi (pid=4041): Deadlock timeout is 300.
[Tue Oct 26 21:35:50 2010] [debug] mod_wsgi.c(8545): mod_wsgi (pid=4041): Inactivity timeout is 0.

1 Ответ

1 голос
/ 28 октября 2010

Казалось бы, ответ - это нефиксированная ошибка в µTidylib: Ошибка # 14691 - привязки не являются безопасными на 64 бита

Патч в комментариях решил проблему:

----------
diff -ruN uTidylib-0.2-orig/tidy/lib.py uTidylib-0.2/tidy/lib.py
--- uTidylib-0.2-orig/tidy/lib.py 2004-02-24 08:12:24.000000000 +0000
+++ uTidylib-0.2/tidy/lib.py 2008-10-16 14:45:56.000000000 +0100
@@ -130,6 +130,8 @@

sinkfactory=SinkFactory()

+_tidy.Create.restype = ctypes.POINTER(ctypes.c_void_p)
+
class _Document(object):
def __init__(self):
self.cdoc = _tidy.Create()
----------

Вместе с модификацией для render_tidy_response:

---     (revision 409)
+++     (working copy)
@@ -15,6 +15,7 @@
                           output_xhtml=1,
                           tidy_mark=0, 
                           numeric_entities=1,
+             input_encoding='utf8',
                           output_encoding='utf8',
                           drop_empty_paras=1,
                           escape_cdata=1,
@@ -25,8 +26,8 @@
                           )
     if (request != None):
         request.session['current_user'] = request.user    
-    #tidy_output = str(tidy.parseString(raw_template, **output_options))    
-    return HttpResponse(raw_template, **httpresponse_kwargs)
+    tidy_output = str(tidy.parseString(raw_template.encode('utf-8'), **output_options))    
+    return HttpResponse(tidy_output, **httpresponse_kwargs)
...