classification
Title: AttributeError: 'NoneType' object has no attribute 'replace'
Type: behavior Stage: test needed
Components: XML Versions: Python 3.2, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, Steve.Romanow, bernie9998, georg.brandl, hda, karlcow, nneonneo, sechi_francesco
Priority: normal Keywords: patch

Created on 2009-04-15 12:34 by hda, last changed 2010-10-15 17:59 by georg.brandl. This issue is now closed.

Files
File name Uploaded Description Edit
minidomcrash.py bernie9998, 2010-09-23 14:54 Simple test script that causes AttributeError exception.
test-minidom-xmlns.patch karlcow, 2010-10-14 23:28 Patch Unit Test Case for python/Lib/test/test_minidom.py to test empty xmlns=""
Messages (11)
msg85990 - (view) Author: Husen daudi (hda) Date: 2009-04-15 12:34
[2009-04-15 17:53:10,198] ERROR:web-services:[19]:    
_write_data(writer, attrs[a_name].value)
[2009-04-15 17:53:10,198] ERROR:web-services:[20]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
305, in _write_data
[2009-04-15 17:53:10,199] ERROR:web-services:[21]:     data =
data.replace("&", "&amp;").replace("<", "&lt;")
[2009-04-15 17:53:10,199] ERROR:web-services:[22]: AttributeError:
'NoneType' object has no attribute 'replace'

_write_data dunction should be something like this
def _write_data(writer, data):
    "Writes datachars to writer."
    if data:
        data = data.replace("&", "&amp;").replace("<", "&lt;")
        data = data.replace("\"", "&quot;").replace(">", "&gt;")
        writer.write(data)
msg85991 - (view) Author: Robert Xiao (nneonneo) * Date: 2009-04-15 14:27
Have you tried this with xml.dom.minidom?
msg86013 - (view) Author: Husen daudi (hda) Date: 2009-04-16 04:59
Yes I am using xml.dom.minidom

doc.toprettyxml(indent="\t").encode('utf-8')
msg86014 - (view) Author: Husen daudi (hda) Date: 2009-04-16 06:36
Here is the full traceback...
[2009-04-16 12:00:03,487] ERROR:web-services:[01]: Exception in call:
Traceback (most recent call last):
[2009-04-16 12:00:03,487] ERROR:web-services:[02]:   File
"/home/hda/tiny/bzrtrunk/latest_released/server/bin/wizard/__init__.py",
line 74, in execute_cr
[2009-04-16 12:00:03,487] ERROR:web-services:[03]:     action_res =
action(self, cr, uid, data, context)
[2009-04-16 12:00:03,488] ERROR:web-services:[04]:   File
"/home/hda/tiny/bzrtrunk/latest_released/addons/base_module_record/wizard/base_module_save.py",
line 113, in _create_module
[2009-04-16 12:00:03,488] ERROR:web-services:[05]:     res_xml =
mod.generate_xml(cr, uid)
[2009-04-16 12:00:03,488] ERROR:web-services:[06]:   File
"/home/hda/tiny/bzrtrunk/latest_released/addons/base_module_record/base_module_record.py",
line 328, in generate_xml
[2009-04-16 12:00:03,488] ERROR:web-services:[07]:     #
vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
[2009-04-16 12:00:03,488] ERROR:web-services:[08]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
47, in toxml
[2009-04-16 12:00:03,488] ERROR:web-services:[09]:     return
self.toprettyxml("", "", encoding)
[2009-04-16 12:00:03,489] ERROR:web-services:[10]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
59, in toprettyxml
[2009-04-16 12:00:03,489] ERROR:web-services:[11]:    
self.writexml(writer, "", indent, newl, encoding)
[2009-04-16 12:00:03,489] ERROR:web-services:[12]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
1746, in writexml
[2009-04-16 12:00:03,489] ERROR:web-services:[13]:    
node.writexml(writer, indent, addindent, newl)
[2009-04-16 12:00:03,489] ERROR:web-services:[14]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
821, in writexml
[2009-04-16 12:00:03,490] ERROR:web-services:[15]:    
node.writexml(writer,indent+addindent,addindent,newl)
[2009-04-16 12:00:03,490] ERROR:web-services:[16]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
821, in writexml
[2009-04-16 12:00:03,490] ERROR:web-services:[17]:    
node.writexml(writer,indent+addindent,addindent,newl)
[2009-04-16 12:00:03,490] ERROR:web-services:[18]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
816, in writexml
[2009-04-16 12:00:03,491] ERROR:web-services:[19]:    
_write_data(writer, attrs[a_name].value)
[2009-04-16 12:00:03,491] ERROR:web-services:[20]:   File
"/usr/lib/python2.5/site-packages/oldxml/_xmlplus/dom/minidom.py", line
304, in _write_data
[2009-04-16 12:00:03,491] ERROR:web-services:[21]:     data =
data.replace("&", "&amp;").replace("<", "&lt;")
[2009-04-16 12:00:03,491] ERROR:web-services:[22]: AttributeError:
'NoneType' object has no attribute 'replace'
msg86221 - (view) Author: Husen daudi (hda) Date: 2009-04-21 05:23
I have solved this error,
My tag has None value for one attribute.
But python should check for None value in _write_data function.
msg87352 - (view) Author: Francesco Sechi (sechi_francesco) Date: 2009-05-06 22:14
I'm not able to reproduce the issue. Why don't you submit the unit test
that generates the problem? Thanks
msg99546 - (view) Author: Steve Romanow (Steve.Romanow) Date: 2010-02-18 23:13
Please see this post.

