[issue24132] Direct sub-classing of pathlib.Path
qb-cea added the comment: Hi, Thanks for reviving this! Feel free to reuse any code I wrote in my PR (or the whole PR itself), I do not think I will ever get around to finishing this work myself. -- ___ Python tracker <https://bugs.python.org/issue24132> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24132] Direct sub-classing of pathlib.Path
qb-cea <quentin.bou...@cea.fr> added the comment: > What about AbstractPath instead of _PurePath ? I will use this, thanks. -- ___ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue24132> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24132] Direct sub-classing of pathlib.Path
qb-cea <quentin.bou...@cea.fr> added the comment: Hi all, I made a pull request proposing a fix for this issue. There is still quite a lot to be done: - I exposed some variables (and probably methods too) that used to be hidden; - I did not update the documentation; - I did not add a proper test. I will try to fix those by the end of the week. The patch mainly consists of two things: - having Path (resp. PurePath) be a variable pointing at either (Pure)PosixPath or (Pure)WindowsPath, depending on the platform (like Kevin Norris suggested); - introducing two new abstract classes _PurePath and ConcretePath from which PurePosixPath, PureWindowsPath and PosixPath, WindowsPath classes inherit; - removing the _Flavor classes, and redistributing their method to platform-specific classes. Ideally I would like _PurePath to become a public class, but I could not come up with a proper name. Any feedback is more than welcome =] -- ___ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue24132> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24132] Direct sub-classing of pathlib.Path
Change by qb-cea <quentin.bou...@cea.fr>: -- keywords: +patch pull_requests: +5981 stage: -> patch review ___ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue24132> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24132] Direct sub-classing of pathlib.Path
Change by qb-cea <quentin.bou...@cea.fr>: -- nosy: +qb-cea ___ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue24132> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32665] pathlib.Path._from_parsed_parts should call cls.__new__(cls)
qb-cea <quentin.bou...@cea.fr> added the comment: Typo in the code of the previous comment: - __slots__ = ("new_attr",) + __slots__ = ("_new_attr",) -- type: -> behavior ___ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue32665> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32665] pathlib.Path._from_parsed_parts should call cls.__new__(cls)
New submission from qb-cea <quentin.bou...@cea.fr>: Hi, I tried subclassing pathlib.Path and provide it with a new attribute (basically an accessor to an extended attribute). I am rather new to the concept of __slots__ and __new__() but here is how I pictured it should look: from errno import ENODATA from os import getxattr, setxattr from pathlib import Path class Path_(type(Path())): __slots__ = ("new_attr",) def __new__(cls, *args, new_attr=None, **kwargs): self = super().__new__(cls, *args, **kwargs) self._new_attr = new_attr return self @property def new_attr(self): if self._new_attr: return self._new_attr try: new_attr = getxattr(self, "user.new_attr") except OSError as exc: if exc.errno != ENODATA: raise exc else: self._new_attr = new_attr return new_attr new_attr = b"something_dynamic" # for example uuid4().bytes setxattr(self, "user.new_attr", new_attr) self._new_attr = new_attr return new_attr The issue I have is that although my class defines its own __new__() method, it is not always called by the methods of pathlib.Path. For example: >>> Path_("/etc").parent.new_attr Traceback (most recent call last): File "", line 1, in File "/path/to/reproducer.py", line 19, in new_attr if self._new_attr: AttributeError: _new_attr The current workaround I use consists in redefining pathlib.Path's _from_parsed_parts() method in my class: instead of creating a new object using: object.__new__(cls) my implementation uses: cls.__new__(cls) This is the first time I play with the __new__() special method, so it is possible I missed something, if so, sorry for the noise. ------ components: Library (Lib) files: reproducer.py messages: 310671 nosy: pitrou, qb-cea priority: normal severity: normal status: open title: pathlib.Path._from_parsed_parts should call cls.__new__(cls) versions: Python 3.6 Added file: https://bugs.python.org/file47409/reproducer.py ___ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue32665> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com