Skip to content

Commit 33a4000

Browse files
committed
Issue python#20627: xmlrpc.client.ServerProxy is now a context manager.
Patch by Claudiu Popa.
1 parent 051f37d commit 33a4000

5 files changed

Lines changed: 42 additions & 10 deletions

File tree

Doc/library/xmlrpc.client.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ grouped under the reserved :attr:`system` attribute:
191191
no such string is available, an empty string is returned. The documentation
192192
string may contain HTML markup.
193193

194+
.. versionchanged:: 3.5
195+
196+
Instances of :class:`ServerProxy` support the :term:`context manager` protocol
197+
for closing the underlying transport.
198+
194199

195200
A working example follows. The server code::
196201

@@ -208,9 +213,9 @@ The client code for the preceding server::
208213

209214
import xmlrpc.client
210215

211-
proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
212-
print("3 is even: %s" % str(proxy.is_even(3)))
213-
print("100 is even: %s" % str(proxy.is_even(100)))
216+
with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy:
217+
print("3 is even: %s" % str(proxy.is_even(3)))
218+
print("100 is even: %s" % str(proxy.is_even(100)))
214219

215220
.. _datetime-objects:
216221

@@ -518,14 +523,14 @@ Example of Client Usage
518523
from xmlrpc.client import ServerProxy, Error
519524

520525
# server = ServerProxy("http://localhost:8000") # local server
521-
server = ServerProxy("http://betty.userland.com")
526+
with ServerProxy("http://betty.userland.com") as proxy:
522527

523-
print(server)
528+
print(proxy)
524529

525-
try:
526-
print(server.examples.getStateName(41))
527-
except Error as v:
528-
print("ERROR", v)
530+
try:
531+
print(proxy.examples.getStateName(41))
532+
except Error as v:
533+
print("ERROR", v)
529534

530535
To access an XML-RPC server through a proxy, you need to define a custom
531536
transport. The following example shows how:

Doc/whatsnew/3.5.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ New Modules
134134
Improved Modules
135135
================
136136

137-
* None yet.
137+
* :class:`xmlrpc.client.ServerProxy` is now a :term:`context manager`
138+
(contributed by Claudiu Popa in :issue:`20627`).
138139

139140

140141
Optimizations

Lib/test/test_xmlrpc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,23 @@ def test_partial_post(self):
713713
conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye')
714714
conn.close()
715715

716+
def test_context_manager(self):
717+
with xmlrpclib.ServerProxy(URL) as server:
718+
server.add(2, 3)
719+
self.assertNotEqual(server('transport')._connection,
720+
(None, None))
721+
self.assertEqual(server('transport')._connection,
722+
(None, None))
723+
724+
def test_context_manager_method_error(self):
725+
try:
726+
with xmlrpclib.ServerProxy(URL) as server:
727+
server.add(2, "a")
728+
except xmlrpclib.Fault:
729+
pass
730+
self.assertEqual(server('transport')._connection,
731+
(None, None))
732+
716733

717734
class MultiPathServerTestCase(BaseServerTestCase):
718735
threadFunc = staticmethod(http_multi_server)
@@ -898,6 +915,7 @@ def test_transport(self):
898915
p = xmlrpclib.ServerProxy(self.url, transport=t)
899916
self.assertEqual(p('transport'), t)
900917

918+
901919
# This is a contrived way to make a failure occur on the server side
902920
# in order to test the _send_traceback_header flag on the server
903921
class FailingMessageClass(http.client.HTTPMessage):

Lib/xmlrpc/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,12 @@ def __call__(self, attr):
14491449
return self.__transport
14501450
raise AttributeError("Attribute %r not found" % (attr,))
14511451

1452+
def __enter__(self):
1453+
return self
1454+
1455+
def __exit__(self, *args):
1456+
self.__close()
1457+
14521458
# compatibility
14531459

14541460
Server = ServerProxy

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Core and Builtins
2323
Library
2424
-------
2525

26+
- Issue #20627: xmlrpc.client.ServerProxy is now a context manager.
27+
2628
- Issue #19165: The formatter module now raises DeprecationWarning instead of
2729
PendingDeprecationWarning.
2830

0 commit comments

Comments
 (0)