Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abf8a1d

Browse files
committed
rewrite_descriptor(): Fix bug that dropped the first TEXT node from
<description> elements. Add another sanity check to make sure a special case only becomes involved for <opcodedesc> and <opcodedescni> elements. fixup_paras_helper(): Clean up control flow. Everywhere that checks node.nodeType==ELEMENT and node.tagName now just check node.nodeName (nodeName is guaranteed for all node types and will be special values that don't conflict with GIs for non-ELEMENT nodes). Simplifies a number of tests and improves readability in a lot of places.
1 parent f2e9e29 commit abf8a1d

1 file changed

Lines changed: 36 additions & 45 deletions

File tree

Doc/tools/sgmlconv/docfixer.py

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def get_childNodes(doc):
7474

7575
def get_first_element(doc, gi):
7676
for n in doc.childNodes:
77-
if n.nodeType == ELEMENT and n.tagName == gi:
77+
if n.nodeName == gi:
7878
return n
7979

8080
def extract_first_element(doc, gi):
@@ -86,7 +86,7 @@ def extract_first_element(doc, gi):
8686

8787
def find_all_elements(doc, gi):
8888
nodes = []
89-
if doc.nodeType == ELEMENT and doc.tagName == gi:
89+
if doc.nodeName == gi:
9090
nodes.append(doc)
9191
for child in doc.childNodes:
9292
if child.nodeType == ELEMENT:
@@ -99,16 +99,15 @@ def find_all_elements(doc, gi):
9999
def find_all_child_elements(doc, gi):
100100
nodes = []
101101
for child in doc.childNodes:
102-
if child.nodeType == ELEMENT:
103-
if child.tagName == gi:
104-
nodes.append(child)
102+
if child.nodeName == gi:
103+
nodes.append(child)
105104
return nodes
106105

107106
def find_all_elements_from_set(doc, gi_set):
108107
return __find_all_elements_from_set(doc, gi_set, [])
109108

110109
def __find_all_elements_from_set(doc, gi_set, nodes):
111-
if doc.nodeType == ELEMENT and doc.tagName in gi_set:
110+
if doc.nodeName in gi_set:
112111
nodes.append(doc)
113112
for child in doc.childNodes:
114113
if child.nodeType == ELEMENT:
@@ -156,7 +155,7 @@ def cleanup_root_text(doc):
156155
skip = 0
157156
if n.nodeType == TEXT and not prevskip:
158157
discards.append(n)
159-
elif n.nodeType == ELEMENT and n.tagName == "COMMENT":
158+
elif n.nodeName == "COMMENT":
160159
skip = 1
161160
for node in discards:
162161
doc.removeChild(node)
@@ -191,14 +190,16 @@ def rewrite_descriptor(doc, descriptor):
191190
# Do these things:
192191
# 1. Add an "index='no'" attribute to the element if the tagName
193192
# ends in 'ni', removing the 'ni' from the name.
194-
# 2. Create a <signature> from the name attribute and <args>.
193+
# 2. Create a <signature> from the name attribute
194+
# 2a.Create an <args> if it appears to be available.
195195
# 3. Create additional <signature>s from <*line{,ni}> elements,
196196
# if found.
197197
# 4. If a <versionadded> is found, move it to an attribute on the
198198
# descriptor.
199199
# 5. Move remaining child nodes to a <description> element.
200200
# 6. Put it back together.
201201
#
202+
# 1.
202203
descname = descriptor.tagName
203204
index = 1
204205
if descname[-2:] == "ni":
@@ -217,7 +218,11 @@ def rewrite_descriptor(doc, descriptor):
217218
signature.appendChild(name)
218219
name.appendChild(doc.createTextNode(descriptor.getAttribute("name")))
219220
descriptor.removeAttribute("name")
221+
# 2a.
220222
if descriptor.attributes.has_key("var"):
223+
if descname != "opcodedesc":
224+
raise RuntimeError, \
225+
"got 'var' attribute on descriptor other than opcodedesc"
221226
variable = descriptor.getAttribute("var")
222227
if variable:
223228
args = doc.createElement("args")
@@ -227,10 +232,12 @@ def rewrite_descriptor(doc, descriptor):
227232
descriptor.removeAttribute("var")
228233
newchildren = [signature]
229234
children = descriptor.childNodes
230-
pos = skip_leading_nodes(children, 0)
235+
pos = skip_leading_nodes(children)
231236
if pos < len(children):
232237
child = children[pos]
233-
if child.nodeType == ELEMENT and child.tagName == "args":
238+
if child.nodeName == "args":
239+
## bwrite("found <args> in descriptor, moving to <signature>\n")
240+
## ewrite(descriptor.toxml() + "\n---\n")
234241
# create an <args> in <signature>:
235242
args = doc.createElement("args")
236243
argchildren = []
@@ -242,10 +249,9 @@ def rewrite_descriptor(doc, descriptor):
242249
signature.appendChild(args)
243250
signature.appendChild(doc.createTextNode("\n "))
244251
# 3, 4.
245-
pos = skip_leading_nodes(children, pos + 1)
252+
pos = skip_leading_nodes(children, pos)
246253
while pos < len(children) \
247-
and children[pos].nodeType == ELEMENT \
248-
and children[pos].tagName in (linename, "versionadded"):
254+
and children[pos].nodeName in (linename, "versionadded"):
249255
if children[pos].tagName == linename:
250256
# this is really a supplemental signature, create <signature>
251257
sig = methodline_to_signature(doc, children[pos])
@@ -351,8 +357,7 @@ def fixup_trailing_whitespace(doc, wsmap):
351357
while queue:
352358
node = queue[0]
353359
del queue[0]
354-
if node.nodeType == ELEMENT \
355-
and wsmap.has_key(node.tagName):
360+
if wsmap.has_key(node.nodeName):
356361
ws = wsmap[node.tagName]
357362
children = node.childNodes
358363
children.reverse()
@@ -467,8 +472,7 @@ def create_module_info(doc, section):
467472
if title:
468473
children = title.childNodes
469474
if len(children) >= 2 \
470-
and children[0].nodeType == ELEMENT \
471-
and children[0].tagName == "module" \
475+
and children[0].nodeName == "module" \
472476
and children[0].childNodes[0].data == name:
473477
# this is it; morph the <title> into <short-synopsis>
474478
first_data = children[1]
@@ -512,8 +516,7 @@ def create_module_info(doc, section):
512516
children = section.childNodes
513517
for i in range(len(children)):
514518
node = children[i]
515-
if node.nodeType == ELEMENT \
516-
and node.tagName == "moduleinfo":
519+
if node.nodeName == "moduleinfo":
517520
nextnode = children[i+1]
518521
if nextnode.nodeType == TEXT:
519522
data = nextnode.data
@@ -595,7 +598,7 @@ def fixup_row(doc, row):
595598
def move_elements_by_name(doc, source, dest, name, sep=None):
596599
nodes = []
597600
for child in source.childNodes:
598-
if child.nodeType == ELEMENT and child.tagName == name:
601+
if child.nodeName == name:
599602
nodes.append(child)
600603
for node in nodes:
601604
source.removeChild(node)
@@ -635,9 +638,7 @@ def move_elements_by_name(doc, source, dest, name, sep=None):
635638

636639
def fixup_paras(doc, fragment):
637640
for child in fragment.childNodes:
638-
if child.nodeType == ELEMENT \
639-
and child.tagName in RECURSE_INTO_PARA_CONTAINERS:
640-
#
641+
if child.nodeName in RECURSE_INTO_PARA_CONTAINERS:
641642
fixup_paras_helper(doc, child)
642643
descriptions = find_all_elements(fragment, "description")
643644
for description in descriptions:
@@ -647,26 +648,17 @@ def fixup_paras(doc, fragment):
647648
def fixup_paras_helper(doc, container, depth=0):
648649
# document is already normalized
649650
children = container.childNodes
650-
start = 0
651+
start = skip_leading_nodes(children)
651652
while len(children) > start:
652-
start = skip_leading_nodes(children, start)
653-
if start >= len(children):
654-
break
655-
#
656-
# Either paragraph material or something to recurse into:
657-
#
658-
if (children[start].nodeType == ELEMENT) \
659-
and (children[start].tagName in RECURSE_INTO_PARA_CONTAINERS):
653+
if children[start].nodeName in RECURSE_INTO_PARA_CONTAINERS:
654+
# Something to recurse into:
660655
fixup_paras_helper(doc, children[start])
661-
start = skip_leading_nodes(children, start + 1)
662-
continue
663-
#
664-
# paragraph material:
665-
#
666-
build_para(doc, container, start, len(children))
667-
if DEBUG_PARA_FIXER and depth == 10:
668-
sys.exit(1)
669-
start = start + 1
656+
else:
657+
# Paragraph material:
658+
build_para(doc, container, start, len(children))
659+
if DEBUG_PARA_FIXER and depth == 10:
660+
sys.exit(1)
661+
start = skip_leading_nodes(children, start + 1)
670662

671663

672664
def build_para(doc, parent, start, i):
@@ -731,7 +723,7 @@ def build_para(doc, parent, start, i):
731723
return start + 1
732724

733725

734-
def skip_leading_nodes(children, start):
726+
def skip_leading_nodes(children, start=0):
735727
"""Return index into children of a node at which paragraph building should
736728
begin or a recursive call to fixup_paras_helper() should be made (for
737729
subsections, etc.).
@@ -785,8 +777,7 @@ def fixup_signatures(doc, fragment):
785777

786778
def fixup_args(doc, arglist):
787779
for child in arglist.childNodes:
788-
if child.nodeType == ELEMENT \
789-
and child.tagName == "optional":
780+
if child.nodeName == "optional":
790781
# found it; fix and return
791782
arglist.insertBefore(doc.createTextNode("["), child)
792783
optkids = child.childNodes
@@ -809,7 +800,7 @@ def fixup_sectionauthors(doc, fragment):
809800
sectauth.removeAttribute("name")
810801
after = section.childNodes[2]
811802
title = section.childNodes[1]
812-
if title.nodeType == ELEMENT and title.tagName != "title":
803+
if title.nodeName != "title":
813804
after = section.childNodes[0]
814805
section.insertBefore(doc.createTextNode("\n "), after)
815806
section.insertBefore(sectauth, after)

0 commit comments

Comments
 (0)