On Sun, Apr 19, 2020 at 7:36 AM Adam Preble <adam.pre...@gmail.com> wrote: > > On Friday, April 17, 2020 at 1:37:18 PM UTC-5, Chris Angelico wrote: > > The level is used for package-relative imports, and will basically be > > the number of leading dots (eg "from ...spam import x" will have a > > level of 3). You're absolutely right with your analysis, with one > > small clarification: > > Thanks for taking that on too. I haven't set up module hierarchy yet so I'm > not in a position to handle levels, but I have started parsing them and > generating the opcodes. Is it sufficient to just use the number of dots as an > indication of level? >
Correct. You can literally just put that exact line of code into a function, disassemble it, and you'll see the level - even if you're not in a package (which would be a run-time error only). > As a side note, I suppose it's sufficient to just *peek* at the stack rather > than pop the module and push it again. I'm guessing that's what the Python > interpreter is doing. > Yep! > > In theory, I suppose, you could replace the POP_TOP with a STORE_FAST > > into "sys", and thus get a two-way import that both grabs the module > > and also grabs something out of it. Not very often wanted, but could > > be done if you fiddle with the bytecode. > > I'm trying to follow along for academic purposes. I'm guessing you mean that > would basically optimize: > > from sys import path > import sys > > It would definitely be a fringe thing to do... Exactly, an incredibly fringe thing to do :) But that's the effect you'd get (at least, I believe so - haven't actually tested that). ChrisA -- https://mail.python.org/mailman/listinfo/python-list