module object has no attribute ElementTree

Bug #254278 reported by ViktorNagy
20
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Bazaar
Fix Released
High
Martin Packman
2.1
Fix Released
Undecided
Unassigned
2.2
Fix Released
Undecided
Unassigned

Bug Description

I am running bzr through Apache+modwsgi locally (for development)

the HTTP authentication is handled by Apache, and it works fine

$ bzr checkout bzr+http://nagyv@127.0.0.1/repos/bae7a163-3cf8-4328-8eab-82c511b1ba56/trunk/ bzr
HTTP nagyv@127.0.0.1, Realm: 'Coosci - Repositories' password:
bzr: ERROR: exceptions.AssertionError: unexpected response code ('error', "'module' object has no attribute 'ElementTree'")

Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/bzrlib/commands.py", line 834, in run_bzr_catch_errors
    return run_bzr(argv)
  File "/usr/lib/python2.5/site-packages/bzrlib/commands.py", line 790, in run_bzr
    ret = run(*run_argv)
  File "/usr/lib/python2.5/site-packages/bzrlib/commands.py", line 492, in run_argv_aliases
    return self.run(**all_cmd_args)
  File "/usr/lib/python2.5/site-packages/bzrlib/builtins.py", line 991, in run
    branch_location)
  File "/usr/lib/python2.5/site-packages/bzrlib/bzrdir.py", line 774, in open_tree_or_branch
    return bzrdir._get_tree_branch()
  File "/usr/lib/python2.5/site-packages/bzrlib/remote.py", line 137, in _get_tree_branch
    return None, self.open_branch()
  File "/usr/lib/python2.5/site-packages/bzrlib/remote.py", line 144, in open_branch
    return RemoteBranch(self, self.find_repository())
  File "/usr/lib/python2.5/site-packages/bzrlib/bzrdir.py", line 519, in find_repository
    return self.open_repository()
  File "/usr/lib/python2.5/site-packages/bzrlib/remote.py", line 161, in open_repository
    'unexpected response code %s' % (response,)
AssertionError: unexpected response code ('error', "'module' object has no attribute 'ElementTree'")

bzr 1.3.1 on python 2.5.2.final.0 (linux2)
arguments: ['/usr/bin/bzr', 'checkout', 'bzr+http://nagyv@127.0.0.1/repos/bae7a163-3cf8-4328-8eab-82c511b1ba56/trunk/', 'bzr']
encoding: 'UTF-8', fsenc: 'UTF-8', lang: 'hu_HU.UTF-8'
plugins:
  gtk /usr/lib/python2.5/site-packages/bzrlib/plugins/gtk [0.93.0]
  launchpad /usr/lib/python2.5/site-packages/bzrlib/plugins/launchpad [unknown]
*** Bazaar has encountered an internal error.
    Please report a bug at https://bugs.launchpad.net/bzr/+filebug
    including this traceback, and a description of what you
    were doing when the error occurred.

Related branches

Revision history for this message
Robert Collins (lifeless) wrote : Re: [Bug 254278] [NEW] module object has no attribute ElementTree

On Sat, 2008-08-02 at 19:17 +0000, ViktorNagy wrote:
> Public bug reported:
>
> I am running bzr through Apache+modwsgi locally (for development)
>
> the HTTP authentication is handled by Apache, and it works fine
>
> $ bzr checkout bzr+http://nagyv@127.0.0.1/repos/bae7a163-3cf8-4328-8eab-82c511b1ba56/trunk/ bzr
> HTTP nagyv@127.0.0.1, Realm: 'Coosci - Repositories' password:
> bzr: ERROR: exceptions.AssertionError: unexpected response code ('error', "'module' object has no attribute 'ElementTree'")

> AssertionError: unexpected response code ('error', "'module' object has no attribute 'ElementTree'")
>
> bzr 1.3.1 on python 2.5.2.final.0 (linux2)

Last time I saw that it was a buggy python build where some of the core
libraries had been omitted for some reason.

Does it have the normal PYTHONPATH in the apache environment?

-Rob
--
GPG key available at: <http://www.robertcollins.net/keys.txt>.

