IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.48k stars 287 forks source link

open function doesn't like WindowsPath #1773

Closed programingfrik closed 8 months ago

programingfrik commented 8 months ago

Description

Open a file using a WindowsPath object as its path fails on ironpython3. On cpython3 it works.

Steps to Reproduce

  1. Execute the interpreter "ipy"
  2. Write the next lines of code:
    • import pathlib
    • fichero = pathlib.Path("c:\\some\\existing\\file.txt")
    • fichero.exists() # should say True
    • obf = open(fichero)

Expected behavior:

The obf object should be created. A file object pointing to "c:\some\existing\file.txt". I should be able to use that file object as any other file object to read or write the file.

Writing this same lines of code on cpython3 (version 3.9.10) works as expected produces the obf file object.

Actual behavior:

The last line raises an exeption "TypeError: expected str, bytes or os.PathLike object, not WindowsPath"

To make it work one has to use "open(str(fichero))".

Version Information

$ ipy -VV
IronPython 3.4.1 (3.4.1.1000)
[.NETFramework,Version=v4.6.2 on .NET Framework 4.8.4645.0 (64-bit)]
slozier commented 8 months ago

Thanks for the report.

The issue is that IronPython 3.4 uses the standard library from Python 3.4 and os.PathLike was only added in Python 3.6. This means the current pathlib classes don't implement os.PathLike. Assuming it's not too complicated, might be worth backporting from 3.6 to our version of pathlib...

programingfrik commented 8 months ago

Thanks for your fast response. I appreciate very much this project.