python-fmrest icon indicating copy to clipboard operation
python-fmrest copied to clipboard

logout error in destructor of my class

Open nexgen opened this issue 1 year ago • 3 comments

I have created a wrapper around python-fmrest where I have:

def init(self, fm_server_url, fm_user, fm_password, fm_db_name): self.fms.login()

def del(self): self.fms.logout()

I get this error: Exception ignored in: <function FileMaker.del at 0x118b38ca0> Traceback (most recent call last): File "filemaker_utils.py", line 21, in del File ".venv/lib/python3.10/site-packages/fmrest/server.py", line 217, in logout File ".venv/lib/python3.10/site-packages/fmrest/server.py", line 925, in _call_filemaker File ".venv/lib/python3.10/site-packages/fmrest/utils.py", line 14, in request fmrest.exceptions.RequestException: Request error: import of time halted; None in sys.modules

nexgen avatar Dec 30 '24 06:12 nexgen

It's an import error and hard to tell without seeing the full project. Maybe you have shadowed time in your project? Maybe a circular import?

If I see correctly, the exception is raised by the requests library and then re-raised by python-fmrest. time is not directly imported in the fmrest utils.

You could check what print(sys.modules.get("time")) returns for you. Maybe it's not <module 'time' (built-in)>?

davidhamann avatar Jan 01 '25 22:01 davidhamann

Here is the simplified working code for you to replicate this problem.

class FileMaker:
    def __init__(self, fm_server_url, fm_user, fm_password, fm_db_name):
        layout = "GLOBAL"
        self.fms = fmrest.Server(
            fm_server_url,
            user=fm_user,
            password=fm_password,
            database=fm_db_name,
            layout=layout,
            verify_ssl=True,
            api_version="v1",
        )

        self.fms.login()

    def __del__(self):
        self.fms.logout()

fm_server_url = ""
fm_user = ""
fm_password = ""
fm_db_name = ""
fm = Filemaker(fm_server_url, fm_user, fm_password, fm_db_name)

nexgen avatar Jan 03 '25 13:01 nexgen

I believe the issue is that you're calling the logout during shutdown of the interpreter when some imports have already been cleared (when __del__ is called).

I would suggest using the context manager of fmrest (or a context manager for your wrapper):

fms = fmrest.Server(...)
with fms:
    fms.login()
    # do stuff
# continue with other stuff

This would also handle the logout for you.

davidhamann avatar Jan 12 '25 21:01 davidhamann