Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e318a883
Kaydet (Commit)
e318a883
authored
Mar 11, 2013
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #15806: Add contextlib.ignored().
üst
c0417357
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
1 deletion
+58
-1
contextlib.rst
Doc/library/contextlib.rst
+20
-0
contextlib.py
Lib/contextlib.py
+13
-1
test_contextlib.py
Lib/test/test_contextlib.py
+22
-0
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/contextlib.rst
Dosyayı görüntüle @
e318a883
...
...
@@ -94,6 +94,26 @@ Functions and classes provided:
without needing to explicitly close ``page``. Even if an error occurs,
``page.close()`` will be called when the :keyword:`with` block is exited.
.. function:: ignored(*exceptions)
Return a context manager that ignores the specified expections if they
occur in the body of a with-statement.
For example::
from contextlib import ignored
with ignored(OSError):
os.remove('somefile.tmp')
This code is equivalent to::
try:
os.remove('somefile.tmp')
except OSError:
pass
.. versionadded:: 3.4
.. class:: ContextDecorator()
...
...
Lib/contextlib.py
Dosyayı görüntüle @
e318a883
...
...
@@ -4,7 +4,7 @@ import sys
from
collections
import
deque
from
functools
import
wraps
__all__
=
[
"contextmanager"
,
"closing"
,
"ContextDecorator"
,
"ExitStack"
]
__all__
=
[
"contextmanager"
,
"closing"
,
"ContextDecorator"
,
"ExitStack"
,
"ignored"
]
class
ContextDecorator
(
object
):
...
...
@@ -140,6 +140,18 @@ class closing(object):
def
__exit__
(
self
,
*
exc_info
):
self
.
thing
.
close
()
@contextmanager
def
ignored
(
*
exceptions
):
"""Context manager to ignore specifed exceptions
with ignored(OSError):
os.remove(somefile)
"""
try
:
yield
except
exceptions
:
pass
# Inspired by discussions on http://bugs.python.org/issue13585
class
ExitStack
(
object
):
...
...
Lib/test/test_contextlib.py
Dosyayı görüntüle @
e318a883
...
...
@@ -594,6 +594,28 @@ class TestExitStack(unittest.TestCase):
stack
.
push
(
cm
)
self
.
assertIs
(
stack
.
_exit_callbacks
[
-
1
],
cm
)
class
TestIgnored
(
unittest
.
TestCase
):
def
test_no_exception
(
self
):
with
ignored
(
ValueError
):
self
.
assertEqual
(
pow
(
2
,
5
),
32
)
def
test_exact_exception
(
self
):
with
ignored
(
TypeError
):
len
(
5
)
def
test_multiple_exception_args
(
self
):
with
ignored
(
ZeroDivisionError
,
TypeError
):
len
(
5
)
def
test_exception_hierarchy
(
self
):
with
ignored
(
LookupError
):
'Hello'
[
50
]
# This is needed to make the test actually run under regrtest.py!
def
test_main
():
...
...
Misc/NEWS
Dosyayı görüntüle @
e318a883
...
...
@@ -280,6 +280,9 @@ Library
_
Issue
#
17385
:
Fix
quadratic
behavior
in
threading
.
Condition
.
The
FIFO
queue
now
uses
a
deque
instead
of
a
list
.
-
Issue
#
15806
:
Add
contextlib
.
ignored
().
This
creates
a
context
manager
to
ignore
specified
exceptions
,
replacing
the
"except Exc: pass"
idiom
.
-
Issue
#
14645
:
The
email
generator
classes
now
produce
output
using
the
specified
linesep
throughout
.
Previously
if
the
prolog
,
epilog
,
or
body
were
stored
with
a
different
linesep
,
that
linesep
was
used
.
This
...
...
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