Skip to content

Commit

Permalink
Merge pull request #498 from gschaffner/followup-479
Browse files Browse the repository at this point in the history
Avoid redefining `hasattr_static` on every `_check_attr` call
  • Loading branch information
comrumino committed Jul 15, 2022
2 parents 5a479cc + 53ff571 commit d1c3a06
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 1 addition & 11 deletions rpyc/core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import time # noqa: F401
import gc # noqa: F401

from inspect import getattr_static
from threading import Lock, Condition, RLock
from rpyc.lib import spawn, Timeout, get_methods, get_id_pack
from rpyc.lib import spawn, Timeout, get_methods, get_id_pack, hasattr_static
from rpyc.lib.compat import pickle, next, maxint, select_error, acquire_lock # noqa: F401
from rpyc.lib.colls import WeakValueDict, RefCountingColl
from rpyc.core import consts, brine, vinegar, netref
Expand Down Expand Up @@ -525,15 +524,6 @@ def root(self): # serving
return self._remote_root

def _check_attr(self, obj, name, perm): # attribute access
def hasattr_static(obj, name):
try:
getattr_static(obj, name)
except AttributeError:
return False
else:
return True


config = self._config
if not config[perm]:
raise AttributeError(f"cannot access {name!r}")
Expand Down
10 changes: 10 additions & 0 deletions rpyc/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ def __set__(self, obj, val):
raise AttributeError("Cannot overwrite method")


def hasattr_static(obj, attr):
"""Returns if `inspect.getattr_static` can find an attribute of ``obj``."""
try:
inspect.getattr_static(obj, attr)
except AttributeError:
return False
else:
return True


def spawn(*args, **kwargs):
"""Start and return daemon thread. ``spawn(func, *args, **kwargs)``."""
func, args = args[0], args[1:]
Expand Down

0 comments on commit d1c3a06

Please sign in to comment.