Hi all,

I'm stumbling with an issue where the co_firstlineno behavior changed from
Python 3.9 to Python 3.10 and I was wondering if this was intentional or
not.

i.e.: Whenever a code is compiled in Python 3.10, the `code.co_firstlineno`
is now always 1, whereas previously it was equal to the first statement.

Also, does anyone know if there is any way to restore the old behavior in
Python 3.10? I tried setting the `module.lineno` but it didn't really make
any difference...

As an example, given the code below:

import dis

source = '''
print(1)

print(2)
'''

initial_module = compile(source, '<nofilename>', 'exec', PyCF_ONLY_AST, 1)

import sys
print(sys.version)

for i in range(2):
    module = Module([initial_module.body[i]], [])
    module_code = compile(module, '<no filename>', 'exec')
    print(' --> First lineno:', module_code.co_firstlineno)
    print(' --> Line starts :', list(lineno for offset, lineno in
dis.findlinestarts(module_code)))
    print('---- dis ---')
    dis.dis(module_code)



I have the following outputs for Pyhon 3.9/Python 3.10:

3.9.6 (default, Jul 30 2021, 11:42:22) [MSC v.1916 64 bit (AMD64)]
 --> First lineno: 2
 --> Line starts : [2]
---- dis ---
  2           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 (1)
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE
 --> First lineno: 4
 --> Line starts : [4]
---- dis ---
  4           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 (2)
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE



3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
 --> First lineno: 1
 --> Line starts : [2]
---- dis ---
  2           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 (1)
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE
 --> First lineno: 1
 --> Line starts : [4]
---- dis ---
  4           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 (2)
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

Thanks,

Fabio
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/VXW3TVHVYOMXDQIQBJNZ4BTLXFT4EPQZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to