cpython icon indicating copy to clipboard operation
cpython copied to clipboard

`dis.dis` on empty classes does nothing

Open Kenny2github opened this issue 3 years ago • 3 comments

Bug report

Expected behavior

dis.dis on an empty class should output a message along the lines of "Nothing to disassemble", or possibly raise an exception in that regard

Actual behavior

dis.dis produces no output when passed a class with no __dict__ members.

Repro case

Test code pasteable in interactive session:

if 1:
 import sys
 from dis import dis
 class Empty: pass
 print('Python', sys.version)
 dis(Empty)

Output: My local machine:

Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]

Tutorialspoint:

Python 3.8.6 (default, Jan 29 2021, 17:38:16) 
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]

JDoodle:

Python 3.9.9 (main, Nov 20 2021, 21:30:06) 
[GCC 11.1.0]

Pyodide:

Python 3.10.2 (main, Aug  9 2022, 13:42:49) [Clang 15.0.0 (https://github.com/llvm/llvm-project 7effcbda49ba32991b8955821b8f

Your environment

  • CPython versions tested on: See output above
  • Operating system and architecture: See output above

Kenny2github avatar Aug 12 '22 21:08 Kenny2github

What do you want it to print for an empty class?

Compare with the output for a non-empty class:

>>> class C: 
...   def f(): return 12
... 
>>> dis.dis(C)
Disassembly of f:
  2           0 RESUME                   0
              2 LOAD_CONST               1 (12)
              4 RETURN_VALUE

iritkatriel avatar Aug 14 '22 11:08 iritkatriel

I guess we could have a "Disassembly of C:" for the topmost class/module as well:

>>> class C:
...   class D:
...     def f(): return 12
... 
>>> dis.dis(C)
Disassembly of D:
Disassembly of f:
  3           0 RESUME                   0
              2 LOAD_CONST               1 (12)
              4 RETURN_VALUE

iritkatriel avatar Aug 14 '22 12:08 iritkatriel

Yeah, that seems right to me. Something like

>>> import dis
>>> class C:
...   class D:
...     def f(): pass
... 
>>> dis.dis(C)
Disassembly of C:
Disassembly of D:
Disassembly of f:
  3           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE

>>> class B: pass
>>> dis.dis(B)
Disassembly of B:
>>> 

Kenny2github avatar Aug 15 '22 07:08 Kenny2github