[Python-Dev] Can Python implementations reject semantically invalid expressions?

Craig Citro craigcitro at gmail.com
Fri Jul 2 16:44:08 CEST 2010


> Whoa.  That's very peculiar looking bytecode.  Is dis.dis behaving as
> it should here?
> BTW, I think you want 'raise TypeError', not 'raise TypeError()'.
>

Yep, that's embarrassing. I was being lazy: I was expecting different
bytecodes, and I got it ... so I apparently didn't bother to actually
read the bytecodes? It's interesting -- if I'd just had TypeError
instead of TypeError(), I would have found this out, because "raise
TypeError" is not the bytes representation of a valid sequence of
bytecodes. ;)

Anyway, here's what I was going for:

>>> def foo():
...     return 1+'1'
...
>>> def bar():
...     raise TypeError
...
>>> dis.dis(foo)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 ('1')
              6 BINARY_ADD
              7 RETURN_VALUE
>>> dis.dis(bar)
  2           0 LOAD_GLOBAL              0 (TypeError)
              3 RAISE_VARARGS            1
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE

That said, I totally concede Martin's point -- this is an
implementation-specific thing. It happens that all the major Python
implementations compile to some VM, and I'd bet that these two compile
to different bytecodes on any of them, but that doesn't preclude
another implementation from making a different choice there.

-cc


More information about the Python-Dev mailing list