From 44f89f63dba3eac7bca69db73897e9e4a044d980 Mon Sep 17 00:00:00 2001 From: satoon101 Date: Sat, 4 Jan 2020 12:16:26 -0500 Subject: [PATCH 1/2] Added caching boolean argument for Player.from_userid. --- addons/source-python/packages/source-python/players/_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/source-python/packages/source-python/players/_base.py b/addons/source-python/packages/source-python/players/_base.py index cf3b5b57c..739890357 100644 --- a/addons/source-python/packages/source-python/players/_base.py +++ b/addons/source-python/packages/source-python/players/_base.py @@ -92,14 +92,14 @@ def __init__(self, index, caching=True): object.__setattr__(self, '_playerinfo', None) @classmethod - def from_userid(cls, userid): + def from_userid(cls, userid, caching=True): """Create an instance from a userid. :param int userid: The userid. :rtype: Player """ - return cls(index_from_userid(userid)) + return cls(index_from_userid(userid), caching=caching) @property def net_info(self): From f90078c980b9306f49fd3b53ebe5766ca20218d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordan=20Bri=C3=A8re?= Date: Wed, 8 Jan 2020 20:55:20 -0500 Subject: [PATCH 2/2] Disabled instance caching for Entity's subclasses that do not explicitly enable it (to retain backward compatibility for existing classes that were not implemented with caching in mind). Added caching keyword to Entity.from_inthandle. Fixed Player.from_userid not following the caching state of the current class unless explicitly specified. Added missing documentation. --- .../packages/source-python/entities/_base.py | 16 ++++++++++------ .../packages/source-python/players/_base.py | 4 +++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/addons/source-python/packages/source-python/entities/_base.py b/addons/source-python/packages/source-python/entities/_base.py index 03520443a..8aa2b756d 100644 --- a/addons/source-python/packages/source-python/entities/_base.py +++ b/addons/source-python/packages/source-python/entities/_base.py @@ -89,11 +89,13 @@ def __init__(cls, classname, bases, attributes): # Set whether or not this class is caching its instances by default try: - cls._caching = signature( - cls.__init__ - ).parameters['caching'].default + cls._caching = bool( + signature( + vars(cls)['__init__'] + ).parameters['caching'].default + ) except KeyError: - cls._caching = True + cls._caching = bool(vars(cls).get('caching', False)) # Add the class to the registered classes _entity_classes.add(cls) @@ -300,14 +302,16 @@ def find_or_create(cls, classname): return entity @classmethod - def from_inthandle(cls, inthandle): + def from_inthandle(cls, inthandle, caching=None): """Create an entity instance from an inthandle. :param int inthandle: The inthandle. + :param bool caching: + Whether to lookup the cache for an existing instance or not. :rtype: Entity """ - return cls(index_from_inthandle(inthandle)) + return cls(index_from_inthandle(inthandle), caching=caching) @classmethod def _obj(cls, ptr): diff --git a/addons/source-python/packages/source-python/players/_base.py b/addons/source-python/packages/source-python/players/_base.py index 739890357..eda5700e2 100644 --- a/addons/source-python/packages/source-python/players/_base.py +++ b/addons/source-python/packages/source-python/players/_base.py @@ -92,11 +92,13 @@ def __init__(self, index, caching=True): object.__setattr__(self, '_playerinfo', None) @classmethod - def from_userid(cls, userid, caching=True): + def from_userid(cls, userid, caching=None): """Create an instance from a userid. :param int userid: The userid. + :param bool caching: + Whether to lookup the cache for an existing instance or not. :rtype: Player """ return cls(index_from_userid(userid), caching=caching)