http://bytes.com/topic/python/answers/25245-minidom-questions
msg111545 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-25 13:14
This issue will not move unless somebody provides a unit test for it.
msg117195 - (view) Author: Brian Bernstein (bernie9998) Date: 2010-09-23 14:54
Experiencing this issue too.  It occurs when an xml element contains a blank xmlns attribute, e.g.:

{{{
<element xmlns=""/>
}}}

When toxml() is called on a minidom document with this attribute, the exception occurs.

I am including a simple script that causes this crash.
msg118732 - (view) Author: karl (karlcow) * Date: 2010-10-14 23:28
This following markup creates the mistake as described earlier in the comments

<element xmlns="">
<b class="foo" alt=""/>
</element>

This markup doesn't

<element xmlns="bla">
<b class="foo" alt=""/>
</element>

It returns

<?xml version="1.0" ?><element xmlns="bla">
<b alt="" class="foo"/>
</element>

When using this markup

<element bar="">
<b class="foo" alt=""/>
</element>

It outputs the right markup,

<?xml version="1.0" ?><element bar="">
<b alt="" class="foo"/>
</element>

So the mistake occurs really when xmlns="".  I have checked and the following markup is a conformant markup according to the XML specification so xmlns="" or bar="" are conformant on the root element.

<element xmlns="">
<b class="foo" alt=""/>
</element>

XML Namespaces are defined in another specification. http://www.w3.org/TR/REC-xml-names/. In the section of Namespaces default http://www.w3.org/TR/REC-xml-names/#defaulting, The specification is clear.

"The attribute value in a default namespace declaration MAY be empty. This has the same effect, within the scope of the declaration, of there being no default namespace."

the proposed "if data:" earlier in the comment solves the issue. I have attached a unit testcase as required by Mark Lawrence (BreamoreBoy)
msg118813 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-15 17:59
Thanks for the test case, committed the fix and the new test in r85546.
History
Date User Action Args
2014-06-24 06:38:30ned.deilylinkissue12066 superseder
2010-10-15 17:59:04georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg118813

resolution: fixed
2010-10-14 23:28:30karlcowsetfiles: + test-minidom-xmlns.patch

nosy: + karlcow
messages: + msg118732

keywords: + patch
2010-09-23 14:57:21bernie9998setversions: + Python 2.6
2010-09-23 14:54:40bernie9998setfiles: + minidomcrash.py
nosy: + bernie9998
messages: + msg117195

2010-07-25 13:14:22BreamoreBoysetversions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.5
nosy: + BreamoreBoy

messages: + msg111545

type: crash -> behavior
stage: test needed
2010-02-18 23:13:33Steve.Romanowsetnosy: + Steve.Romanow
messages: + msg99546
2009-05-06 22:14:01sechi_francescosetmessages: + msg87352
2009-05-06 22:12:26sechi_francescosetnosy: + sechi_francesco
2009-04-21 05:23:06hdasetmessages: + msg86221
2009-04-16 06:37:29hdasetmessages: + msg86014
2009-04-16 04:59:47hdasetmessages: + msg86013
2009-04-15 14:27:32nneonneosetnosy: + nneonneo
messages: + msg85991
2009-04-15 12:34:33hdacreate