Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
17173cfe
Commit
17173cfe
authored
Jun 09, 2010
by
Kristján Valur Jónsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http://bugs.python.org/issue8832
Issue minidom.unlink with a context manager
parent
3dcb5acd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
0 deletions
+22
-0
Doc/library/xml.dom.minidom.rst
Doc/library/xml.dom.minidom.rst
+7
-0
Lib/test/test_minidom.py
Lib/test/test_minidom.py
+7
-0
Lib/xml/dom/minidom.py
Lib/xml/dom/minidom.py
+8
-0
No files found.
Doc/library/xml.dom.minidom.rst
View file @
17173cfe
...
...
@@ -114,6 +114,13 @@ module documentation. This section lists the differences between the API and
to
be
called
on
the
:
class
:`
Document
`
object
,
but
may
be
called
on
child
nodes
to
discard
children
of
that
node
.
You
can
avoid
calling
this
method
explicitly
by
using
the
:
keyword
:`
with
`
statement
.
The
following
code
will
automatically
unlink
*
dom
*
when
the
:
keyword
:`
with
`
block
is
exited
::
with
xml
.
dom
.
minidom
.
parse
(
datasource
)
as
dom
:
...
#
Work
with
dom
.
..
method
::
Node
.
writexml
(
writer
,
indent
=
""
,
addindent
=
""
,
newl
=
""
,
encoding
=
""
)
...
...
Lib/test/test_minidom.py
View file @
17173cfe
...
...
@@ -228,7 +228,14 @@ class MinidomTest(unittest.TestCase):
def
testUnlink
(
self
):
dom
=
parse
(
tstfile
)
self
.
assertTrue
(
dom
.
childNodes
)
dom
.
unlink
()
self
.
assertFalse
(
dom
.
childNodes
)
def
testContext
(
self
):
with
parse
(
tstfile
)
as
dom
:
self
.
assertTrue
(
dom
.
childNodes
)
self
.
assertFalse
(
dom
.
childNodes
)
def
testElement
(
self
):
dom
=
Document
()
...
...
Lib/xml/dom/minidom.py
View file @
17173cfe
...
...
@@ -268,6 +268,14 @@ class Node(xml.dom.Node):
self
.
previousSibling
=
None
self
.
nextSibling
=
None
# A Node is its own context manager, to ensure that an unlink() call occurs.
# This is similar to how a file object works.
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
et
,
ev
,
tb
):
self
.
unlink
()
defproperty
(
Node
,
"firstChild"
,
doc
=
"First child node, or None."
)
defproperty
(
Node
,
"lastChild"
,
doc
=
"Last child node, or None."
)
defproperty
(
Node
,
"localName"
,
doc
=
"Namespace-local name of this node."
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment