Repository: plone/plone.restapi (language: python)

Issue:
KeyError: 'portal' in tests with Python 3.12.1
Tests with Python 3.12.1 are failing:

```bash
Traceback (most recent call last):
  File "/home/runner/work/plone.restapi/plone.restapi/bin/test", line 289, in <module>
    sys.exit(zope.testrunner.run((['-s', 'plone.restapi', '--auto-color', '--auto-progress']) + [
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/__init__.py", line 31, in run
    failed = run_internal(defaults, args, script_parts=script_parts, cwd=cwd,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/__init__.py", line 55, in run_internal
    runner.run()
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/runner.py", line 191, in run
    self.run_tests()
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/runner.py", line 292, in run_tests
    self.ran += run_layer(self.options, layer_name, layer, tests,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/runner.py", line 473, in run_layer
    return run_tests(options, tests, layer_name, failures, errors, skipped,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/runner.py", line 399, in run_tests
    test(result)
  File "/opt/hostedtoolcache/Python/3.12.1/x64/lib/python3.12/unittest/case.py", line 692, in __call__
    return self.run(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.1/x64/lib/python3.12/unittest/case.py", line 662, in run
    result.stopTest(self)
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/runner.py", line 1019, in stopTest
    self.testTearDown()
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/zope.testrunner-6.1-py3.12.egg/zope/testrunner/runner.py", line 891, in testTearDown
    layer.testTearDown()
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/plone.app.testing-7.0.1-py3.12.egg/plone/app/testing/layers.py", line 291, in testTearDown
    self.tearDownEnvironment(self["portal"])
                             ~~~~^^^^^^^^^^
  File "/home/runner/work/plone.restapi/plone.restapi/eggs/plone.testing-8.0.4-py3.12.egg/plone/testing/layer.py", line 30, in __getitem__
    raise KeyError(key)
KeyError: 'portal'
```

See: https://github.com/plone/plone.restapi/actions/runs/7232945652/job/19707677378

When the tests were run with Python 3.12.0 they worked:

https://github.com/plone/plone.restapi/actions/runs/6999363538/job/19038558131#step:3:13

This appears to be related to: https://github.com/zopefoundation/Zope/issues/1188

I suggest forcing the tests to run with Python 3.12.0, as long as the problem is not fixed.


Discussion / hints:
@Akshat2Jain can you please create a PR for this?
sorry @wesleybl for the late response, I have exams going on at my school! :(
I do not think this has to do with https://github.com/zopefoundation/Zope/issues/1188. (There we see another traceback.)
This is probably another problem with Python 3.12.1.
@icemac you are right. I pinned [zope.testrunner = 6.2.1](https://github.com/plone/plone.restapi/commit/b6a31aa0792496519f2914ab7a915beb486b0ced) and the [same error occurred](https://github.com/plone/plone.restapi/actions/runs/7300511080/job/19895292605). Maybe something needs to be changed in plone.app.testing.

@mauritsvanrees FYI.
@wesleybl It looks like this could happen if there was actually an error while setting up the test, so that `"portal"` never got set as an item on the layer.

Probably that testTearDown method should be a bit more careful. But, will that reveal the original error, or is it swallowed? I'm not sure.
I did some debugging; this is related to https://github.com/python/cpython/issues/113267
Reopening since we worked around the issue rather than solving it
@davisagli while python/cpython#113267 is not fixed, we can make a test [here](https://github.com/plone/plone.app.testing/blob/3dc8a72d846e55db6af72be63f77db0cd5131a37/src/plone/app/testing/layers.py#L291-L292) to make sure the `portal` exists?
@wesleybl I started to do that, but then found that other similar fixes were needed for other layer resources.
@wesleybl The change in Python was [reverted](https://github.com/python/cpython/pull/114994) a few days ago, so I guess it'll be in the next patch release of Python 3.12

Fix patch:
diff --git a/news/1740.internal b/news/1740.internal
new file mode 100644
index 0000000000..5ef67a1aee
--- /dev/null
+++ b/news/1740.internal
@@ -0,0 +1,1 @@
+Use last version of Python 3.12 in tests. https://github.com/python/cpython/issues/113267 has been fixed. @wesleybl


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index d677545c98..ebaa5eaa61 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -18,8 +18,7 @@ jobs:
             plone-version: "6.0"
           - python-version: "3.11"
             plone-version: "6.0"
-          # FIXME: Change to 3.12 when https://github.com/python/cpython/issues/113267 is fixed
-          - python-version: "3.12.0"
+          - python-version: "3.12"
             plone-version: "6.0"
     steps:
       # git checkout