Skip to content

Simplify KeyError fallbacks that don't need to be lazy#2330

Merged
Avasam merged 6 commits intomhammond:mainfrom
Avasam:simplify-KeyError-fallbacks
Oct 13, 2024
Merged

Simplify KeyError fallbacks that don't need to be lazy#2330
Avasam merged 6 commits intomhammond:mainfrom
Avasam:simplify-KeyError-fallbacks

Conversation

@Avasam
Copy link
Copy Markdown
Collaborator

@Avasam Avasam commented Jul 26, 2024

This also avoids touching error re-raising.

By "lazy" I mean that for example in com/win32comext/axdebug/codecontainer.py:

    def GetCodeContextAtPosition(self, charPos):
        # Convert to line number.
        lineNo, offset = self.GetLineOfPosition(charPos)
        charPos = self.GetPositionOfLine(lineNo)
        try:
            cc = self.codeContexts[charPos]
        except KeyError:
            cc = self._MakeContextAtPosition(charPos)
            self.codeContexts[charPos] = cc
        return cc

Can't be simplified to:

    def GetCodeContextAtPosition(self, charPos):
        # Convert to line number.
        lineNo, offset = self.GetLineOfPosition(charPos)
        charPos = self.GetPositionOfLine(lineNo)
        return self.codeContexts.setdefault(charPos, self._MakeContextAtPosition(charPos))

because it would needlessly call self._MakeContextAtPosition(charPos) early

Comment thread com/win32comext/axdebug/codecontainer.py Outdated
@Avasam Avasam merged commit 6fa1b68 into mhammond:main Oct 13, 2024
@Avasam Avasam deleted the simplify-KeyError-fallbacks branch October 13, 2024 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants