Pull requests

#1 Declined
Repository
rubik rubik
Branch
default
Repository
pitrou pitrou
Branch
default

Porting to Python 2 (alpha)

Author
  1. Michele Lacchia
Reviewers
Description

Here is a first patch that tries to make pathlib Python 2 compatible (and still working on Python 3).

There are still a few problems: I had to manually disable test_as_bytes_common, because unittest.skipIf(sys.version_info < (3, 2), 'reason') didn't work; tempfile.TemporaryDirectory has been introduced in version 3.2, and tempfile.mkdtemp isn't exactly the same (cleanup missing). I didn't replaced it, for now this test fails; * Comparison between PureNTPath and PurePosixPath fails (it should raise TypeError, while it does not). This is due to this line:

{{{ if self._flavour is not other._flavour: return NotImplemented }}}

On Python 3.2 it evaluates to True, while on Python 2.7 it evaluates to False. I don't know the reason.

I notice now that on line 593 it uses os.fsencode, which has been introduced in Python 3.2, I'll fix that in the next patch.

Comments (7)

  1. Antoine Pitrou repo owner

    Hi,

    Thanks for the patch.

    I had to manually disable `test_as_bytes_common`, because `unittest.skipIf(sys.version_info < (3, 2), 'reason')` didn't work;

    AFAIK, skipIf must be used as a decorator:

    @unittest.skipIf(...)
    def test_foobar(self):
        ...
    

    tempfile.TemporaryDirectory has been introduced in version 3.2, and tempfile.mkdtemp isn't exactly the same (cleanup missing). I didn't replaced it, for now this test fails;

    It's only test_resolve_common, right? You can change it to use mkdtemp, and call `self.addCleanup(rmtree, tempdir)` (roughly).

    Comparison between PureNTPath and PurePosixPath fails (it should raise TypeError, while it does not)

    It would be nice if you could investigate a bit more.

    1. Michele Lacchia author

      I used skipIf as a decorator, and it didn't do the work... I don't know why. I'll replace TemporaryDirectory as you said, and I'll run tests again.

      As for the comparison I'll investigate more.

      1. Michele Lacchia author

        I finally got it! In Python 2 when a rich comparison method returns NotImplemented TypeError is not raised. On the other hand, this happens on Python 3:

        ## Python 2
        
        >>> class A(object):
             def __le__(self, other):
                 return NotImplemented    
        
        >>> class B(object):
             def __ge__(self, other):
                 return NotImplemented   
        
        >>> a, b = A(), B()
        >>> a<b
        True
        
        ## Python 3
        
        >>> class A:
        ...     def __le__(self, other):
        ...             return NotImplemented
        ... 
        >>> class B:
        ...     def __ge__(self, other):
        ...             return NotImplemented
        ... 
        >>> a,b = A(), B()
        >>> 
        >>> a < b
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: unorderable types: A() < B()
        
          1. Antoine Pitrou repo owner

            NotImplemented allows the comparison to be made on the ``other`` object. That said, most types are orderable in 2.x, so perhaps the test should be simply skipped

            2.7:

            >>> 0 < "a"
            True
            

            3.2:

            >>> 0 < "a"
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            TypeError: unorderable types: int() < str()