From 305bce5a785247900d2d5a91718074375cdbc3ca Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Mon, 27 Jan 2020 16:28:08 +0100 Subject: [PATCH] Enforce Cura API to be a singleton If it's not used that way we want to know about it and fail. If plug-ins use it this way, the plug-in won't get initialised so it'll be as if the plug-in is disabled or that function doesn't work. Contributes to issue CURA-7135. --- cura/API/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cura/API/__init__.py b/cura/API/__init__.py index b3e702263a..26c9a4c829 100644 --- a/cura/API/__init__.py +++ b/cura/API/__init__.py @@ -28,11 +28,12 @@ class CuraAPI(QObject): # The main reason for this is that we want to prevent consumers of API to have a dependency on CuraApplication. # Since the API is intended to be used by plugins, the cura application should have already created this. def __new__(cls, application: Optional["CuraApplication"] = None): - if cls.__instance is None: - if application is None: - raise Exception("Upon first time creation, the application must be set.") - cls.__instance = super(CuraAPI, cls).__new__(cls) - cls._application = application + if cls.__instance is not None: + raise RuntimeError("Tried to create singleton '{class_name}' more than once.".format(class_name = CuraAPI.__name__)) + if application is None: + raise RuntimeError("Upon first time creation, the application must be set.") + cls.__instance = super(CuraAPI, cls).__new__(cls) + cls._application = application return cls.__instance def __init__(self, application: Optional["CuraApplication"] = None) -> None: