From 04dc1742ef2bd12b55e52498ee8d5cd95ab9a0ca Mon Sep 17 00:00:00 2001 From: Nabarun Pal Date: Wed, 11 Mar 2020 17:41:04 +0530 Subject: [PATCH] Apply hotfixes --- kubernetes/client/api/custom_objects_api.py | 12 +++++----- kubernetes/client/api_client.py | 12 +++++++++- kubernetes/client/apis/__init__.py | 13 +++++++++++ kubernetes/test/test_api_client.py | 25 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 kubernetes/client/apis/__init__.py create mode 100644 kubernetes/test/test_api_client.py diff --git a/kubernetes/client/api/custom_objects_api.py b/kubernetes/client/api/custom_objects_api.py index 10412abee..fcdb530a3 100644 --- a/kubernetes/client/api/custom_objects_api.py +++ b/kubernetes/client/api/custom_objects_api.py @@ -1719,7 +1719,7 @@ class CustomObjectsApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json']) # noqa: E501 + ['application/merge-patch+json']) # noqa: E501 # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -1851,7 +1851,7 @@ class CustomObjectsApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json']) # noqa: E501 + ['application/merge-patch+json']) # noqa: E501 # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -1983,7 +1983,7 @@ class CustomObjectsApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json']) # noqa: E501 + ['application/merge-patch+json']) # noqa: E501 # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -2123,7 +2123,7 @@ class CustomObjectsApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json']) # noqa: E501 + ['application/merge-patch+json']) # noqa: E501 # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -2263,7 +2263,7 @@ class CustomObjectsApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json']) # noqa: E501 + ['application/merge-patch+json']) # noqa: E501 # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -2403,7 +2403,7 @@ class CustomObjectsApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json']) # noqa: E501 + ['application/merge-patch+json']) # noqa: E501 # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 diff --git a/kubernetes/client/api_client.py b/kubernetes/client/api_client.py index 34d765854..6c9acf6b5 100644 --- a/kubernetes/client/api_client.py +++ b/kubernetes/client/api_client.py @@ -10,6 +10,7 @@ from __future__ import absolute_import +import atexit import datetime import json import mimetypes @@ -77,11 +78,19 @@ class ApiClient(object): # Set default User-Agent. self.user_agent = 'OpenAPI-Generator/11.0.0/python' - def __del__(self): + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + + def close(self): if self._pool: self._pool.close() self._pool.join() self._pool = None + if hasattr(atexit, 'unregister'): + atexit.unregister(self.close) @property def pool(self): @@ -89,6 +98,7 @@ class ApiClient(object): avoids instantiating unused threadpool for blocking clients. """ if self._pool is None: + atexit.register(self.close) self._pool = ThreadPool(self.pool_threads) return self._pool diff --git a/kubernetes/client/apis/__init__.py b/kubernetes/client/apis/__init__.py new file mode 100644 index 000000000..ca4b321de --- /dev/null +++ b/kubernetes/client/apis/__init__.py @@ -0,0 +1,13 @@ +from __future__ import absolute_import +import warnings + +# flake8: noqa + +# alias kubernetes.client.api package and print deprecation warning +from kubernetes.client.api import * + +warnings.filterwarnings('default', module='kubernetes.client.apis') +warnings.warn( + "The package kubernetes.client.apis is renamed and deprecated, use kubernetes.client.api instead (please note that the trailing s was removed).", + DeprecationWarning +) diff --git a/kubernetes/test/test_api_client.py b/kubernetes/test/test_api_client.py new file mode 100644 index 000000000..f0a9416cf --- /dev/null +++ b/kubernetes/test/test_api_client.py @@ -0,0 +1,25 @@ +# coding: utf-8 + + +import atexit +import weakref +import unittest + +import kubernetes + + +class TestApiClient(unittest.TestCase): + + def test_context_manager_closes_threadpool(self): + with kubernetes.client.ApiClient() as client: + self.assertIsNotNone(client.pool) + pool_ref = weakref.ref(client._pool) + self.assertIsNotNone(pool_ref()) + self.assertIsNone(pool_ref()) + + def test_atexit_closes_threadpool(self): + client = kubernetes.client.ApiClient() + self.assertIsNotNone(client.pool) + self.assertIsNotNone(client._pool) + atexit._run_exitfuncs() + self.assertIsNone(client._pool)