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
2e01b758
Kaydet (Commit)
2e01b758
authored
Haz 01, 2018
tarafından
Scott Sanderson
Kaydeden (comit)
Brett Cannon
Haz 01, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-29235: Make cProfile.Profile a context manager (GH-6808)
üst
252f6abe
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
0 deletions
+52
-0
profile.rst
Doc/library/profile.rst
+10
-0
cProfile.py
Lib/cProfile.py
+7
-0
test_cprofile.py
Lib/test/test_cprofile.py
+27
-0
2018-05-14-15-01-55.bpo-29235.47Fzwt.rst
...S.d/next/Library/2018-05-14-15-01-55.bpo-29235.47Fzwt.rst
+8
-0
No files found.
Doc/library/profile.rst
Dosyayı görüntüle @
2e01b758
...
...
@@ -262,6 +262,16 @@ functions:
ps.print_stats()
print(s.getvalue())
The :class:`Profile` class can also be used as a context manager (see
:ref:`typecontextmanager`)::
import cProfile
with cProfile.Profile() as pr:
# ... do something ...
pr.print_stats()
.. method:: enable()
Start collecting profiling data.
...
...
Lib/cProfile.py
Dosyayı görüntüle @
2e01b758
...
...
@@ -110,6 +110,13 @@ class Profile(_lsprof.Profiler):
finally
:
self
.
disable
()
def
__enter__
(
self
):
self
.
enable
()
return
self
def
__exit__
(
self
,
*
exc_info
):
self
.
disable
()
# ____________________________________________________________
def
label
(
code
):
...
...
Lib/test/test_cprofile.py
Dosyayı görüntüle @
2e01b758
...
...
@@ -49,6 +49,33 @@ class CProfileTest(ProfileTest):
# Test successful run
assert_python_ok
(
'-m'
,
'cProfile'
,
'-m'
,
'timeit'
,
'-n'
,
'1'
)
def
test_profile_enable_disable
(
self
):
prof
=
self
.
profilerclass
()
# Make sure we clean ourselves up if the test fails for some reason.
self
.
addCleanup
(
prof
.
disable
)
prof
.
enable
()
self
.
assertIs
(
sys
.
getprofile
(),
prof
)
prof
.
disable
()
self
.
assertIs
(
sys
.
getprofile
(),
None
)
def
test_profile_as_context_manager
(
self
):
prof
=
self
.
profilerclass
()
# Make sure we clean ourselves up if the test fails for some reason.
self
.
addCleanup
(
prof
.
disable
)
with
prof
as
__enter__return_value
:
# profile.__enter__ should return itself.
self
.
assertIs
(
prof
,
__enter__return_value
)
# profile should be set as the global profiler inside the
# with-block
self
.
assertIs
(
sys
.
getprofile
(),
prof
)
# profile shouldn't be set once we leave the with-block.
self
.
assertIs
(
sys
.
getprofile
(),
None
)
def
test_main
():
run_unittest
(
CProfileTest
)
...
...
Misc/NEWS.d/next/Library/2018-05-14-15-01-55.bpo-29235.47Fzwt.rst
0 → 100644
Dosyayı görüntüle @
2e01b758
The :class:`cProfile.Profile` class can now be used as a context manager.
You can profile a block of code by running::
import cProfile
with cProfile.Profile() as profiler:
# ... code to be profiled ...
Patch by Scott Sanderson.
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