lxml + mod_python: cannot unmarshal code objects in restricted execution mode

Discussion in 'Python' started by Dmitri Fedoruk, Sep 13, 2007.

  1. Hello everyone,

    I'm developing a mod_python application that is based on XML\XSLT
    transforming.

    I used 4Suite libraries for that, but as the speed was unacceptable
    for me, I switched to lxml. Everything became much easier and 10 times
    faster, but I've encountered the subject problem.

    In brief - all my data and xslt are stored and transferred in UTF-8.
    With 4Suite everything was fine all the time. With lxml it works fine
    from the console, but inside mod_python it occasionaly dies, ~ one
    time out of three. Strange - the same code with the same data works or
    dies by its own means.

    As far as I have found, there was a similar problem with PyXML and
    encodings module, but there was no clear solution.

    So, my configuration is the following:
    Python 2.5.1
    Server version: Apache/2.2.4 (FreeBSD)
    mod_python-3.3.1

    And the relevant parts of my code are these:

    def extApplyXslt(xslt, data, logger ):
    try:
    strXslt = urllib2.urlopen(xslt).read()
    # i have to read the xslt url to the python string
    except urllib2.HTTPError, e:
    .......
    except urllib2.URLError, e:
    .............
    try:
    xslt_parser = etree.XMLParser()
    xslt_parser.resolvers.add( PrefixResolver("XSLT") )

    # and now I have to use the string; a more elegant solution,
    anyone?
    f = StringIO(strXslt)
    xslt_doc = etree.parse(f, xslt_parser)

    # and here where the problem comes
    transform = etree.XSLT(xslt_doc)

    except Exception, exc:
    logger.log(logging.CRITICAL, exc.__str__() )

    try:
    result_tree = transform(data)
    return etree.tostring(result_tree, 'utf-8')
    except Exception, exc:
    print "xslt processing error!", exc.__str__()
    return ""

    It dies with the message 'cannot unmarshal code objects in restricted
    execution mode'. By profiling I detected the point where problem
    occurs:
    transform = etree.XSLT(xslt_doc)

    So, I would be grateful for any suggestions how to get rid of this.
    I'd really like to use lxml. Maybe I should initialize the xslt
    processor in somehow other way?

    Thanks in advance,
    Dmitri
    Dmitri Fedoruk, Sep 13, 2007
    #1
    1. Advertising

  2. Re: lxml + mod_python: cannot unmarshal code objects in restrictedexecution mode

    Dmitri Fedoruk wrote:
    > def extApplyXslt(xslt, data, logger ):
    > try:
    > strXslt = urllib2.urlopen(xslt).read()
    > # i have to read the xslt url to the python string
    > except urllib2.HTTPError, e:
    > .......
    > except urllib2.URLError, e:
    > .............
    > try:
    > xslt_parser = etree.XMLParser()
    > xslt_parser.resolvers.add( PrefixResolver("XSLT") )
    >
    > # and now I have to use the string; a more elegant solution,
    > anyone?


    Sure, lxml.etree can parse from file-like objects. Just hand in the result of
    urlopen().

    Apart from that, I saw that you found your way to the lxml mailing list, I'll
    respond over there.

    Stefan
    Stefan Behnel, Sep 13, 2007
    #2
    1. Advertising

  3. On Sep 13, 5:05 pm, Dmitri Fedoruk <> wrote:
    > So, my configuration is the following:
    > Python 2.5.1
    > Server version: Apache/2.2.4 (FreeBSD)
    > mod_python-3.3.1

    update:
    lxml-1.3.4
    libxslt-1.1.21
    libxml2-2.6.29
    Dmitri Fedoruk, Sep 13, 2007
    #3
  4. On Sep 13, 11:05 pm, Dmitri Fedoruk <> wrote:
    > Hello everyone,
    >
    > I'm developing a mod_python application that is based on XML\XSLT
    > transforming.
    >
    > I used 4Suite libraries for that, but as the speed was unacceptable
    > for me, I switched to lxml. Everything became much easier and 10 times
    > faster, but I've encountered the subject problem.
    >
    > In brief - all my data and xslt are stored and transferred in UTF-8.
    > With 4Suite everything was fine all the time. With lxml it works fine
    > from the console, but inside mod_python it occasionaly dies, ~ one
    > time out of three. Strange - the same code with the same data works or
    > dies by its own means.
    >
    > As far as I have found, there was a similar problem with PyXML and
    > encodings module, but there was no clear solution.
    >
    > So, my configuration is the following:
    > Python 2.5.1
    > Server version: Apache/2.2.4 (FreeBSD)
    > mod_python-3.3.1
    >
    > And the relevant parts of my code are these:
    >
    > def extApplyXslt(xslt, data, logger ):
    > try:
    > strXslt = urllib2.urlopen(xslt).read()
    > # i have to read the xslt url to the python string
    > except urllib2.HTTPError, e:
    > .......
    > except urllib2.URLError, e:
    > .............
    > try:
    > xslt_parser = etree.XMLParser()
    > xslt_parser.resolvers.add( PrefixResolver("XSLT") )
    >
    > # and now I have to use the string; a more elegant solution,
    > anyone?
    > f = StringIO(strXslt)
    > xslt_doc = etree.parse(f, xslt_parser)
    >
    > # and here where the problem comes
    > transform = etree.XSLT(xslt_doc)
    >
    > except Exception, exc:
    > logger.log(logging.CRITICAL, exc.__str__() )
    >
    > try:
    > result_tree = transform(data)
    > return etree.tostring(result_tree, 'utf-8')
    > except Exception, exc:
    > print "xslt processing error!", exc.__str__()
    > return ""
    >
    > It dies with the message 'cannot unmarshal code objects in restricted
    > execution mode'. By profiling I detected the point where problem
    > occurs:
    > transform = etree.XSLT(xslt_doc)
    >
    > So, I would be grateful for any suggestions how to get rid of this.
    > I'd really like to use lxml. Maybe I should initialize the xslt
    > processor in somehow other way?
    >
    > Thanks in advance,
    > Dmitri


    Try forcing mod_python to run your code in the first interpreter
    instance created by Python.

    PythonInterpreter main_interpreter

    If that still doesn't work then you are hitting problems in mod_python
    related to how it does GIL management or how the third party package
    is implemented and whether it is compatible with use in secondary
    Python interpreters. For details see:

    If it still doesn't work and your application is a WSGI application
    and you aren't using mod_python specific features, only choice at this
    point would be to use mod_fastcgi or mod_wsgi instead. If using
    mod_wsgi, you would still need to tell it to run your code in first
    interpreter instance created by Python.

    WSGIApplicationGroup %{GLOBAL}

    For some details on issues related to this problem, see sections
    'Python Simplified GIL State API' and 'Multiple Python Sub
    Interpreters' in mod_wsgi document:

    http://code.google.com/p/modwsgi/wiki/ApplicationIssues

    Graham
    Graham Dumpleton, Sep 14, 2007
    #4
  5. On Sep 14, 3:04 am, Graham Dumpleton <>
    wrote:
    > Try forcing mod_python to run your code in the first interpreter
    > instance created by Python.
    > PythonInterpreter main_interpreter

    Thank you very much, that solved the problem! A more detailed
    discussion can also be found in the lxml-dev mailing list (
    http://comments.gmane.org/gmane.comp.python.lxml.devel/2942 )

    Dmitri
    Dmitri Fedoruk, Sep 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Serge Corbucci
    Replies:
    0
    Views:
    481
    Serge Corbucci
    Jan 14, 2004
  2. crash.test.dummy

    unmarshal XML from String using CastorXML

    crash.test.dummy, Jan 29, 2006, in forum: Java
    Replies:
    2
    Views:
    5,056
    crash.test.dummy
    Jan 30, 2006
  3. Sean R. Lynch
    Replies:
    30
    Views:
    752
    Sean R. Lynch
    Jan 6, 2004
  4. David Pokorny

    Restricted Execution on the cheap

    David Pokorny, Nov 30, 2004, in forum: Python
    Replies:
    2
    Views:
    270
    Nick Coghlan
    Nov 30, 2004
  5. Emanuele D'Arrigo

    Restricted Execution of untrusted code

    Emanuele D'Arrigo, Oct 30, 2008, in forum: Python
    Replies:
    7
    Views:
    281
    Emanuele D'Arrigo
    Nov 19, 2008
Loading...

Share This Page