`etree.parse` should support `pathlib.Path` objects
Bug #1820837 reported by
Allan Lewis
This bug affects 3 people
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Confirmed
|
Undecided
|
Unassigned |
Bug Description
If I pass a `pathlib.Path` object to `etree.parse`, it fails with:
TypeError: cannot parse from 'PosixPath'
It should be simple to add support for `pathlib.Path` objects here.
Python : sys.version_
lxml.etree : (4, 3, 0, 0)
libxml used : (2, 9, 9)
libxml compiled : (2, 9, 9)
libxslt used : (1, 1, 33)
libxslt compiled : (1, 1, 33)
Right. PR welcome.
This affects not only etree.parse() but also etree.iterparse(), XSLT(), XMLSchema(), etc. I think this is best implemented in the _encodeFilename() helper function in apihelpers.pxi, with adaptations wherever it's used since it's usually preceded by a type test using _isString(). There should probably be a new type check function that detects path objects in Python versions that support it.
Tests belong into the corresponding test files, e.g. test_etree.py, test_xslt.py, etc.