prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Handle pathlib paths with `get_file_obj`
  #### Is your feature request related to a problem? Please describe
  I'm trying to read a statsmodels.tsa.statespace.sarimax.SARIMAXResults pickle file I've saved. I'd like to pass a pathlib.Path object as I find it easier to handle (I work with different machines, Mac and Windows).
  
  #### Describe the solution you'd like
  The function `open` already supports pathlib objects. Why not using it?
  
  You could replace `statsmodels.iolib.openfile.get_file_obj` with:
  
  ```python
  if _is_string_like(fname):
      return _open(fname, mode, encoding)
  try:  # try opening it with 'open'
      return open(fname, mode, encoding)
  except TypeError:  # else, maybe return context manager (please check if it's always TypeError)
      try:
          # Make sure the object has the write methods
          if 'r' in mode:
              fname.read
          if 'w' in mode or 'a' in mode:
              fname.write
      except AttributeError:
          raise ValueError('fname must be a string or a file-like object')
      return EmptyContextManager(fname)
  ```
  
  This is just a suggestion though :)
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `statsmodels/iolib/openfile.py`
  Content:
  ```
  1 """
  2 Handle file opening for read/write
  3 """
  4 from numpy.lib._iotools import _is_string_like
  5 
  6 
  7 class EmptyContextManager(object):
  8     """
  9     This class is needed to allow file-like object to be used as
  10     context manager, but without getting closed.
  11     """
  12     def __init__(self, obj):
  13         self._obj = obj
  14 
  15     def __enter__(self):
  16         '''When entering, return the embedded object'''
  17         return self._obj
  18 
  19     def __exit__(self, *args):
  20         '''Do not hide anything'''
  21         return False
  22 
  23     def __getattr__(self, name):
  24         return getattr(self._obj, name)
  25 
  26 
  27 def _open(fname, mode, encoding):
  28     if fname.endswith('.gz'):
  29         import gzip
  30         return gzip.open(fname, mode, encoding=encoding)
  31     else:
  32         return open(fname, mode, encoding=encoding)
  33 
  34 
  35 def get_file_obj(fname, mode='r', encoding=None):
  36     """
  37     Light wrapper to handle strings and let files (anything else) pass through.
  38 
  39     It also handle '.gz' files.
  40 
  41     Parameters
  42     ----------
  43     fname : str or file-like object
  44         File to open / forward
  45     mode : str
  46         Argument passed to the 'open' or 'gzip.open' function
  47     encoding : str
  48         For Python 3 only, specify the encoding of the file
  49 
  50     Returns
  51     -------
  52     A file-like object that is always a context-manager. If the `fname` was
  53     already a file-like object, the returned context manager *will not
  54     close the file*.
  55     """
  56     if _is_string_like(fname):
  57         return _open(fname, mode, encoding)
  58     try:
  59         # Make sure the object has the write methods
  60         if 'r' in mode:
  61             fname.read
  62         if 'w' in mode or 'a' in mode:
  63             fname.write
  64     except AttributeError:
  65         raise ValueError('fname must be a string or a file-like object')
  66     return EmptyContextManager(fname)
  67
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/statsmodels/iolib/openfile.py b/statsmodels/iolib/openfile.py
  --- a/statsmodels/iolib/openfile.py
  +++ b/statsmodels/iolib/openfile.py
  @@ -34,13 +34,14 @@
   
   def get_file_obj(fname, mode='r', encoding=None):
       """
  -    Light wrapper to handle strings and let files (anything else) pass through.
  +    Light wrapper to handle strings, path objects and let files (anything else)
  +    pass through.
   
       It also handle '.gz' files.
   
       Parameters
       ----------
  -    fname : str or file-like object
  +    fname : str, path object or file-like object
           File to open / forward
       mode : str
           Argument passed to the 'open' or 'gzip.open' function
  @@ -56,11 +57,14 @@
       if _is_string_like(fname):
           return _open(fname, mode, encoding)
       try:
  -        # Make sure the object has the write methods
  -        if 'r' in mode:
  -            fname.read
  -        if 'w' in mode or 'a' in mode:
  -            fname.write
  -    except AttributeError:
  -        raise ValueError('fname must be a string or a file-like object')
  -    return EmptyContextManager(fname)
  +        return open(fname, mode, encoding=encoding)  # handle pathlib-like objs
  +    except TypeError:
  +        try:
  +            # Make sure the object has the write methods
  +            if "r" in mode:
  +                fname.read
  +            if "w" in mode or "a" in mode:
  +                fname.write
  +        except AttributeError:
  +            raise ValueError("fname must be a string or a file-like object")
  +        return EmptyContextManager(fname)
  
verification_info: {"golden_diff": "diff --git a/statsmodels/iolib/openfile.py b/statsmodels/iolib/openfile.py\n--- a/statsmodels/iolib/openfile.py\n+++ b/statsmodels/iolib/openfile.py\n@@ -34,13 +34,14 @@\n \n def get_file_obj(fname, mode='r', encoding=None):\n     \"\"\"\n-    Light wrapper to handle strings and let files (anything else) pass through.\n+    Light wrapper to handle strings, path objects and let files (anything else)\n+    pass through.\n \n     It also handle '.gz' files.\n \n     Parameters\n     ----------\n-    fname : str or file-like object\n+    fname : str, path object or file-like object\n         File to open / forward\n     mode : str\n         Argument passed to the 'open' or 'gzip.open' function\n@@ -56,11 +57,14 @@\n     if _is_string_like(fname):\n         return _open(fname, mode, encoding)\n     try:\n-        # Make sure the object has the write methods\n-        if 'r' in mode:\n-            fname.read\n-        if 'w' in mode or 'a' in mode:\n-            fname.write\n-    except AttributeError:\n-        raise ValueError('fname must be a string or a file-like object')\n-    return EmptyContextManager(fname)\n+        return open(fname, mode, encoding=encoding)  # handle pathlib-like objs\n+    except TypeError:\n+        try:\n+            # Make sure the object has the write methods\n+            if \"r\" in mode:\n+                fname.read\n+            if \"w\" in mode or \"a\" in mode:\n+                fname.write\n+        except AttributeError:\n+            raise ValueError(\"fname must be a string or a file-like object\")\n+        return EmptyContextManager(fname)\n", "issue": "Handle pathlib paths with `get_file_obj`\n#### Is your feature request related to a problem? Please describe\r\nI'm trying to read a statsmodels.tsa.statespace.sarimax.SARIMAXResults pickle file I've saved. I'd like to pass a pathlib.Path object as I find it easier to handle (I work with different machines, Mac and Windows).\r\n\r\n#### Describe the solution you'd like\r\nThe function `open` already supports pathlib objects. Why not using it?\r\n\r\nYou could replace `statsmodels.iolib.openfile.get_file_obj` with:\r\n\r\n```python\r\nif _is_string_like(fname):\r\n    return _open(fname, mode, encoding)\r\ntry:  # try opening it with 'open'\r\n    return open(fname, mode, encoding)\r\nexcept TypeError:  # else, maybe return context manager (please check if it's always TypeError)\r\n    try:\r\n        # Make sure the object has the write methods\r\n        if 'r' in mode:\r\n            fname.read\r\n        if 'w' in mode or 'a' in mode:\r\n            fname.write\r\n    except AttributeError:\r\n        raise ValueError('fname must be a string or a file-like object')\r\n    return EmptyContextManager(fname)\r\n```\r\n\r\nThis is just a suggestion though :)\n", "before_files": [{"content": "\"\"\"\nHandle file opening for read/write\n\"\"\"\nfrom numpy.lib._iotools import _is_string_like\n\n\nclass EmptyContextManager(object):\n    \"\"\"\n    This class is needed to allow file-like object to be used as\n    context manager, but without getting closed.\n    \"\"\"\n    def __init__(self, obj):\n        self._obj = obj\n\n    def __enter__(self):\n        '''When entering, return the embedded object'''\n        return self._obj\n\n    def __exit__(self, *args):\n        '''Do not hide anything'''\n        return False\n\n    def __getattr__(self, name):\n        return getattr(self._obj, name)\n\n\ndef _open(fname, mode, encoding):\n    if fname.endswith('.gz'):\n        import gzip\n        return gzip.open(fname, mode, encoding=encoding)\n    else:\n        return open(fname, mode, encoding=encoding)\n\n\ndef get_file_obj(fname, mode='r', encoding=None):\n    \"\"\"\n    Light wrapper to handle strings and let files (anything else) pass through.\n\n    It also handle '.gz' files.\n\n    Parameters\n    ----------\n    fname : str or file-like object\n        File to open / forward\n    mode : str\n        Argument passed to the 'open' or 'gzip.open' function\n    encoding : str\n        For Python 3 only, specify the encoding of the file\n\n    Returns\n    -------\n    A file-like object that is always a context-manager. If the `fname` was\n    already a file-like object, the returned context manager *will not\n    close the file*.\n    \"\"\"\n    if _is_string_like(fname):\n        return _open(fname, mode, encoding)\n    try:\n        # Make sure the object has the write methods\n        if 'r' in mode:\n            fname.read\n        if 'w' in mode or 'a' in mode:\n            fname.write\n    except AttributeError:\n        raise ValueError('fname must be a string or a file-like object')\n    return EmptyContextManager(fname)\n", "path": "statsmodels/iolib/openfile.py"}], "after_files": [{"content": "\"\"\"\nHandle file opening for read/write\n\"\"\"\nfrom numpy.lib._iotools import _is_string_like\n\n\nclass EmptyContextManager(object):\n    \"\"\"\n    This class is needed to allow file-like object to be used as\n    context manager, but without getting closed.\n    \"\"\"\n    def __init__(self, obj):\n        self._obj = obj\n\n    def __enter__(self):\n        '''When entering, return the embedded object'''\n        return self._obj\n\n    def __exit__(self, *args):\n        '''Do not hide anything'''\n        return False\n\n    def __getattr__(self, name):\n        return getattr(self._obj, name)\n\n\ndef _open(fname, mode, encoding):\n    if fname.endswith('.gz'):\n        import gzip\n        return gzip.open(fname, mode, encoding=encoding)\n    else:\n        return open(fname, mode, encoding=encoding)\n\n\ndef get_file_obj(fname, mode='r', encoding=None):\n    \"\"\"\n    Light wrapper to handle strings, path objects and let files (anything else)\n    pass through.\n\n    It also handle '.gz' files.\n\n    Parameters\n    ----------\n    fname : str, path object or file-like object\n        File to open / forward\n    mode : str\n        Argument passed to the 'open' or 'gzip.open' function\n    encoding : str\n        For Python 3 only, specify the encoding of the file\n\n    Returns\n    -------\n    A file-like object that is always a context-manager. If the `fname` was\n    already a file-like object, the returned context manager *will not\n    close the file*.\n    \"\"\"\n    if _is_string_like(fname):\n        return _open(fname, mode, encoding)\n    try:\n        return open(fname, mode, encoding=encoding)  # handle pathlib-like objs\n    except TypeError:\n        try:\n            # Make sure the object has the write methods\n            if \"r\" in mode:\n                fname.read\n            if \"w\" in mode or \"a\" in mode:\n                fname.write\n        except AttributeError:\n            raise ValueError(\"fname must be a string or a file-like object\")\n        return EmptyContextManager(fname)\n", "path": "statsmodels/iolib/openfile.py"}]}