Why generate POP_TOP after an "import from?"

Adam Preble adam.preble at gmail.com
Sat Apr 18 17:32:47 EDT 2020


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?

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.

> 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...


More information about the Python-list mailing list