0

Is there any way to get Python's interpreter to compile any Python code into a NOP instruction?

(I'm specifically talking about obtaining bytecode via compiling Python code, not generating it directly.)

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    Could you give some context on why you want to do this, and how much you care about things like where the NOP goes and what code is around it? – user2357112 Jun 05 '17 at 23:34
  • By "any code", do you mean even code that would ordinarily compile to instructions that do something, or do you just want to find some code that produces a NOP? – user2357112 Jun 05 '17 at 23:36
  • @user2357112: Originally I was hoping to control what's around the NOP (it comes in handy when making a bytecode template so I don't have to mess with jump offsets manually) but given how I couldn't find *any* way to generate a NOP, at this point I'm just trying to see if it's even possible to generate it. So if you know a way to generate it with arbitrary code around it, all the better, but otherwise any way to generate it would do. – user541686 Jun 05 '17 at 23:42
  • Looking through the compiler internals, I'm not seeing a way to generate a NOP. It looks like they only arise temporarily in the peephole optimizer, and the last step of peephole optimization removes NOPs. – user2357112 Jun 05 '17 at 23:46
  • @user2357112: Ahh I was afraid that might be the case, okay thanks :( – user541686 Jun 05 '17 at 23:51
  • @user2357112: Feel free to post that as an answer! – user541686 Jun 05 '17 at 23:51

1 Answers1

2

This appears to be impossible. NOP opcodes are only generated by the peephole optimizer, but the last step of peephole optimization removes all NOPs and retargets jumps for the new instruction indices.

In fact, barring bugs, this seems likely to have been impossible in every Python version ever released. In Python 2.3, there was no NOP opcode, and in Python 2.4, the peephole optimizer already removed all NOPs it generated.

user2357112
  • 260,549
  • 28
  • 431
  • 505