Repository: hgrecco/pint

Issue:
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
```

Discussion:
crusaderky: @hgrecco ready for review and merge
crusaderky: @hgrecco ready again
hgrecco: bors r+
bors[bot]: Merge conflict.

Patch:
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


Test patch:
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: