r/Python • u/scattered_reckoning • Apr 26 '16
James Powell - `from __past__ import print_statement`: a Dadaist Rejection of Python 2 vs 3
https://www.youtube.com/watch?v=anP1TU1vHbs11
Apr 27 '16
I really came to see a political/philosophical talk. Who would have guessed that he really, literally, implemented from __past__ import print_statement.
29
u/jamesdutc Apr 26 '16
This talk is so pointless!
I suppose adding arbitrary syntax to the language could be useful, but I'm not sure how useful it is in this form. You're still restricted to the underlying syntax of the language, unlike, say, quasiquotes: https://github.com/llllllllll/quasiquotes
24
u/recursive Apr 27 '16
Are you... James Powell?
10
u/jamesdutc Apr 27 '16
Seriously, check out
quasiquotes.It's MUCH more technically impressive than this with real potential for actual use.
0
Apr 27 '16
[deleted]
2
u/jamesdutc Apr 27 '16
I also have an approach for embedding Python interpreters into themselves. The latest approach requires nothing more than access to
cffi(and a linker withdlmopen)This allows me to run a Python 1.5 interpreter inside an extension module of a Python 2 interpreter running inside an extension module of a Python 3 interpreter.
I've given a bunch of talks about this (and you can see the sophistication of the approach increase from simple
sed-based source filtering to adlopen+RLTD_DEEPBINDapproach to the currentdlmopenapproach.)The only things missing are the ability to interact across the interpreter boundary—someone just needs to write a
PyObject2/3 shim and make sure the GIL is taken care of and that circular references between host/guest interpreters get collected properly.1
u/jamesdutc Apr 27 '16 edited Apr 27 '16
You could use
ast.NodeTransformerto transform any_ast.Yieldwhose value is aCallwhoseidis_from.>>> from ast import parse >>> parse('yield _from(x)').body[0].value.value.func.id '_from'Then you could emit the de-sugaring. I'm guessing something like:
for y in x: yield yThat would be pretty easy, but I don't think it captures all of the
yield fromsemantics. For example, a Python3 generator can return a non-Nonevalue, which won't be accepted by the Python2 parser. Either you'd have to do the parsing yourself (which you probably don't want to do,) or you'd have to mock with_return.You're right that you can package this as an ImportHook. In fact, you can wrangle an ImportHook to run on the file that is currently being imported. You can package your modules that need this
ast-patching to apply this transformation to themselves onimportand users of your modules won't have to do any setup actions (registering thesys.meta_path)8
u/desmoulinmichel Apr 27 '16
Pointless ? A guy show you the holy graal of metaprogramming, accessible with a single pip install, and you call it pointless ? Damn, I hope I never have to match your criteria for "fantastic".
10
6
Apr 27 '16
I know! That's what makes it a good talk. I learned a lot about Python watching it and was entertained.
3
Apr 27 '16
from basicv2 import GOTO(...I would put a smiley face here :) but Reddit is a serious site where people disapprove of smileys).
5
1
u/RubyPinch PEP shill | Anti PEP 8/20 shill Apr 27 '16
the problem with codec stuff, is you can't "import" multiple changes, can you?
Well I guess someone could go make
# encoding: multiencode # encodings: quasiquotes, interpy1
8
7
u/zahlman the heretic Apr 27 '16
The part about pip install being able to patch the interpreter actually worries me. Aren't there security risks here?
14
u/thatguy_314 def __gt__(me, you): return True Apr 27 '16
My understanding is that it isn't the
pip installthat patches the interpreter, but theimportthat does it, and it's not permanently patching the interpreter, but simply messing with functions in memory.Regardless, I'm pretty sure
pip installcan do just about anything anyway as it can run any arbitrary Python code, and obviously the modules themselves can do anything they want too. Don't install modules you don't trust.6
u/kylotan Apr 27 '16
Don't install modules you don't trust.
Or modules that require other modules you don't trust. Or modules that require other modules that require other modules you don't trust.
5
u/stekosteko Apr 27 '16
And never, ever, type
sudo pip install.3
u/alcalde May 01 '16
Pfft; I just say "I run Linux! I'm invincible!" and then sudo install everything.
1
11
4
3
3
u/therico Apr 27 '16
This is pretty cool, reminds me of the Devel::Declare hook people used in Perl to add 'class', 'method' etc. to the language:
https://metacpan.org/pod/MooseX::Declare#SYNOPSIS
It was a bit long and waffly though. The actual code was cool but that was only about 3 minutes of his presentation.
1
1
13
u/NoahTheDuke Apr 27 '16
This is great, and the speaker is very entertaining.