traverseda
Anyone looking to do this kind of thing, the macropy library makes it a fair bit easier: https://macropy3.readthedocs.io/en/latest/

You really do need to be modifying the AST and not just applying a regex to the source code text. This can be a challenge if you want to modify python syntax as you first need to get an AST you can modify, personally I recommend starting with the LARK library and modifying the python syntax grammer it includes.

https://lark-parser.readthedocs.io/en/latest/examples/advanc...

I use similar techniques to transpile openscad code into a python AST in my "pySdfScad" project, while still theoretically getting the benefits of fancy tracebacks and debugging and the like. Probably should have gone with a simple parser instead, but what can you do.

I think they should have stopped at the "cursed way" and not the "truly cursed way", if they really wanted the syntax changes than having your own python parser like the LARK implementation I mention above is a must.

albertzeyer
I don't understand the reasoning why the transformation directly on the source code is better than on the AST level?

Because you can use coding and then it's somewhat automatic?

But you can do sth similar on AST level, namely installing a meta path finder, i.e. an import module hook which does the AST transformation automatically (https://docs.python.org/3/library/sys.html#sys.meta_path).

Then, there is also the new frame evaluation API (https://peps.python.org/pep-0523/), which allows you to dynamically rewrite bytecode. This has been used by PyTorch 2.0 for torch.compile.

chriskw
I did something similar to option 3 to make the builtin numeric types "callable", since the dunder __call__ methods can't be overwritten for builtins. For example, in regular arithmetic notation, something like 6(7+8) could be read as 6*(7+8), but trying to do this in Python gives you `SyntaxWarning: 'int' object is not callable;`, since you're essentially trying to do a function call on the literal 6. The workaround was to use a custom codec to wrap all integer literals to give them the expected call behavior.

Repo if anyone is interested: https://github.com/ckw017/blursed

This was inspired by a way less silly usecase, future f-strings, which added f-string support to older versions of Python in a similar way using codecs: https://github.com/asottile-archive/future-fstrings

benrutter
There really is nothing greater than an completely idiotic idea implemented with pure genius - this was immense reading and I learnt a lot!
hsfzxjy
I've attempted something more crazy -- to make Python support multi-line lambda expression [1].

It's done with AST manipulation as well, but on a larger scale and completer functionalities.

[1] https://github.com/hsfzxjy/lambdex

mjburgess
So it seems you can register a source transformer with python which will trigger when a `# coding: ...` header is used (to signal character encoding).

This seems like an interesting avenue for static analysis tools, code generators, (etc.) to explore. Is it a viable approach?

I'd be interested in some analysis on this as a mechanism: performance, maintability, etc. wise.

The "dont do this" argument writes itself; nevertheless, its worth exploring.

ur-whale
Didn't know about codecs ...

The idea of using (abusing in fact) codecs to pre-process python code before it gets to the actual python interpreter is just fantastic!

I'm starting to think of all the terrible things I'm going to be able to do with this to work around things that have annoyed me for years in python.

[EDIT]: I'm thinking one can probably plug the C preprocessor in python now ... oh the sheer joy :D

Mockapapella
This is the kind of shit I love about python. You can put this in a pre processor script so that it works for every python program you run
___kaukas___
A long time ago I stumbled upon (and contributed to) https://github.com/delfick/nose-of-yeti. The most delightful bit of cursed Python I’ve seen!
Awelton
There is something to be said for doing something just because it's ridiculous. It's terrible and I love it.
hot_gril
"It is recognized that you have a funny sense of fun."
atorodius
Thanks for this wild ride, it went way further than I anticipated ..
Too
It’s a bit sad that with-blocks can’t yield more than once. It would be very convenient to write retry code this way.
MathMonkeyMan
This is evil, and I love it.

I wonder if the codec could use python's lexer (assuming it's exposed) to parse the for loops and nothing else. Then replace the loops with a placeholder, and then replace the placeholder in the AST after a parse. Might be cleaner than source->source transform by the codec, maybe not.

kristianp
Where did the "cursed" adjective start being used like this? It's not used in my country, and I've only seen it being used in the last couple of years as part of subreddit titles.
akkartik
Just from the description I think this doesn't handle continue.
Too
Codecs free to transform all code before it’s executed? Sounds like the perfect place for hackers to hide RCEs very difficult to spot. Just hide an innocent comment at the top of a file.
wheelerof4te
So it's true that you can do anything with Python. Even if you shouldn't.
StunxFS
Very good!
winterqt
(2022)
travisgriggs
Two things:

1. I wish more HN posts ended in "for fun" -- so much of what makes being a progammer fun and enjoyable is plumbing the depths of what is possible, not because it's "best practice" or whatever. I think these types of forrays are where true mastery comes from.

2. The less reserved keywords a language has, the better. It makes things like this easier. Years ago I figured out how to implement Goto in Smalltalk "for fun". It was easier because Smalltalk had less reserved keywords.

epr
Some people have a lot of time on their hands.

  (defmacro cfor [initialize condition advance #* body]
   `(do ~initialize
        (while ~condition (do [email protected] ~advance))))

  (cfor (setv i 0) (< i 10) (+= i 1)
    (print i))
This runs on the python vm with hy right now. Took about 2 mins to have it up and running after noticing this post on hn. Couldn't pass it up.
masklinn
> Or alternatively: How I made the most cursed Python package of all time.

Beg your pardon, http://entrian.com/goto/ exists.

mixmastamyk
The range builtin already gives 98% of the functionality, just need to put "i in range" before it.
bb88
I say this with love: He needs a girlfriend (or life partner)
sr.ht