This is determined by the device number and i-node number and raises an exception if an os.stat() call on either pathname fails.
Which exception is raised? The other functions in os.path explicitly document this (“Raise ValueError if …”, “Raise OSError if …” etc.)
Trying it out I get: FileNotFoundError: [WinError 2] The system cannot find the file specified: '...'
But since this is not documented I can not rely on that? It could be a different error in some cases?
One might suggest that the answer is in the linked os.stat() documentation. It doesn’t say anything about exceptions there either, but at the top of the os documentation it says:
All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.
So does this also apply to os.path.samefile? It doesn’t seem clear to me.
So the exception should be considered whatever os.stat() would raise, which as you point out isn’t much clearer either. The real trick is what situation is triggering the exception as failures can range. It will probably require reviewing the code to figure out what should be documented for what failure conditions.
As for OSError, that can probably be clarified to “package” (even though os is technically not a package), but once again it would require double-checking quickly if that does hold for os.path (which I would be surprised if it didn’t).
The note on OSError does not seem to hold in general for most os.path functions. Only four are explicitly documented to “Raise OSError if the file does not exist or is inaccessible”:
os.path.getatime(path)
os.path.getmtime(path)
os.path.getctime(path)
os.path.getsize(path)
Of the others, I could only get two to raise an OSError:
os.path.samefile('', '')
os.path.ismount(':::')
Some raise ValueError for some (but not all) invalid paths:
os.path.abspath('\0')
os.path.exists('\0')
os.path.lexists('\0')
os.path.isfile('\0')
os.path.isdir('\0')
os.path.islink('\0')
os.path.realpath('\0')
os.path.relpath(':::')
Others do not seem to raise any exceptions for any of the invalid paths I tried:
Similar exceptions are raised perhaps in hundreds functions. It is impossible to document them all
This is surprising. Should one generally assume any Python standard library function can raise any exception? I.e. try: ... except: ... must be used? I would have assumed the goal is to document all exceptions.
Yes. Some typical exceptions are expected. But there are some exceptions; KeyyboardInterrupted, RuntimeError, MemoryError, maybe more.
Not must. For some unexpected exceptions, default error handler is enough for most cases.
For example, when you writing script, Python will exit with error message including stack trace.
When you writing Web applications, WAF (Web Application Framework) will catch any exception, log the error with stacktrace, and return “503 Internal Server Error” to the web application users.
So you must use try ... except ... in very rare cases.
I would have assumed the goal is to document all exceptions.
I don’t think it’s not pragmatic for Python. There are some Python implementations. There are some implementation specific exceptions.
Even when thinking only CPython, listing all possible exceptions is a very tough task, and it will block Python evolves.
Let’s focus on the “expected exception” instead of the “exceptions to those expected exceptions”.
Can a user of the Python standard library expect assume “expected exceptions” are documented? What are the “expected exceptions” for os.path.samefile? I don’t want to use try: ... except: ..., but I don’t want a “503 Internal Server Error” either. I just want to compare two paths and see if they refer to the same existing file or not.
I suspect OSError should be considered an “expected exception” for os.path.samefile and should be documented. Then I could use try: ... except OSError: .... Am I wrong?
I guess I could use os.path.isfile(a) and os.path.isfile(b) and os.path.samefile(a, b) instead. Although it seems to me that os.path.samefile() should ideally do that for me. But it’s documented to raise instead. It’s just not documented what. One can guess it’s just os.stat()'s exception. But to me it seems somewhat troubling that one can not be sure.
I’m OK to add document about samefile raises OSError.
What can you do other than return 503 when you cannot know two files are same or not by some reasons, like PermissionError?
If you want to handle “file not found” case, you can catch the specific exception. For all other cases, only you can do is return 503. Am I wrong?
I’m not against adding document to samefile. But I just against about this one:
Is your topic is document exception, or changing behavior?
I don’t know translating all OSError into False is good idea. For example, when a and b is same file, but current user doesn’t have permission to access it, I expect PermissonError, not False.
I don’t propose changing any behavior. I would welcome improved documentation. I suspected someone would tell me I misread or missed some section in the current documentation, which explains that OSError is raised.
But I agree: If possible, the standard library should raise PermissionError, not return False. Again, I propose no change of behavior.
But users of the standard library often can and want to handle such errors; as shown above, often the correctpreferred way to handle it may look similar to “ignoring it silently”. Of course this should only be done with a good reason and not blindly.
Since PermissionError is a OSError, it would be enough to document only OSError.
Sorry, I don’t understand “What do you make of~”. I’m not good at English.
If you request me comment about changing it’s behavior; changing behavior is hard decision. Strong reason is required when breaking backward compatibility.
And I don’t have such strong reason. So -1 unless someone shows reason strong enough to break backward compatibility.