Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute 'replace' #50012

Closed
hda mannequin opened this issue Apr 15, 2009 · 11 comments
Closed

AttributeError: 'NoneType' object has no attribute 'replace' #50012

hda mannequin opened this issue Apr 15, 2009 · 11 comments
Labels
topic-XML type-bug An unexpected behavior, bug, or error

Comments

@hda
Copy link
Mannequin

hda mannequin commented Apr 15, 2009

BPO 5762
Nosy @birkenfeld, @karlcow
Files
  • minidomcrash.py: Simple test script that causes AttributeError exception.
  • test-minidom-xmlns.patch: Patch Unit Test Case for python/Lib/test/test_minidom.py to test empty xmlns=""
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-10-15.17:59:04.653>
    created_at = <Date 2009-04-15.12:34:33.266>
    labels = ['expert-XML', 'type-bug']
    title = "AttributeError: 'NoneType' object has no attribute 'replace'"
    updated_at = <Date 2010-10-15.17:59:04.651>
    user = 'https://bugs.python.org/hda'

    bugs.python.org fields:

    activity = <Date 2010-10-15.17:59:04.651>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-10-15.17:59:04.653>
    closer = 'georg.brandl'
    components = ['XML']
    creation = <Date 2009-04-15.12:34:33.266>
    creator = 'hda'
    dependencies = []
    files = ['18975', '19239']
    hgrepos = []
    issue_num = 5762
    keywords = ['patch']
    message_count = 11.0
    messages = ['85990', '85991', '86013', '86014', '86221', '87352', '99546', '111545', '117195', '118732', '118813']
    nosy_count = 8.0
    nosy_names = ['georg.brandl', 'nneonneo', 'karlcow', 'hda', 'sechi_francesco', 'Steve.Romanow', 'BreamoreBoy', 'bernie9998']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5762'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @hda
    Copy link
    Mannequin Author

    hda mannequin commented Apr 15, 2009

    [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("&", "&").replace("<", "<")
    [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("&", "&").replace("<", "<")
    data = data.replace("\"", """).replace(">", ">")
    writer.write(data)

    @hda hda mannequin added type-crash A hard crash of the interpreter, possibly with a core dump topic-XML labels Apr 15, 2009
    @nneonneo
    Copy link
    Mannequin

    nneonneo mannequin commented Apr 15, 2009

    Have you tried this with xml.dom.minidom?

    @hda
    Copy link
    Mannequin Author

    hda mannequin commented Apr 16, 2009

    Yes I am using xml.dom.minidom

    doc.toprettyxml(indent="\t").encode('utf-8')

    @hda
    Copy link
    Mannequin Author

    hda mannequin commented Apr 16, 2009

    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'

    @hda
    Copy link
    Mannequin Author

    hda mannequin commented Apr 21, 2009

    I have solved this error,
    My tag has None value for one attribute.
    But python should check for None value in _write_data function.

    @sechifrancesco
    Copy link
    Mannequin

    sechifrancesco mannequin commented May 6, 2009

    I'm not able to reproduce the issue. Why don't you submit the unit test
    that generates the problem? Thanks

    @SteveRomanow
    Copy link
    Mannequin

    SteveRomanow mannequin commented Feb 18, 2010

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 25, 2010

    This issue will not move unless somebody provides a unit test for it.

    @BreamoreBoy BreamoreBoy mannequin added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Jul 25, 2010
    @bernie9998
    Copy link
    Mannequin

    bernie9998 mannequin commented Sep 23, 2010

    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.

    @karlcow
    Copy link
    Mannequin

    karlcow mannequin commented Oct 14, 2010

    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)

    @birkenfeld
    Copy link
    Member

    Thanks for the test case, committed the fix and the new test in r85546.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-XML type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant