Python 2.6 reports: os.rename () or os.renames () OSError, but the file name is None

If you invoke the os.renameprovision of files or directories that do not exist OSError, which is raised, skips the file name by setting it to None. Is this a bug in version 2.6 fixed in a later version?

You can reproduce the problem by simply doing:

python -c 'import os ; os.rename("/tmp/abc", "/tmp/cba")'

Where does not exist /tmp/abcand /tmp/cba.

I'm just wondering if I should implement the os.rename shell to catch OSError and fix the file name attribute before sending the error again.

Update

I implemented a simple test shell that creates the desired behavior:

$ /tmp/osrename.py
Traceback (most recent call last):
File "/tmp/osrename.py", line 26, in <module>
  os.rename('/tmp/abc', '/tmp/cba')
File "/tmp/osrename.py", line 8, in __os_rename
  os_rename(a, b)
OSError: [Errno 2] No such file or directory: '/tmp/abc'

Here is the implementation:

import os, sys

def __os_rename_wrapper(os_rename):
    def __os_rename(a, b):
        try:
            os_rename(a, b)

        except OSError:
            exc = sys.exc_info()[1]

            if getattr(exc, 'filename', None) is None:
                exc.filename = "{0} -> {1}".format(repr(a), repr(b))

            raise

    __os_rename.__name__ = os_rename.__name__
    __os_rename.__doc__ = os_rename.__doc__

    return __os_rename

os.rename = __os_rename_wrapper(os.rename)


os.rename('/tmp/abc', '/tmp/cba')

Is there a way to enable module loading so that these types of fixes can be applied dynamically?

+4
1

python 2.7.8, , .

:

python 3.4 filename2 OSError, @Random832, .

PEP 3151 , :

WindowsError OSError, winerror Windows. None , errno, strerror (, OSError Python).

:

, , (, errno, filename , , winerror).

PEP 3.3. , 3.3 :

, (, open() os.unlink()), , , , .

, , , , filename .

- PEP, .

, , , , , ( ?).

, , , .

+1

All Articles