I have been working on a side project for which I’m using bpython, a “fancy interface to the Python interpreter.” If you use the Python REPL often, you should check it out. It offers unique features like in-line syntax highlighting, readline-like autocomplete, a “rewind” function to pop the last line of code from memory, auto-indentation and more.

Anyway, today I found a bug in bpython, and that’s that Python’s `decimal.getcontext()`

does not work with it.

```
bpython version 0.24 on top of Python 3.11.3
>>> import decimal
>>> decimal.getcontext().prec = 6
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal('0.1428571428571428571428571429')
```

If you run the same lines in the standard Python REPL, what you get instead is:

```
bpython version 0.24 on top of Python 3.11.3
>>> import decimal
>>> decimal.getcontext().prec = 6
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal('0.142857')
```

Further experimenting revealed that, as a workaround, setting `DefaultContext`

works as expected:

```
bpython version 0.24 on top of Python 3.11.3
>>> decimal.DefaultContext.prec = 6
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal('0.142857')
```

I suspect this has something to do with threads, as `decimal.getcontext`

targets the current thread while
`DefaultContext`

is global. I went to the bpython repository only to find that a ticket was already opened in 2021. I
added my `DefaultContext`

observation there.

*Subscribe to the newsletter, the RSS feed, or follow me on Mastodon*