THRIFT-3618 Python TSSLSocket deprecation message should print caller's location
Client: Python
Patch: Nobuaki Sukegawa

This closes #852
diff --git a/lib/py/src/transport/TSSLSocket.py b/lib/py/src/transport/TSSLSocket.py
index f7122fe..a66461a 100644
--- a/lib/py/src/transport/TSSLSocket.py
+++ b/lib/py/src/transport/TSSLSocket.py
@@ -88,9 +88,9 @@
             return
         real_pos = pos + 3
         warnings.warn(
-            '%dth positional argument is deprecated. Use keyward argument insteand.'
-            % real_pos,
-            DeprecationWarning)
+            '%dth positional argument is deprecated.'
+            'please use keyward argument insteand.'
+            % real_pos, DeprecationWarning, stacklevel=3)
 
         if key in kwargs:
             raise TypeError(
@@ -107,16 +107,19 @@
 
     def __getattr__(self, key):
         if key == 'SSL_VERSION':
-            warnings.warn('Use ssl_version attribute instead.',
-                          DeprecationWarning)
+            warnings.warn(
+                'SSL_VERSION is deprecated.'
+                'please use ssl_version attribute instead.',
+                DeprecationWarning, stacklevel=2)
             return self.ssl_version
 
     def __init__(self, server_side, host, ssl_opts):
         self._server_side = server_side
         if TSSLBase.SSL_VERSION != self._default_protocol:
             warnings.warn(
-                'SSL_VERSION is deprecated. Use ssl_version keyward argument instead.',
-                DeprecationWarning)
+                'SSL_VERSION is deprecated.'
+                'please use ssl_version keyward argument instead.',
+                DeprecationWarning, stacklevel=2)
         self._context = ssl_opts.pop('ssl_context', None)
         self._server_hostname = None
         if not self._server_side:
@@ -248,9 +251,9 @@
         if validate is not None:
             cert_reqs_name = 'CERT_REQUIRED' if validate else 'CERT_NONE'
             warnings.warn(
-                'validate is deprecated. Use cert_reqs=ssl.%s instead'
+                'validate is deprecated. please use cert_reqs=ssl.%s instead'
                 % cert_reqs_name,
-                DeprecationWarning)
+                DeprecationWarning, stacklevel=2)
             if 'cert_reqs' in kwargs:
                 raise TypeError('Cannot specify both validate and cert_reqs')
             kwargs['cert_reqs'] = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE
@@ -262,12 +265,14 @@
 
     @property
     def validate(self):
-        warnings.warn('Use cert_reqs instead', DeprecationWarning)
+        warnings.warn('validate is deprecated. please use cert_reqs instead',
+                      DeprecationWarning, stacklevel=2)
         return self.cert_reqs != ssl.CERT_NONE
 
     @validate.setter
     def validate(self, value):
-        warnings.warn('Use cert_reqs instead', DeprecationWarning)
+        warnings.warn('validate is deprecated. please use cert_reqs instead',
+                      DeprecationWarning, stacklevel=2)
         self.cert_reqs = ssl.CERT_REQUIRED if value else ssl.CERT_NONE
 
     def open(self):
@@ -409,7 +414,9 @@
 
         Raises an IOError exception if the certfile is not present or unreadable.
         """
-        warnings.warn('Use certfile property instead.', DeprecationWarning)
+        warnings.warn(
+            'setCertfile is deprecated. please use certfile property instead.',
+            DeprecationWarning, stacklevel=2)
         self.certfile = certfile
 
     def accept(self):
@@ -440,5 +447,5 @@
                 return None
 
         result = TSocket.TSocket()
-        result.setHandle(client)
+        result.handle = client
         return result
diff --git a/lib/py/test/test_sslsocket.py b/lib/py/test/test_sslsocket.py
index c76d6d2..259945f 100644
--- a/lib/py/test/test_sslsocket.py
+++ b/lib/py/test/test_sslsocket.py
@@ -28,9 +28,7 @@
 import warnings
 from contextlib import contextmanager
 
-import six
-
-import _import_local_thrift
+import _import_local_thrift  # noqa
 from thrift.transport.TSSLSocket import TSSLSocket, TSSLServerSocket
 from thrift.transport.TTransport import TTransportException
 
@@ -153,49 +151,51 @@
 
     # deprecated feature
     def test_deprecation(self):
-        if not six.PY3:
-            # The checks below currently only work for python3.
-            # See: https://issues.apache.org/jira/browse/THRIFT-3618
-            print('skiping test_deprecation')
-            return
-
         with warnings.catch_warnings(record=True) as w:
-            warnings.filterwarnings('always', category=DeprecationWarning, module='thrift.*SSL.*')
+            warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
             TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
             self.assertEqual(len(w), 1)
 
         with warnings.catch_warnings(record=True) as w:
-            warnings.filterwarnings('always', category=DeprecationWarning, module='thrift.*SSL.*')
+            warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
             # Deprecated signature
             # def __init__(self, host='localhost', port=9090, validate=True, ca_certs=None, keyfile=None, certfile=None, unix_socket=None, ciphers=None):
-            client = TSSLSocket('localhost', 0, True, SERVER_CERT, CLIENT_KEY, CLIENT_CERT, None, TEST_CIPHERS)
+            TSSLSocket('localhost', 0, True, SERVER_CERT, CLIENT_KEY, CLIENT_CERT, None, TEST_CIPHERS)
             self.assertEqual(len(w), 7)
 
         with warnings.catch_warnings(record=True) as w:
-            warnings.filterwarnings('always', category=DeprecationWarning, module='thrift.*SSL.*')
+            warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
             # Deprecated signature
             # def __init__(self, host=None, port=9090, certfile='cert.pem', unix_socket=None, ciphers=None):
-            server = TSSLServerSocket(None, 0, SERVER_PEM, None, TEST_CIPHERS)
+            TSSLServerSocket(None, 0, SERVER_PEM, None, TEST_CIPHERS)
             self.assertEqual(len(w), 3)
 
     # deprecated feature
     def test_set_cert_reqs_by_validate(self):
-        c1 = TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
-        self.assertEqual(c1.cert_reqs, ssl.CERT_REQUIRED)
+        with warnings.catch_warnings(record=True) as w:
+            warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
+            c1 = TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
+            self.assertEqual(c1.cert_reqs, ssl.CERT_REQUIRED)
 
-        c1 = TSSLSocket('localhost', 0, validate=False)
-        self.assertEqual(c1.cert_reqs, ssl.CERT_NONE)
+            c1 = TSSLSocket('localhost', 0, validate=False)
+            self.assertEqual(c1.cert_reqs, ssl.CERT_NONE)
+
+            self.assertEqual(len(w), 2)
 
     # deprecated feature
     def test_set_validate_by_cert_reqs(self):
-        c1 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_NONE)
-        self.assertFalse(c1.validate)
+        with warnings.catch_warnings(record=True) as w:
+            warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
+            c1 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_NONE)
+            self.assertFalse(c1.validate)
 
-        c2 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
-        self.assertTrue(c2.validate)
+            c2 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
+            self.assertTrue(c2.validate)
 
-        c3 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_OPTIONAL, ca_certs=SERVER_CERT)
-        self.assertTrue(c3.validate)
+            c3 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_OPTIONAL, ca_certs=SERVER_CERT)
+            self.assertTrue(c3.validate)
+
+            self.assertEqual(len(w), 3)
 
     def test_unix_domain_socket(self):
         if platform.system() == 'Windows':