TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A function decorator that rewrites the bytecode to enable goto in Python

197 pointsby sea6earover 3 years ago

8 comments

btownover 3 years ago
Along the lines of rewriting bytecode at runtime, <a href="https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0302&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0302&#x2F;</a> lets you arbitrarily hook Python&#x27;s import process and rewrite .pyc files as desired.<p>As a practical application, pytest uses this to take the statement `assert foo == bar` and rewrite the AST into a number of instructions that extract the values of foo and bar and print them if the assertion fails: <a href="http:&#x2F;&#x2F;pybites.blogspot.com&#x2F;2011&#x2F;07&#x2F;behind-scenes-of-pytests-new-assertion.html" rel="nofollow">http:&#x2F;&#x2F;pybites.blogspot.com&#x2F;2011&#x2F;07&#x2F;behind-scenes-of-pytests...</a> (<a href="https:&#x2F;&#x2F;docs.pytest.org&#x2F;en&#x2F;6.2.x&#x2F;assert.html" rel="nofollow">https:&#x2F;&#x2F;docs.pytest.org&#x2F;en&#x2F;6.2.x&#x2F;assert.html</a> for those unfamiliar with the library) - so if you use pytest, you&#x27;re already relying on these arcane techniques!
评论 #28603019 未加载
评论 #28605848 未加载
ufoover 3 years ago
Jesus, what a disgustingly clever hack!<p>I wonder if we could use a similar mechanism to finally add a &quot;continue&quot; to Lua.
评论 #28601457 未加载
评论 #28601237 未加载
评论 #28601379 未加载
JNRoweover 3 years ago
There was an equally intriguing Python goto implementation posted last year¹, with some interesting comments²(including some in the gist itself).<p>This version does seem better, but I&#x27;m not really sure that is a good thing.<p>¹ <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;georgexsh&#x2F;ede5163a294ced53c3e2369ccaa392cc" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;georgexsh&#x2F;ede5163a294ced53c3e2369cca...</a><p>² <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23076859" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23076859</a>
paiuteover 3 years ago
yes! goto is one of the 2 things python got wrong. The other is the ability to deliver end user executables.
评论 #28601087 未加载
评论 #28601072 未加载
评论 #28604069 未加载
评论 #28601011 未加载
评论 #28601332 未加载
评论 #28600970 未加载
noduermeover 3 years ago
Just... out of curiosity as someone who learned to code in BASIC on a TRS-80... <i>WHY ON EARTH</i> would you ever want goto or gosub statements instead of proper loops and methods? It&#x27;s not like they&#x27;re faster. They certainly don&#x27;t make code more readable. The last time I had to use that type of construct was gotoAndStop() in Flash, which at least had the benefit of queueing up a bunch of art and sound effects. Other than the &quot;neat-o&quot; factor, what&#x27;s the use case or impetus behind this apparent horror?
评论 #28602199 未加载
评论 #28602973 未加载
评论 #28603605 未加载
评论 #28603336 未加载
评论 #28602809 未加载
estover 3 years ago
Javascript has a rare yet similar feature called label<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Statements&#x2F;label" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
hamilyon2over 3 years ago
Exceptions are forward gotos. A &quot;while&quot; loop with continue and break is backward goto. The only thing missing is break from nested while. Java has one, called break with label, one of more obscure features of language. So, in a sense, python already does have goto.
评论 #28605960 未加载
评论 #28606090 未加载
评论 #28605726 未加载
anonymoushnover 3 years ago
Great! It&#x27;s too bad that python&#x27;s jump instructions only accept constant targets or constant offests (which seem like they end up being equivalent), otherwise this approach could also support labels-as-values.