BUG: unclosed file warning when passing invalid encoding
This isn't directly related to #30031 but might solve it if the problem there is unclosed files piling up.

TL;DR: the actual problem is in the stdlib `codecs.open`:

```
def open(filename, mode='r', encoding=None, errors='strict', buffering=1):
    if encoding is not None and 'b' not in mode:
        mode = mode + 'b'
    file = builtins.open(filename, mode, buffering)
    if encoding is None:
        return file
    info = lookup(encoding)
    srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
    # Add attributes to simplify introspection
    srw.encoding = encoding
    return srw
```

When we pass a bogus encoding "foo" in test_format, the `lookup` call raises, and leaves behind the file opened a few lines earlier.