Skip to content

Commit 6fa1b68

Browse files
authored
Simplify KeyError fallbacks that don't need to be lazy (#2330)
1 parent 22b0c0a commit 6fa1b68

14 files changed

Lines changed: 29 additions & 74 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ https://mhammond.github.io/pywin32_installers.html.
1414
Coming in build 309, as yet unreleased
1515
--------------------------------------
1616

17-
* Improved handling of dict iterations (removes Python 2 support code, small general speed improvement) (#2332, @Avasam)
17+
* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, @Avasam)
1818
* Fixed "Open GL Demo" (`Pythonwin/pywin/Demos/openGLDemo.py`) and restore "Font" demo in `Pythonwin/pywin/Demos/guidemo.py` (#2345, @Avasam)
1919

2020
Build 308, released 2024-10-12

Pythonwin/pywin/Demos/cmdserver.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ def unregister(self):
3737

3838
def getwriter(self):
3939
"Return the current thread's writer, default sys.stdout"
40-
try:
41-
return self.writers[_thread.get_ident()]
42-
except KeyError:
43-
return self.origStdOut
40+
self.writers.get(_thread.get_ident(), self.origStdOut)
4441

4542
def write(self, str):
4643
"Write to the current thread's writer, default sys.stdout"

com/win32com/client/combrowse.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ class HLITypeLibEntry(HLICOM):
265265
def GetText(self):
266266
tlb, index = self.myobject
267267
name, doc, ctx, helpFile = tlb.GetDocumentation(index)
268-
try:
269-
typedesc = HLITypeKinds[tlb.GetTypeInfoType(index)][1]
270-
except KeyError:
271-
typedesc = "Unknown!"
268+
typedesc = HLITypeKinds.get(tlb.GetTypeInfoType(index), (None, "Unknown!"))[1]
272269
return name + " - " + typedesc
273270

274271
def GetSubList(self):
@@ -448,10 +445,7 @@ def GetText(self):
448445

449446
def MakeReturnTypeName(self, typ):
450447
justtyp = typ & pythoncom.VT_TYPEMASK
451-
try:
452-
typname = self.vartypes[justtyp]
453-
except KeyError:
454-
typname = "?Bad type?"
448+
typname = self.vartypes.get(justtyp, "?Bad type?")
455449
for flag, desc in self.type_flags:
456450
if flag & typ:
457451
typname = f"{desc}({typname})"
@@ -493,15 +487,9 @@ def GetSubList(self):
493487
val += f" (Default={default})"
494488
ret.append(browser.MakeHLI(val, "Argument"))
495489

496-
try:
497-
fkind = self.funckinds[fd[3]]
498-
except KeyError:
499-
fkind = "Unknown"
490+
fkind = self.funckinds.get(fd[3], "Unknown")
500491
ret.append(browser.MakeHLI(fkind, "Function Kind"))
501-
try:
502-
ikind = self.invokekinds[fd[4]]
503-
except KeyError:
504-
ikind = "Unknown"
492+
ikind = self.invokekinds.get([fd[4]], "Unknown")
505493
ret.append(browser.MakeHLI(ikind, "Invoke Kind"))
506494
# 5 = call conv
507495
# 5 = offset vtbl

com/win32com/servers/dictionary.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
8585
if wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET):
8686
if l > 1:
8787
raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
88-
try:
89-
return self._obj_[key]
90-
except KeyError:
91-
return None # unknown keys return None (VT_NULL)
88+
return self._obj_.get(key) # unknown keys return None (VT_NULL)
9289

9390
if l != 2:
9491
raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)

com/win32com/test/testExchange.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ def TestUser(session):
8686
print("User has %d fields" % len(fields))
8787
for f in range(len(fields)):
8888
field = fields[f + 1]
89-
try:
90-
id = PropTagsById[field.ID]
91-
except KeyError:
92-
id = field.ID
89+
id = PropTagsById.get(field.ID, field.ID)
9390
print(f"{field.Name}/{id}={field.Value}")
9491

9592

com/win32comext/axdebug/adb.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ def fnull(*args):
1919
pass
2020

2121

22-
try:
23-
os.environ["DEBUG_AXDEBUG"]
24-
debugging = 1
25-
except KeyError:
26-
debugging = 0
27-
22+
debugging = "DEBUG_AXDEBUG" in os.environ
2823
traceenter = fnull # trace enter of functions
2924
tracev = fnull # verbose trace
3025

com/win32comext/axdebug/codecontainer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ def GetCodeContextAtPosition(self, charPos):
197197
charPos = self.GetPositionOfLine(lineNo)
198198
try:
199199
cc = self.codeContexts[charPos]
200-
# trace(" GetContextOfPos using existing")
201200
except KeyError:
202201
cc = self._MakeContextAtPosition(charPos)
203202
self.codeContexts[charPos] = cc

com/win32comext/axdebug/util.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
import winerror
1111
from win32com.server.exception import COMException
1212

13-
try:
14-
os.environ["DEBUG_AXDEBUG"]
15-
debugging = 1
16-
except KeyError:
17-
debugging = 0
13+
debugging = "DEBUG_AXDEBUG" in os.environ
1814

1915

2016
def trace(*args):

com/win32comext/axscript/client/debug.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
from win32com.server.exception import COMException
1313
from win32com.util import IIDToInterfaceName
1414

15-
try:
16-
os.environ["DEBUG_AXDEBUG"]
17-
debuggingTrace = 1 # Should we print "trace" output?
18-
except KeyError:
19-
debuggingTrace = 0
15+
debuggingTrace = "DEBUG_AXDEBUG" in os.environ # Should we print "trace" output?
2016

2117

2218
def trace(*args):

com/win32comext/axscript/client/pyscript.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,12 @@ def Register(self):
160160
self.attributeObject = NamedScriptAttribute(self)
161161
if self.dispatch:
162162
# Need to avoid the new Python "lazy" dispatch behaviour.
163+
olerepr, clsid = None
163164
try:
164165
engine = self.GetEngine()
165-
olerepr = clsid = None
166166
typeinfo = self.dispatch.GetTypeInfo()
167167
clsid = typeinfo.GetTypeAttr()[0]
168-
try:
169-
olerepr = engine.mapKnownCOMTypes[clsid]
170-
except KeyError:
171-
pass
168+
olerepr = engine.mapKnownCOMTypes.get(clsid)
172169
except pythoncom.com_error:
173170
typeinfo = None
174171
if olerepr is None:

0 commit comments

Comments
 (0)