Vincent Ladeuil (vila)
Changed in bzr:
importance: Undecided → Low
status: New → Incomplete
Revision history for this message
ViktorNagy (viktor-nagy) wrote :

Sorry for not replying before, but I was using ssh instead, so there was no error.

Now I am running a small script served through django and mod_wsgi, and I get the same error. If I run the script straight then everything is fine. I've printed sys.path just before calling the bzrlib functions, and sys.path is the same in both cases. The script and the two diffs are provided below.

The script is:
import bzrlib.bzrdir
import os, sys
from bzrlib.builtins import cmd_init_repository
from tempfile import mkstemp

def createRepo(url, mask=0750):
    '''
    Run "bzr init-repo --no-working-tree" at the given location
    '''
    open('/home/coosci/dev-repos/log/pythonpath-3.py', 'w').write(str(sys.path))
    repository_format = bzrlib.bzrdir.format_registry.make_bzrdir('pack-0.92')
    bzr_init_repo = cmd_init_repository()
    bzr_init_repo.outf = open(mkstemp()[1], 'w')

    os.mkdir(url, mask)
    bzr_init_repo.run(url, format=repository_format, no_trees=True)

and the two sys.paths are
$ diff pythongpath-2.py pythonpath-3.py
1c1
< ['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/PIL', '/var/lib/python-support/python2.5', '/home/coosci/dev-repos/repos/webapp/main', '/home/coosci/dev-repos/repos/webapp/main']
\ No newline at end of file
---
> ['/home/coosci/dev-repos/tmp', '/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/PIL', '/var/lib/python-support/python2.5', '/home/coosci/dev-repos/repos/webapp/main/']
\ No newline at end of file

any ideas what might be the problem?
I am running bzr 1.8

Revision history for this message
Michael Gratton (mjog) wrote :

I'm getting this at the moment, I'm running two instances of bzr in the same apache/mod_wsgi container - the smart server for anon access and the track bzr plugin.

If I start apache and access the smart server first, that works fine but the trac instance fails, but if I access trac first then the smart server fails, both with the "AttributeError: 'module' object has no attribute 'ElementTree'" error.

So it sounds like the import-fu at the top of bzrlib/xml_serializer.py is failing when run twice? Is that even possible?

Attaching the stack from the logs.

Revision history for this message
ViktorNagy (viktor-nagy) wrote : Re: [Bug 254278] Re: module object has no attribute ElementTree

To work around this problem, I set up my system to use separate VirtualHosts
for the different bzr instances, and point every VirtualHost to a different
virtualenv. This way they run separate bzr instances, and now everything
works fine for me.

V
--
Viktor Nagy - http://viktornagy.com
PhD student
Toulouse School of Economics

2009/3/19 Mike Gratton <email address hidden>

> I'm getting this at the moment, I'm running two instances of bzr in the
> same apache/mod_wsgi container - the smart server for anon access and
> the track bzr plugin.
>
> If I start apache and access the smart server first, that works fine but
> the trac instance fails, but if I access trac first then the smart
> server fails, both with the "AttributeError: 'module' object has no
> attribute 'ElementTree'" error.
>
> So it sounds like the import-fu at the top of bzrlib/xml_serializer.py
> is failing when run twice? Is that even possible?
>
> Attaching the stack from the logs.
>
> ** Attachment added: "wsgi backtrace"
> http://launchpadlibrarian.net/24105815/wsgi-smartserver.bt
>
> --
> module object has no attribute ElementTree
> https://bugs.launchpad.net/bugs/254278
> You received this bug notification because you are a direct subscriber
> of the bug.
>

Revision history for this message
Michael Gratton (mjog) wrote :

The problem is indeed in bzrlib/xml_serializer.py, line 33 (in bzr 1.11 and 1.13, at least).

Changing that import statement from:

> import xml.etree as elementtree

to:

> import xml.etree.cElementTree as elementtree

Fixes the error. I'll spin a patch for this if needed, but can't right now.

I think the except clause immediately below that is also buggy, too, but again, can't really test that right now and I'm using Python 2.5, which is probably why isn't a problem for me.

Martin Pool (mbp)
Changed in bzr:
assignee: nobody → mjg
status: Incomplete → In Progress
Revision history for this message
Fran Boon (flavour) wrote :

I got bitten by this error when I upgraded my Trac's Bzr to v2.0.0
Mike's fix to xml_serializer.py resolves it :)

F

Revision history for this message
David Hostetler (dahoste) wrote :

I encountered this when setting up a wsgi-served repository.

I 'resolved' it by installing celementtree. Since the use of celementtree vs. elementree is treated by bzr as an either/or situation, having celementtree available dodged whatever namespace-related import problems seem to be occurring for elementtree. I think this amounts to the same thing as what Mike Gratton suggested above (where he just assumes celementtree is available).

My understanding is that celementtree is a performance-related option, but not a requirement. So this is still technically just a work-around and not a solution.

Revision history for this message
Martin Pool (mbp) wrote :

David, what version of bzr and of Python are you using?

--
Martin <http://launchpad.net/~mbp/>

Revision history for this message
Toshio Kuratomi (toshio) wrote :

The workaround posted in comment #5 looks like it will break if elementtree.ElementTree._escape_attrib() is ever called as cElementTree dosen't provide the private methods that are needed there.

I did notice that the following line was changed sometime between bzr-1.13 and bzr-2.1b1::

    except ImportError:
        from cElementTree import (ElementTree, SubElement, Element,
                                  XMLTreeBuilder, fromstring, tostring)
- import elementtree
+ import elementtree.ElementTree

The new import makes the code work on python2.4 when cElementTree is installed for me. (For Fedora EPEL packages which are stuck on bzr-1.13 because the API has changed. I'll backport this change to our package.)

I'm not sure which versions of python + bzr are still affected, the combination seems important. For instance, it seems like the following code might be broken if the particular version of cElementTree isn't importing xml.etree.ElementTree internally:

        from xml.etree.cElementTree import (ElementTree, SubElement, Element,
            XMLTreeBuilder, fromstring, tostring)
        import xml.etree as elementtree

Revision history for this message
Toshio Kuratomi (toshio) wrote :

Above should be bzr-1.3.1

Revision history for this message
Martin Pool (mbp) wrote :

Thanks, Toshio.

I'm not clear what change, if any, is still desired in bzr's trunk?

--
Martin <http://launchpad.net/~mbp/>

Revision history for this message
Torsten Bronger (bronger) wrote :

bzr's trunk may be sane now, but isn't it desireable to get a fix into Lucid's bzr package?

Revision history for this message
Martin Pool (mbp) wrote :

Lucid has 2.1 so from what was said above, this is fixed in Lucid. Is it still broken there? Can you paste a traceback?

Vincent Ladeuil (vila)
Changed in bzr:
assignee: Mike Gratton (mjog) → nobody
status: In Progress → Confirmed
Revision history for this message
Glen Mailer (glenjamin) wrote :

Reproduced this with a mod_wsgi + http smart server setup using a single daemon group for multiple vhosts.
Ubuntu Lucid Lynx, standard apt versions of bazaar (2.1.1) and mod_wsgi.

As mentioned in comment #9, it's the monkeypatched performance improving bits of xml_serializer which cause this to break - removing them fixes this issue.

I applied the following changes directly to xml_serializer.
https://code.edge.launchpad.net/~gz/bzr/remove_monkey_patched_elementtree_escaping_614522/+merge/34028

Vincent Ladeuil (vila)
Changed in bzr:
importance: Low → High
Revision history for this message
Martin Packman (gz) wrote :

Toshio is completely right about this bug. As it's still possible to hit, I'll put up a branch for 2.1 (which 2.2 will also want). Though removing the monkey patching as per 2.3 works, a one line import change will also do and is less scary.

Changed in bzr:
assignee: nobody → Martin [gz] (gz)
milestone: none → 2.1.3
Martin Packman (gz)
Changed in bzr:
status: Confirmed → Fix Released
Andrew Bennetts (spiv)
Changed in bzr:
milestone: 2.1.3 → 2.3b1
Revision history for this message
Maxim Yaskevich (mayask) wrote :

Could anybody explain to my why does this monkeypatching trick cause the error?

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.