BUG: Fixed `tm.set_locale` context manager, it could error and leak when category LC_ALL was used
State: closed


- [x] closes #46595
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Labels:
name: Testing
description: pandas testing functions or related to the test suite
name: Localization
description: Internationalization of data

Comments:
It would be great to add a test reproducing the initial problem, before fixing it.
However it is a bit tricky because we need to execute this test only when we know the locale is available. And of course we can't use `can_set_locale` for this :)
https://github.com/pandas-dev/pandas/issues/46595#issuecomment-1115288925
@jbrockmendel @mroeschke : Please review this very small PR to fix a leakage problem in `set_locale` (#46595). 

When the problem was first fixed, then a few tests started to fail on workers it_IT and zh_CN (see https://github.com/pandas-dev/pandas/actions/runs/2597381464). Looking more closely at the problem, I saw that this was tests marked as `xfail` if the locale is... it_IT or zh_CN :)

https://github.com/pandas-dev/pandas/runs/7152086548?check_suite_focus=true#step:7:54

```
=================================== FAILURES ===================================
_ TestTimeConversionFormats.test_to_datetime_format_time[True-01/10/2010 08:14 PM-%m/%d/%Y %I:%M %p-dt3] _
[gw0] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_ TestTimeConversionFormats.test_to_datetime_format_time[True-01/10/2010 07:40 AM-%m/%d/%Y %I:%M %p-dt4] _
[gw0] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_ TestTimeConversionFormats.test_to_datetime_format_time[True-01/10/2010 09:12:56 AM-%m/%d/%Y %I:%M:%S %p-dt5] _
[gw0] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_ TestTimeConversionFormats.test_to_datetime_format_time[False-01/10/2010 08:14 PM-%m/%d/%Y %I:%M %p-dt3] _
[gw0] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_ TestTimeConversionFormats.test_to_datetime_format_time[False-01/10/2010 07:40 AM-%m/%d/%Y %I:%M %p-dt4] _
[gw0] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_ TestTimeConversionFormats.test_to_datetime_format_time[False-01/10/2010 09:12:56 AM-%m/%d/%Y %I:%M:%S %p-dt5] _
[gw0] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_____________________ TestToTime.test_parsers_time[2:15pm] _____________________
[gw1] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
_____________________ TestToTime.test_parsers_time[0215pm] _____________________
[gw1] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
___________________ TestToTime.test_parsers_time[2:15:00pm] ____________________
[gw1] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
____________________ TestToTime.test_parsers_time[021500pm] ____________________
[gw1] linux -- Python 3.8.13 /home/runner/micromamba/envs/test/bin/python
[XPASS(strict)] fail on a CI build with LC_ALL=zh_CN.utf8/it_IT.utf8
```

This was actually a good thing: it proves that the fix worked ! 

I simply added the `strict=False` flag to the corresponding xfail marks, because currently pytest is run with strict xfail mode.
I also added a few tests, as well as a convenience function `get_current_locale` to ease long-term maintenance if this kind of very hard-to-debug issue.

When this will be merged, we will be able to proceed with PR https://github.com/pandas-dev/pandas/pull/46405 so stay tuned :)
thanks @smarie 
Thanks to both of you for the fast review !