`etree.parse` should support `pathlib.Path` objects

Bug #1820837 reported by Allan Lewis
16
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_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)
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)

description: updated
description: updated
Revision history for this message
scoder (scoder) wrote :

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.

Changed in lxml:
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.