Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1958

Cannot import pymongo when pymongocrypt's dlopen fails

      When pymongocrypt fails to find the libmongocrypt dll it raises an OSError:

      python -c 'import pymongo'
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
        File "/Users/shane/git/mongo-python-driver/pymongo/__init__.py", line 77, in <module>
          from pymongo.collection import ReturnDocument
        File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 29, in <module>
          from pymongo import (common,
        File "/Users/shane/git/mongo-python-driver/pymongo/common.py", line 31, in <module>
          from pymongo.encryption_options import validate_auto_encryption_opts_or_none
        File "/Users/shane/git/mongo-python-driver/pymongo/encryption_options.py", line 20, in <module>
          import pymongocrypt
        File "/Users/shane/git/libmongocrypt/bindings/python/pymongocrypt/__init__.py", line 15, in <module>
          from pymongocrypt.binding import libmongocrypt_version, lib
        File "/Users/shane/git/libmongocrypt/bindings/python/pymongocrypt/binding.py", line 919, in <module>
          lib = ffi.dlopen(os.environ.get('PYMONGOCRYPT_LIB', 'mongocrypt'))
        File "/usr/local/Cellar/pypy3/7.0.0/libexec/lib_pypy/cffi/api.py", line 146, in dlopen
          lib, function_cache = _make_ffi_library(self, name, flags)
        File "/usr/local/Cellar/pypy3/7.0.0/libexec/lib_pypy/cffi/api.py", line 828, in _make_ffi_library
          backendlib = _load_backend_lib(backend, libname, flags)
        File "/usr/local/Cellar/pypy3/7.0.0/libexec/lib_pypy/cffi/api.py", line 823, in _load_backend_lib
          raise OSError(msg)
      OSError: ctypes.util.find_library() did not manage to locate a library called 'mongocrypt'
      

      We can fix this in two ways:

      1. make pymongocrypt catch the OSError and add a new function to check if the library was loaded correctly called "is_initialized()". PyMongo can call is_initialized() when creating an AutoEncryptionOpts or ClientEncryption.
      2. make pymongo catch OSError when importing pymongocrypt. Later we can raise an informative error to the user about what went wrong.

      I prefer option 1 because it abstracts libmongocrypt better; pymongo should not need to know or care that pymongocrypt uses CFFI.

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: