crusaderky: @hgrecco ready for review and merge
crusaderky: @hgrecco ready again
hgrecco: bors r+
bors[bot]: Merge conflict.
diff --git a/CHANGES b/CHANGES
index 6e67ea0..eabdfd0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -34,6 +34,8 @@ Pint Changelog
 - Fixed a bug where unit exponents were only partially superscripted in HTML format
 - Multiple contexts containing the same redefinition can now be stacked
   (Issue #1108, Thanks Guido Imperiale)
+- Fixed crash when some specific combinations of contexts were enabled
+  (Issue #1112, Thanks Guido Imperiale)
 
 
 0.12 (2020-05-29)
diff --git a/pint/context.py b/pint/context.py
index e1405d5..6a54301 100644
--- a/pint/context.py
+++ b/pint/context.py
@@ -327,8 +327,8 @@ class ContextChain(ChainMap):
 
     @property
     def defaults(self):
-        if self:
-            return next(iter(self.maps[0].values())).defaults
+        for ctx in self.values():
+            return ctx.defaults
         return {}
 
     @property

enable_contexts: RuntimeError: generator raised StopIteration
No clue what's going on - investigating.
Very slight changes to this POC make the issue disappear.

```python
import pint
ureg = pint.UnitRegistry()
ureg.load_definitions(
    """
    @context c1
        [time]->[length] : value * 10 m/s
    @end
    @context c2
        ft = 0.3 m
    @end
    @context c3
        lb = 0.5 kg
    @end
    """.splitlines()
)
ureg.enable_contexts("c1")
ureg.enable_contexts("c2")
ureg.enable_contexts("c3")
```

Output:
```
>       ureg.enable_contexts("c3")

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
lib/python3.8/site-packages/pint/registry.py:1614: in enable_contexts
    if self._active_ctx.defaults:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = ContextChain(<WeakValueDictionary at 0x7fd8366d3f70>, <WeakValueDictionary at 0x7fd8366e8100>)

    @property
    def defaults(self):
        if self:
>           return next(iter(self.maps[0].values())).defaults
E           StopIteration
```
diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py
index 6d1e5d1..855a6b9 100644
--- a/pint/testsuite/test_issues.py
+++ b/pint/testsuite/test_issues.py
@@ -702,6 +702,31 @@ class TestIssues(QuantityTestCase):
                 q = ureg.Quantity(1, "nm")
                 q.to("J")
 
+    def test_issue1112(self):
+        ureg = UnitRegistry(
+            """
+            m = [length]
+            g = [mass]
+            s = [time]
+
+            ft = 0.305 m
+            lb = 454 g
+
+            @context c1
+                [time]->[length] : value * 10 m/s
+            @end
+            @context c2
+                ft = 0.3 m
+            @end
+            @context c3
+                lb = 500 g
+            @end
+            """.splitlines()
+        )
+        ureg.enable_contexts("c1")
+        ureg.enable_contexts("c2")
+        ureg.enable_contexts("c3")
+
 
 if np is not None:
 

pip install -e .
parse_log_pytest
pytest
pytest
apt-get update apt-get install -y gcc
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///croot/attrs_1668696182826/work
certifi @ file:///croot/certifi_1671487769961/work/certifi
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1648562407465/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
packaging @ file:///croot/packaging_1671697413597/work
-e git+https://github.com/hgrecco/pint.git@583b2979f67452f0c5d29399ace07dd98808278e#egg=Pint
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pytest==7.1.2
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
typing_extensions @ file:///croot/typing_extensions_1669924550328/work
zipp @ file:///croot/zipp_1672387121353/work

name: pint
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib-metadata=4.11.3=py37h06a4308_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - typing_extensions=4.4.0=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - pint==0.13.dev97+g583b297
prefix: /opt/conda/envs/pint

pint/testsuite/test_issues.py::TestIssues::test_issue1112
pint/testsuite/test_issues.py::TestIssues::test_alternative_angstrom_definition pint/testsuite/test_issues.py::TestIssues::test_angstrom_creation pint/testsuite/test_issues.py::TestIssues::test_issue1032 pint/testsuite/test_issues.py::TestIssues::test_issue104 pint/testsuite/test_issues.py::TestIssues::test_issue105 pint/testsuite/test_issues.py::TestIssues::test_issue1062_issue1097 pint/testsuite/test_issues.py::TestIssues::test_issue170 pint/testsuite/test_issues.py::TestIssues::test_issue252 pint/testsuite/test_issues.py::TestIssues::test_issue29 pint/testsuite/test_issues.py::TestIssues::test_issue323 pint/testsuite/test_issues.py::TestIssues::test_issue339 pint/testsuite/test_issues.py::TestIssues::test_issue354_356_370 pint/testsuite/test_issues.py::TestIssues::test_issue45 pint/testsuite/test_issues.py::TestIssues::test_issue468 pint/testsuite/test_issues.py::TestIssues::test_issue50 pint/testsuite/test_issues.py::TestIssues::test_issue507 pint/testsuite/test_issues.py::TestIssues::test_issue52 pint/testsuite/test_issues.py::TestIssues::test_issue523 pint/testsuite/test_issues.py::TestIssues::test_issue532 pint/testsuite/test_issues.py::TestIssues::test_issue54 pint/testsuite/test_issues.py::TestIssues::test_issue54_related pint/testsuite/test_issues.py::TestIssues::test_issue61 pint/testsuite/test_issues.py::TestIssues::test_issue61_notNP pint/testsuite/test_issues.py::TestIssues::test_issue62 pint/testsuite/test_issues.py::TestIssues::test_issue625a pint/testsuite/test_issues.py::TestIssues::test_issue625b pint/testsuite/test_issues.py::TestIssues::test_issue625c pint/testsuite/test_issues.py::TestIssues::test_issue655a pint/testsuite/test_issues.py::TestIssues::test_issue655b pint/testsuite/test_issues.py::TestIssues::test_issue66 pint/testsuite/test_issues.py::TestIssues::test_issue66b pint/testsuite/test_issues.py::TestIssues::test_issue69 pint/testsuite/test_issues.py::TestIssues::test_issue783 pint/testsuite/test_issues.py::TestIssues::test_issue85 pint/testsuite/test_issues.py::TestIssues::test_issue856 pint/testsuite/test_issues.py::TestIssues::test_issue856b pint/testsuite/test_issues.py::TestIssues::test_issue86 pint/testsuite/test_issues.py::TestIssues::test_issue876 pint/testsuite/test_issues.py::TestIssues::test_issue902 pint/testsuite/test_issues.py::TestIssues::test_issue912 pint/testsuite/test_issues.py::TestIssues::test_issue93 pint/testsuite/test_issues.py::TestIssues::test_issue932 pint/testsuite/test_issues.py::TestIssues::test_issue960 pint/testsuite/test_issues.py::TestIssues::test_issues86b pint/testsuite/test_issues.py::TestIssues::test_micro_creation
pint/context.py CHANGES
pint/context.py CHANGES