Use if-else instead of try-except for perf reason
This commit is contained in:
parent
2e8944916c
commit
84f1b433de
1 changed files with 6 additions and 6 deletions
|
|
@ -141,9 +141,10 @@ def wrap(cap, owned=False):
|
||||||
cls = cap.get_class()
|
cls = cap.get_class()
|
||||||
addr = cap.pointer
|
addr = cap.pointer
|
||||||
name = cap.name
|
name = cap.name
|
||||||
try: # lookup cached object
|
# lookup cached object
|
||||||
return _cache[cls][addr]
|
if cls in _cache and addr in _cache[cls]:
|
||||||
except KeyError:
|
obj = _cache[cls][addr]
|
||||||
|
else:
|
||||||
if not owned and cls._has_dtor():
|
if not owned and cls._has_dtor():
|
||||||
_addr2dtor[(name, addr)] = cls._delete_
|
_addr2dtor[(name, addr)] = cls._delete_
|
||||||
obj = cap.instantiate()
|
obj = cap.instantiate()
|
||||||
|
|
@ -207,11 +208,10 @@ def downcast(obj, cls):
|
||||||
logger.debug("Downcast %s to %s" , fromty, toty)
|
logger.debug("Downcast %s to %s" , fromty, toty)
|
||||||
fname = 'downcast_%s_to_%s' % (fromty, toty)
|
fname = 'downcast_%s_to_%s' % (fromty, toty)
|
||||||
fname = fname.replace('::', '_')
|
fname = fname.replace('::', '_')
|
||||||
try:
|
if not hasattr(_api.downcast, fname):
|
||||||
caster = getattr(_api.downcast, fname)
|
|
||||||
except AttributeError:
|
|
||||||
fmt = "Downcast from %s to %s is not supported"
|
fmt = "Downcast from %s to %s is not supported"
|
||||||
raise TypeError(fmt % (fromty, toty))
|
raise TypeError(fmt % (fromty, toty))
|
||||||
|
caster = getattr(_api.downcast, fname)
|
||||||
old = unwrap(obj)
|
old = unwrap(obj)
|
||||||
new = caster(old)
|
new = caster(old)
|
||||||
used_to_own = has_ownership(old)
|
used_to_own = has_ownership(old)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue