From 070df5a507a3a209521a6187da518caddd981db7 Mon Sep 17 00:00:00 2001 From: Dillon Walls Date: Mon, 21 Oct 2024 17:46:05 -0400 Subject: [PATCH] removed pin on pymongo<4.9 and fixed errors and warnings --- ming/datastore.py | 15 +++++++++++++-- ming/mim.py | 14 +++++++++++--- setup.py | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/ming/datastore.py b/ming/datastore.py index 3187bc0..565beb2 100644 --- a/ming/datastore.py +++ b/ming/datastore.py @@ -1,10 +1,14 @@ from __future__ import annotations import time +from contextlib import closing import logging from threading import Lock from typing import Union, TYPE_CHECKING import urllib +import warnings +import weakref + from pymongo import MongoClient from pymongo.database import Database from pymongo.encryption import ClientEncryption, Algorithm @@ -96,8 +100,8 @@ def create_datastore(uri, **kwargs) -> DataStore: # Create engine without connection. bind = create_engine(**kwargs) - return DataStore(bind, database, encryption_config) + return DataStore(bind, database, encryption_config) class Engine: """Engine represents the connection to a MongoDB (or in-memory database). @@ -106,6 +110,11 @@ class Engine: accessed. """ + @staticmethod + def _cleanup_conn(client, *args, **kwargs): + if getattr(client, 'close', None) is not None: + client.close() + def __init__(self, Connection, conn_args, conn_kwargs, connect_retry, auto_ensure_indexes, _sleep=time.sleep): self._Connection = Connection @@ -147,8 +156,10 @@ def connect(self): with self._lock: if self._conn is None: # NOTE: Runs MongoClient/EncryptionClient - self._conn = self._Connection( + conn = self._Connection( *self._conn_args, **self._conn_kwargs) + weakref.finalize(self, Engine._cleanup_conn, conn) + self._conn = conn else: return self._conn except ConnectionFailure: diff --git a/ming/mim.py b/ming/mim.py index e2244af..4c1e298 100644 --- a/ming/mim.py +++ b/ming/mim.py @@ -35,7 +35,7 @@ from bson.binary import UuidRepresentation, Binary from bson.codec_options import CodecOptions from bson.raw_bson import RawBSONDocument -from pymongo import database, collection, ASCENDING, MongoClient, UpdateOne +from pymongo import database, collection, ASCENDING, MongoClient as RealMongoClient, UpdateOne from pymongo.cursor import Cursor as PymongoCursor from pymongo.errors import InvalidOperation, OperationFailure, DuplicateKeyError from pymongo.results import DeleteResult, UpdateResult, InsertManyResult, InsertOneResult, BulkWriteResult @@ -51,7 +51,11 @@ def __del__(self): pass -class Connection: +class MongoClient: + pass + + +class Connection(MongoClient): _singleton = None @classmethod @@ -64,12 +68,13 @@ def __init__(self): self._databases = {} # Clone defaults from a MongoClient instance. - mongoclient = MongoClient(uuidRepresentation=UUID_REPRESENTATION_STR) + mongoclient = RealMongoClient(uuidRepresentation=UUID_REPRESENTATION_STR) self.options = mongoclient.options self.read_preference = mongoclient.read_preference self.write_concern = mongoclient.write_concern self.codec_options = mongoclient.codec_options self.read_concern = getattr(mongoclient, 'read_concern', None) + mongoclient.close() def drop_all(self): self._databases = {} @@ -98,6 +103,9 @@ def _get(self, name): def list_database_names(self): return self._databases.keys() + def close(self): + pass + def drop_database(self, name): try: del self._databases[name] diff --git a/setup.py b/setup.py index 7ca128e..189f169 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ include_package_data=True, zip_safe=True, install_requires=[ - "pymongo[encryption]<4.9", + "pymongo[encryption]", "pytz", ], tests_require=[