Skip to content

Commit 39df8ed

Browse files
committed
Refactor plugin loading handler existence checks
1 parent 0aa963f commit 39df8ed

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

pyrogram/client.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,16 @@ def load_plugins(self):
895895
module = import_module(module_path)
896896

897897
for name in vars(module).keys():
898-
# noinspection PyBroadException
899-
try:
900-
for handler, group in getattr(module, name).handlers:
898+
target_attr = getattr(module, name)
899+
if hasattr(target_attr, "handlers"):
900+
for handler, group in target_attr.handlers:
901901
if isinstance(handler, Handler) and isinstance(group, int):
902902
self.add_handler(handler, group)
903903

904904
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
905905
self.name, type(handler).__name__, name, group, module_path))
906906

907907
count += 1
908-
except Exception:
909-
pass
910908
else:
911909
for path, handlers in include:
912910
module_path = root + "." + path
@@ -927,20 +925,19 @@ def load_plugins(self):
927925
warn_non_existent_functions = False
928926

929927
for name in handlers:
930-
# noinspection PyBroadException
931-
try:
932-
for handler, group in getattr(module, name).handlers:
928+
target_attr = getattr(module, name)
929+
if hasattr(target_attr, "handlers"):
930+
for handler, group in target_attr.handlers:
933931
if isinstance(handler, Handler) and isinstance(group, int):
934932
self.add_handler(handler, group)
935933

936934
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
937935
self.name, type(handler).__name__, name, group, module_path))
938936

939937
count += 1
940-
except Exception:
941-
if warn_non_existent_functions:
942-
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
943-
self.name, name, module_path))
938+
elif warn_non_existent_functions:
939+
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
940+
self.name, name, module_path))
944941

945942
if exclude:
946943
for path, handlers in exclude:
@@ -962,20 +959,19 @@ def load_plugins(self):
962959
warn_non_existent_functions = False
963960

964961
for name in handlers:
965-
# noinspection PyBroadException
966-
try:
967-
for handler, group in getattr(module, name).handlers:
962+
target_attr = getattr(module, name)
963+
if hasattr(target_attr, "handlers"):
964+
for handler, group in target_attr.handlers:
968965
if isinstance(handler, Handler) and isinstance(group, int):
969966
self.remove_handler(handler, group)
970967

971968
log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
972969
self.name, type(handler).__name__, name, group, module_path))
973970

974971
count -= 1
975-
except Exception:
976-
if warn_non_existent_functions:
977-
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
978-
self.name, name, module_path))
972+
elif warn_non_existent_functions:
973+
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
974+
self.name, name, module_path))
979975

980976
if count > 0:
981977
log.info('[{}] Successfully loaded {} plugin{} from "{}"'.format(

0 commit comments

Comments
 (0)