[Python-ideas] Allow manual creation of DirEntry objects

Victor Stinner victor.stinner at gmail.com
Tue Aug 16 19:56:35 EDT 2016


2016-08-17 1:50 GMT+02:00 Guido van Rossum <guido at python.org>:
> We could expose the class with a
> constructor that always fails (the C code could construct instances through
> a backdoor).

Oh, in fact you cannot create an instance of os.DirEntry, it has no
(Python) constructor:

$ ./python
Python 3.6.0a4+ (default:e615718a6455+, Aug 17 2016, 00:12:17)
>>> import os
>>> os.DirEntry(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 'posix.DirEntry' instances

Only os.scandir() can produce such objects.

The question is still if it makes sense to allow to create DirEntry
objects in Python :-)


> Also, what does the scandir package mentioned by the OP use as the
> constructor signature?

The implementation of os.scandir() comes from the scandir package. It
contains the same code, and so has the same behaviour (DirEntry has no
constructor).

Victor


More information about the Python-ideas mailing list