Repository: spyder-ide/spyder

Issue:
Execution of code cells fails for Spyder on python 3.11
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [x] Tried basic troubleshooting (if a bug/error)
    * [ ] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

Starting and running Spyder on python 3.11 works fine mostly. There is an issue though with execution of code cells.

### What steps reproduce the problem?

From the console code is executed normally.
```
In [13]: 1+2
Out[13]: 3
```
Or:
```
In [5]: 1/0
Traceback (most recent call last):

  File "C:\Users\eendebakpt\AppData\Local\Temp\ipykernel_15804\2354412189.py", line 1, in <cell line: 0>
    1/0

ZeroDivisionError: division by zero
```
However, when the code is executed as a code cell the output is
```
runcell(1, 'C:/Users/eendebakpt/untitled0.py')
Traceback (most recent call last):

  File "C:\develop\env311\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 469, in exec_code
    exec_fun(compile(ast_code, filename, 'exec'), ns_globals, ns_locals)

ValueError: AST node line range (3, 1) is not valid
```
The script in the editor was:
```
#%%

1+2

#%%

1/0
```

### What is the expected output? What do you see instead?

Code executed from cells should behave as code entered from the console.

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

Python 3.11.0rc2 64-bit | Qt 5.15.2 | PyQt5 5.15.7 | Windows 10 


### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
# Mandatory:
atomicwrites >=1.2.0          :  1.4.1 (OK)
chardet >=2.0.0               :  5.0.0 (OK)
cloudpickle >=0.5.0           :  2.2.0 (OK)
cookiecutter >=1.6.0          :  2.1.1 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.31.1;<8.0.0       :  7.34.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  0.9.0 (OK)
jsonschema >=3.2.0            :  4.16.0 (OK)
keyring >=17.0.0              :  23.9.3 (OK)
nbconvert >=4.0               :  7.2.1 (OK)
numpydoc >=0.6.0              :  1.5.0 (OK)
paramiko >=2.4.0              :  2.11.0 (OK)
parso >=0.7.0;<0.9.0          :  0.8.3 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.9.2 (OK)
pygments >=2.0                :  2.13.0 (OK)
pylint >=2.5.0;<3.0           :  2.15.4 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.5.0;<1.6.0          :  1.5.0 (OK)
pylsp_black >=1.2.0           :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0     :  3.0.3 (OK)
qstylizer >=0.1.10            :  0.2.2 (OK)
qtawesome >=1.0.2             :  1.1.1 (OK)
qtconsole >=5.3.2;<5.4.0      :  5.3.2 (OK)
qtpy >=2.1.0                  :  2.2.1 (OK)
rtree >=0.9.7                 :  1.0.1 (OK)
setuptools >=49.6.0           :  65.3.0 (OK)
sphinx >=0.6.6                :  5.3.0 (OK)
spyder_kernels >=2.3.3;<2.4.0 :  2.3.3 (OK)
textdistance >=4.2.0          :  4.5.0 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.9 (OK)
zmq >=22.1.0                  :  24.0.1 (OK)

# Optional:
cython >=0.21                 :  0.29.32 (OK)
matplotlib >=3.0.0            :  None (NOK)
numpy >=1.7                   :  1.23.4 (OK)
pandas >=1.1.1                :  None (NOK)
scipy >=0.17.0                :  1.9.2 (OK)
sympy >=0.7.3                 :  None (NOK)

```



Discussion:
Hey @eendebakpt, thanks for reporting. It seems 3.11 is not released yet but we could try to fix this bug before it is.

@impact27, could you take a look at this one?
Fixed by https://github.com/spyder-ide/spyder-kernels/pull/423

Thanks for your help @impact27!

Resolving patch:
diff --git a/external-deps/spyder-kernels/spyder_kernels/customize/utils.py b/external-deps/spyder-kernels/spyder_kernels/customize/utils.py
--- a/external-deps/spyder-kernels/spyder_kernels/customize/utils.py
+++ b/external-deps/spyder-kernels/spyder_kernels/customize/utils.py
@@ -8,6 +8,7 @@
 import ast
 import os
 import re
+import sys
 import sysconfig
 
 
@@ -113,6 +114,14 @@ def capture_last_Expr(code_ast, out_varname):
         # Fix line number and column offset
         assign_node.lineno = expr_node.lineno
         assign_node.col_offset = expr_node.col_offset
+        if sys.version_info[:2] >= (3, 8):
+            # Exists from 3.8, necessary from 3.11
+            assign_node.end_lineno = expr_node.end_lineno
+            if assign_node.lineno == assign_node.end_lineno:
+                # Add 'globals()[{}] = ' and remove 'None'
+                assign_node.end_col_offset += expr_node.end_col_offset - 4
+            else:
+                assign_node.end_col_offset = expr_node.end_col_offset
         code_ast.body[-1] = assign_node
     return code_ast, capture_last_expression
 


Test patch: