Skip to content

Commit

Permalink
[FIX] xml-syntax-error: Fix crash if a xml has syntax errors (#343)
Browse files Browse the repository at this point in the history
Fix #331
  • Loading branch information
moylop260 committed Sep 8, 2021
1 parent bf5f2b7 commit 7d727a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
21 changes: 9 additions & 12 deletions pylint_odoo/checkers/modules_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import astroid
import polib
from collections import defaultdict
from lxml import etree
from pylint.checkers import utils
from six import string_types

from .. import misc, settings

Expand Down Expand Up @@ -641,10 +641,12 @@ def _check_xml_syntax_error(self):
"""
self.msg_args = []
for xml_file in self.filter_files_ext('xml', relpath=True):
result = self.parse_xml(os.path.join(self.module_path, xml_file))
if isinstance(result, string_types):
try:
self.parse_xml(os.path.join(self.module_path, xml_file),
raise_if_error=True)
except etree.XMLSyntaxError as xmlsyntax_error:
self.msg_args.append((
xml_file, result.strip('\n').replace('\n', '|')))
xml_file, str(xmlsyntax_error).strip('\n').replace('\n', '|')))
if self.msg_args:
return False
return True
Expand Down Expand Up @@ -740,8 +742,7 @@ def _check_character_not_valid_in_resource_link(self):
for xml_file in self.filter_files_ext('xml'):
doc = self.parse_xml(os.path.join(self.module_path, xml_file))
for name, attr in (('link', 'href'), ('script', 'src')):
nodes = (doc.xpath('.//%s[@%s]' % (name, attr))
if not isinstance(doc, string_types) else [])
nodes = doc.xpath('.//%s[@%s]' % (name, attr))
for node in nodes:
resource = node.get(attr, '')
ext = os.path.splitext(os.path.basename(resource))[1]
Expand Down Expand Up @@ -911,8 +912,7 @@ def _check_deprecated_data_xml_node(self):
self.msg_args = []
for xml_file in xml_files:
doc = self.parse_xml(os.path.join(self.module_path, xml_file))
odoo_nodes = doc.xpath("/odoo") \
if not isinstance(doc, string_types) else []
odoo_nodes = doc.xpath("/odoo")
children, data_node = ((odoo_nodes[0].getchildren(),
odoo_nodes[0].findall('data'))
if odoo_nodes else ([], []))
Expand All @@ -932,8 +932,7 @@ def _check_deprecated_openerp_xml_node(self):
self.msg_args = []
for xml_file in xml_files:
doc = self.parse_xml(os.path.join(self.module_path, xml_file))
openerp_nodes = doc.xpath("/openerp") \
if not isinstance(doc, string_types) else []
openerp_nodes = doc.xpath("/openerp")
if openerp_nodes:
lineno = openerp_nodes[0].sourceline
self.msg_args.append(("%s:%s" % (xml_file, lineno)))
Expand Down Expand Up @@ -1123,8 +1122,6 @@ def _check_xml_deprecated_qweb_directive(self):
self.msg_args = []
for xml_file in self.filter_files_ext('xml', relpath=False):
doc = self.parse_xml(xml_file)
if isinstance(doc, string_types):
continue
for node in doc.xpath(xpath):
# Find which directive was used exactly.
directive = next(
Expand Down
9 changes: 5 additions & 4 deletions pylint_odoo/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def get_duplicated_items(self, items):
unique_items.add(item)
return list(duplicated_items)

def parse_xml(self, xml_file):
def parse_xml(self, xml_file, raise_if_error=False):
"""Get xml parsed.
:param xml_file: Path of file xml
:return: Doc parsed (lxml.etree object)
Expand All @@ -504,7 +504,9 @@ def parse_xml(self, xml_file):
with open(xml_file, "rb") as f_obj:
doc = etree.parse(f_obj)
except etree.XMLSyntaxError as xmlsyntax_error_exception:
return str(xmlsyntax_error_exception)
if raise_if_error:
raise xmlsyntax_error_exception
return etree.Element("__empty__")
return doc

def get_xml_records(self, xml_file, model=None, more=None):
Expand All @@ -530,8 +532,7 @@ def get_xml_records(self, xml_file, model=None, more=None):
more_filter = more
doc = self.parse_xml(xml_file)
return doc.xpath("/openerp//record" + model_filter + more_filter) + \
doc.xpath("/odoo//record" + model_filter + more_filter) \
if not isinstance(doc, string_types) else []
doc.xpath("/odoo//record" + model_filter + more_filter)

def get_field_csv(self, csv_file, field='id'):
"""Get xml ids from csv file
Expand Down

0 comments on commit 7d727a9

Please sign in to comment.