mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Avoid multiple getattrs instead of a single one, see also #1933
This commit is contained in:
@@ -9,7 +9,7 @@ import re
|
|||||||
import builtins
|
import builtins
|
||||||
import typing
|
import typing
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from jedi.inference.compiled.getattr_static import getattr_static
|
from jedi.inference.compiled.getattr_static import getattr_static
|
||||||
|
|
||||||
@@ -329,26 +329,26 @@ class DirectObjectAccess:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_allowed_getattr(self, name, safe=True):
|
def is_allowed_getattr(self, name, safe=True) -> Tuple[bool, bool]:
|
||||||
# TODO this API is ugly.
|
# TODO this API is ugly.
|
||||||
if not safe:
|
|
||||||
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
|
||||||
# getattr_static works for properties, but the underscore methods
|
|
||||||
# are just ignored (because it's safer and avoids more code
|
|
||||||
# execution). See also GH #1378.
|
|
||||||
|
|
||||||
# Avoid warnings, see comment in the next function.
|
|
||||||
with warnings.catch_warnings(record=True):
|
|
||||||
warnings.simplefilter("always")
|
|
||||||
try:
|
|
||||||
return hasattr(self._obj, name), False
|
|
||||||
except Exception:
|
|
||||||
# Obviously has an attribute (probably a property) that
|
|
||||||
# gets executed, so just avoid all exceptions here.
|
|
||||||
return False, False
|
|
||||||
try:
|
try:
|
||||||
attr, is_get_descriptor = getattr_static(self._obj, name)
|
attr, is_get_descriptor = getattr_static(self._obj, name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
if not safe:
|
||||||
|
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
||||||
|
# getattr_static works for properties, but the underscore methods
|
||||||
|
# are just ignored (because it's safer and avoids more code
|
||||||
|
# execution). See also GH #1378.
|
||||||
|
|
||||||
|
# Avoid warnings, see comment in the next function.
|
||||||
|
with warnings.catch_warnings(record=True):
|
||||||
|
warnings.simplefilter("always")
|
||||||
|
try:
|
||||||
|
return hasattr(self._obj, name), False
|
||||||
|
except Exception:
|
||||||
|
# Obviously has an attribute (probably a property) that
|
||||||
|
# gets executed, so just avoid all exceptions here.
|
||||||
|
pass
|
||||||
return False, False
|
return False, False
|
||||||
else:
|
else:
|
||||||
if is_get_descriptor and type(attr) not in ALLOWED_DESCRIPTOR_ACCESS:
|
if is_get_descriptor and type(attr) not in ALLOWED_DESCRIPTOR_ACCESS:
|
||||||
|
|||||||
Reference in New Issue
Block a user