CactusDev / CactusBot

An open source, community-written service-agnostic chat bot
MIT License
30 stars 6 forks source link

Use context managers #256

Closed 2Cubed closed 7 years ago

2Cubed commented 7 years ago

We should be using context managers in a number of places which we currently aren't. (For example, WebSocket!)

In addition to making the code cleaner and more Pythonic, it will help with #62.

2Cubed commented 7 years ago

Our tests output the following:

Exception ignored in: <bound method ClientSession.__del__ of <cactusbot.api.CactusAPI object at 0x7f2afea59e10>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 96, in __del__
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 437, in close
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/connector.py", line 239, in close
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/helpers.py", line 90, in create_future
  File "/usr/lib/python3.5/asyncio/base_events.py", line 252, in create_future
AttributeError: 'NoneType' object has no attribute 'Future'
Exception ignored in: <bound method ClientSession.__del__ of <cactusbot.api.CactusAPI object at 0x7f2b01020710>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 96, in __del__
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 437, in close
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/connector.py", line 239, in close
TypeError: 'NoneType' object is not callable
Exception ignored in: <bound method ClientSession.__del__ of <cactusbot.api.CactusAPI object at 0x7f2afea6f1d0>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 96, in __del__
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 437, in close
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/connector.py", line 239, in close
TypeError: 'NoneType' object is not callable
Exception ignored in: <bound method ClientSession.__del__ of <cactusbot.api.CactusAPI object at 0x7f2afea6ff60>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 96, in __del__
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/client.py", line 437, in close
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/connector.py", line 239, in close
TypeError: 'NoneType' object is not callable
2Cubed commented 7 years ago

WebSocket was already using context managers, through aiohttp.ClientSession (were not overridden).

2Cubed commented 7 years ago

Context managers were implemented on fix/clean-shutdown. Improved our code's "Pythonic-ness" and overall cleanliness, but did not entirely solve #62.