Make a unique filesystem path, without creating the file

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Feb 16 03:35:40 EST 2016


On 16 Feb 2016 05:57, "Ben Finney" <ben+python at benfinney.id.au> wrote:
>
> Cameron Simpson <cs at zip.com.au> writes:
>
> > I've been watching this for a few days, and am struggling to
> > understand your use case.
>
> Yes, you're not alone. This surprises me, which is why I'm persisting.
>
> > Can you elaborate with a concrete example and its purpose which would
> > work with a mktemp-ish official function?
>
> An example::
>
>     import io
>     import tempfile
>     names = tempfile._get_candidate_names()
>
>     def test_frobnicates_configured_spungfile():
>         """ ‘foo’ should frobnicate the configured spungfile. """
>
>         fake_file_path = os.path.join(tempfile.gettempdir(), names.next())
>         fake_file = io.BytesIO("Lorem ipsum, dolor sit
amet".encode("utf-8"))
>
>         patch_builtins_open(
>                 when_accessing_path=fake_file_path,
>                 provide_file=fake_file)
>
>         system_under_test.config.spungfile_path = fake_file_path
>         system_under_test.foo()
>         assert_correctly_frobnicated(fake_file)

If you're going to patch open to return a fake file when asked to open
fake_file_path why do you care whether there is a real file of that name?

--
Oscar



More information about the Python-list mailing list