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
92d54d5e
Commit
92d54d5e
authored
Jan 04, 2007
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
temporarily set or unset environment variables.
parent
5399910e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
1 deletion
+55
-1
Doc/lib/libtest.tex
Doc/lib/libtest.tex
+18
-0
Lib/test/test_support.py
Lib/test/test_support.py
+33
-1
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Doc/lib/libtest.tex
View file @
92d54d5e
...
...
@@ -281,4 +281,22 @@ Execute the \class{unittest.TestSuite} instance \var{suite}.
The optional argument
\var
{
testclass
}
accepts one of the test classes in the
suite so as to print out more detailed information on where the testing suite
originated from.
The
\module
{
test.test
_
support
}
module defines the following classes:
\begin{classdesc}
{
EnvironmentVarGuard
}{}
Class used to temporarily set or unset environment variables. Instances can be
used as a context manager.
\versionadded
{
2.6
}
\end{classdesc}
\begin{methoddesc}
{
set
}{
envvar, value
}
Temporarily set the environment variable
\code
{
envvar
}
to the value of
\code
{
value
}
.
\end{methoddesc}
\begin{methoddesc}
{
unset
}{
envvar
}
Temporarily unset the environment variable
\code
{
envvar
}
.
\end{methoddesc}
\end{funcdesc}
Lib/test/test_support.py
View file @
92d54d5e
...
...
@@ -279,7 +279,39 @@ def guard_warnings_filter():
yield
finally
:
warnings
.
filters
=
original_filters
class
EnvironmentVarGuard
(
object
):
"""Class to help protect the environment variable properly. Can be used as
a context manager."""
def
__init__
(
self
):
from
os
import
environ
self
.
_environ
=
environ
self
.
_unset
=
set
()
self
.
_reset
=
dict
()
def
set
(
self
,
envvar
,
value
):
if
envvar
not
in
self
.
_environ
:
self
.
_unset
.
add
(
envvar
)
else
:
self
.
_reset
[
envvar
]
=
self
.
_environ
[
envvar
]
self
.
_environ
[
envvar
]
=
value
def
unset
(
self
,
envvar
):
if
envvar
in
self
.
_environ
:
self
.
_reset
[
envvar
]
=
self
.
_environ
[
envvar
]
del
self
.
_environ
[
envvar
]
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
*
ignore_exc
):
for
envvar
,
value
in
self
.
_reset
.
iteritems
():
self
.
_environ
[
envvar
]
=
value
for
unset
in
self
.
_unset
:
del
self
.
_environ
[
unset
]
#=======================================================================
# Decorator for running a function in a different locale, correctly resetting
...
...
Misc/NEWS
View file @
92d54d5e
...
...
@@ -323,6 +323,10 @@ Extension Modules
Tests
-----
-
Added
test
.
test_support
.
EnvironmentVarGuard
.
It
's a class that provides a
context manager so that one can temporarily set or unset environment
variables.
- Added guard_warnings_filter to test.test_support. It returns a context
manager that protects the '
warnings
' module'
s
filter
from
being
mutated
once
the
context
has
been
exited
.
...
...
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