This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: marshal.dump cannot write to temporary file
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: tim.golden Nosy List: Gregory.Salvan, lm1, python-dev, tim.golden, vstinner
Priority: normal Keywords:

Created on 2013-11-17 08:09 by lm1, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (9)
msg203125 - (view) Author: Lukasz Mielicki (lm1) Date: 2013-11-17 08:09
marshal.dump yields error when temporary file is given as a second argument.

import tempfile
import marshal
tmpfile = tempfile.TemporaryFile(mode='w+b')
# TypeError: marshal.dump() 2nd arg must be file
marshal.dump({}, tmpfile)
msg203132 - (view) Author: Gregory Salvan (Gregory.Salvan) Date: 2013-11-17 10:36
I can't reproduce this issue (on linux).
Are you sure you've necessary rights to write to tempdir ?
msg203133 - (view) Author: Lukasz Mielicki (lm1) Date: 2013-11-17 10:39
I'm seeing this on Windows with Python 2.7.6 (amd64). I can write to the
same file with other methods.

On 17 November 2013 18:36, Gregory Salvan <report@bugs.python.org> wrote:

>
> Gregory Salvan added the comment:
>
> I can't reproduce this issue (on linux).
> Are you sure you've necessary rights to write to tempdir ?
>
> ----------
> nosy: +Gregory.Salvan
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue19630>
> _______________________________________
>
msg203134 - (view) Author: Gregory Salvan (Gregory.Salvan) Date: 2013-11-17 10:49
Sorry I don't have windows to test.

Try to set the temporary directory to a path you're sure you've rights, either by setting tempfile.tempdir (http://docs.python.org/2/library/tempfile.html#tempfile.tempdir) or by adding the argument dir="C:\\user\path" in tempfile.TemporaryFile function.

Otherwise I can't help you.
Maybe you should show us the result of tempfile.gettempdir().
msg203138 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2013-11-17 10:58
marshal.c does a check that the 2nd arg is a subclass of the builtin file class. On non-Posix platforms, TemporaryFile is a wrapper class providing context manager support for delete-on-close. This fails the subclass test.

The docs for TemporaryFile:

http://docs.python.org/2/library/tempfile.html?highlight=temporaryfile#tempfile.TemporaryFile

do state that, on non-Posix systems, the object is not a true file. The docs for marshal:

http://docs.python.org/2/library/marshal.html?highlight=marshal#marshal.dump

more or less state the 2nd param must be a true file object. So I think we're within rights here. But I accept that it's not an ideal. The best we can opt for here, I think, is a doc patch to marshal.dump reinforcing that the file must a true file.

BTW, why are you marshalling into a file which will be deleted as soon as it's closed?
msg203144 - (view) Author: Lukasz Mielicki (lm1) Date: 2013-11-17 13:20
Thank you for detailed explanation. Too bad tempfile is inherently
non-portable, but I'm fine with marshal.dumps as a w/a in this case.

I marshal to a temporary file to serialize input for Perforce command line
client which is capable of accepting marshaled Python objects as input for
some commands. Unfortunately writing directly to its stdin deadlocks (at
least on Windows, where streams seems not be fully buffered). My guess is
that Perforce client performs seek to end of file before reading it.
msg203226 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-11-17 23:11
I cannot reproduce the issue on Python 3 (on Linux), I suppose that it is specific to Python 2.

On Python 3, mashal.dump(obj, fileobj) only calls fileobj.write(data), no test on fileobj type is done.
msg217521 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-04-29 15:11
New changeset 0f6bdc2b0e38 by Tim Golden in branch '2.7':
Issue #19630 Emphasise that the file parameter to marshal.dump must be a real file object
http://hg.python.org/cpython/rev/0f6bdc2b0e38
msg217522 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2014-04-29 15:12
I updated the docs to emphasise that the file parameter to marshal.dump must be a real file, not a wrapper.
History
Date User Action Args
2022-04-11 14:57:53adminsetgithub: 63829
2014-04-29 15:12:34tim.goldensetstatus: open -> closed
assignee: tim.golden
messages: + msg217522
2014-04-29 15:11:32python-devsetnosy: + python-dev
messages: + msg217521
2013-11-17 23:11:14vstinnersetnosy: + vstinner
messages: + msg203226
2013-11-17 15:33:30tim.goldensetresolution: wont fix
stage: resolved
2013-11-17 13:20:24lm1setmessages: + msg203144
2013-11-17 10:58:36tim.goldensetnosy: + tim.golden
messages: + msg203138
2013-11-17 10:49:57Gregory.Salvansetmessages: + msg203134
2013-11-17 10:39:38lm1setmessages: + msg203133
2013-11-17 10:36:39Gregory.Salvansetnosy: + Gregory.Salvan
messages: + msg203132
2013-11-17 08:09:50lm1create