Skip to content

Commit 0717e88

Browse files
authored
Fix Unraisable exception in PyTest (#43)
* Fixing test-tox and test-example * Fix ResourceWarning: unclosed ssl.SSLSocket * Fix ResourceWarning: unclosed ssl.SSLSocket for ApiClient * Fix tests on Windows
1 parent b7ce7ca commit 0717e88

File tree

7 files changed

+32
-15
lines changed

7 files changed

+32
-15
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
- name: Test
3131
env:
3232
TEST_CONFIGURATION_ACCESS_TOKEN: ${{ secrets.TEST_CONFIGURATION_ACCESS_TOKEN }}
33-
run: make test
33+
run: make test test-example

Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SRC=./aspose_barcode_cloud
22

33
.PHONY: all
4-
all: format lint test-all
4+
all: format lint test-tox
55

66
.PHONY: check_git
77
check_git:
@@ -47,19 +47,23 @@ lint:
4747
flake8 . --count --exit-zero --max-line-length=127 --statistics --extend-ignore=E501 --extend-exclude '.*'
4848

4949
.PHONY: publish
50-
publish: check_git test-all clean dist
50+
publish: check_git test-tox clean dist
5151
python3 -m twine upload dist/*
5252

5353
.PHONY: publish-docker
54-
publish-docker: init-docker test-all dist
54+
publish-docker: init-docker test-tox dist
5555
python3 -m twine upload dist/* --verbose
5656

5757
.PHONY: test
5858
test:
5959
python -Werror -m pytest --cov tests/
6060

61-
.PHONY: test-all
62-
test-all:
61+
.PHONY: test-example
62+
test-example:
63+
python -Werror example.py
64+
65+
.PHONY: test-tox
66+
test-tox:
6367
tox $(SRC)
6468

6569
.PHONY: update

aspose_barcode_cloud/api_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None, cook
9393
self.user_agent = "Aspose-Barcode-SDK/22.8.0/python"
9494

9595
def __del__(self):
96+
self.rest_client.close()
9697
if self._pool is not None:
9798
self._pool.close()
9899
self._pool.join()

aspose_barcode_cloud/configuration.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from __future__ import absolute_import, division
2828

29+
import contextlib
2930
import copy
3031
import json
3132
import logging
@@ -290,12 +291,20 @@ def to_debug_report(self):
290291

291292
@staticmethod
292293
def fetch_token(client_id, client_secret, token_url):
293-
client = RESTClientObject(Configuration(client_id=client_id, client_secret=client_secret, token_url=token_url))
294-
response = client.POST(
295-
token_url,
296-
headers={"Content-Type": "application/x-www-form-urlencoded"},
297-
post_params={"grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret},
298-
)
299-
js_data = json.loads(response.data)
294+
with contextlib.closing(
295+
RESTClientObject(Configuration(client_id=client_id, client_secret=client_secret, token_url=token_url))
296+
) as client:
297+
response = client.POST(
298+
token_url,
299+
headers={"Content-Type": "application/x-www-form-urlencoded"},
300+
post_params={
301+
"grant_type": "client_credentials",
302+
"client_id": client_id,
303+
"client_secret": client_secret,
304+
},
305+
)
306+
js_data = json.loads(response.data)
307+
300308
access_token = js_data["access_token"]
309+
301310
return access_token

aspose_barcode_cloud/rest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ def PATCH(
370370
body=body,
371371
)
372372

373+
def close(self):
374+
self.pool_manager.clear()
375+
373376

374377
class ApiException(Exception):
375378
def __init__(self, status=None, reason=None, http_resp=None):

tests/test_generate_and_recognize.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def test_generate_and_recognize(self):
1818
generated = self.api.get_barcode_generate(EncodeBarcodeType.QR, "Should recognize this")
1919
self.assertGreater(len(generated.data), 0)
2020

21-
_, temp_filename = tempfile.mkstemp()
21+
fd, temp_filename = tempfile.mkstemp()
22+
os.close(fd)
2223
try:
2324
with open(temp_filename, "wb") as f:
2425
f.write(generated.data)

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ deps=-r{toxinidir}/requirements.txt
1010

1111
commands=
1212
make test
13-
python -Werror example.py

0 commit comments

Comments
 (0)