[Python-Dev] Path inherits from string

M.-A. Lemburg mal at egenix.com
Thu Jan 26 17:51:48 CET 2006


BJörn Lindqvist wrote:
> This seems to be the only really major issue with the PEP. Everything
> else is negotiable, IMHO. But the string inheritance seem to be such a
> critical issue it deserves its own thread. I have tried to address all
> criticism of it here:

I don't see why this is critical for the success of the Path
object. I agree with Thomas that interfaces should be made
compatible to Path object.

Please note that inheritance from string will cause the C type
checks of the form PyString_Check(obj) to return true.
C code will then assume that it has an object which is
compatible to string C API which instances aren't.

If the C code then uses the C API string macros, you
get segfaults - and lot's of old code does, since there
was no way to inherit from a string type at the time.

In fact, you're lucky that open() doesn't give you segfaults,
since the code used to fetch the string argument does exactly
that...

>...
>
> And there is absolutely nothing that can be done about that. As far as
> I can tell, the string inheritance is either livable with or is a
> showstopper. If it is the latter, then:
> 
>     1. Someone has to make the required modifications to the Python
>        core.

Right.

Plus convert a few PyString_Check()s to PyString_CheckExact()...

>     2. Create a Path class (or adapt the existing one so) that does
>        not inherit from string.
>     3. Release it and wait a few years hoping for it to gain
>        widespread acceptance in the Python community.
>     4. Make a PEP (or adapt this PEP) that gets accepted.
> 
> This scenario makes me sad because it basically means that there will
> never be a Path module in Python, atleast not during my lifetime. :(

Why not ? We've added Unicode support to at least some
file I/O APIs - adding support for instances which
support the string interface shouldn't be all that
difficult :-)

BTW, if you're fine with this API:

class File:
    def __unicode__(self):
        return u"test.txt"

then the required change is minimal: we'd just need to
use PyObject_Unicode() in getargs.c:837 and you should
be set.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 26 2006)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the Python-Dev mailing list