Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary files cause issue under Windows 7 #25

Open
phaefele opened this issue Oct 9, 2015 · 1 comment
Open

Temporary files cause issue under Windows 7 #25

phaefele opened this issue Oct 9, 2015 · 1 comment

Comments

@phaefele
Copy link

phaefele commented Oct 9, 2015

Seems that NamedTemporaryFiles under Windows are opened automatically. This causes issues when codec.open is then used to open the file in the IPython V3 code. I changed the code to use the following method as opposed to codecs_open in the save and get methods.

Am happy to integrate it in if need be.

Thanks for this work.

Regards
Paul

     def _codecs_open(self, filename, file_handle, mode='rb', encoding=None, errors='strict', buffering=1):
        # Copy of codecs.open
        if encoding is not None:
            if 'U' in mode:
                # No automatic conversion of '\n' is done on reading and writing
                mode = mode.strip().replace('U', '')
                if mode[:1] not in set('rwa'):
                    mode = 'r' + mode
            if 'b' not in mode:
                # Force opening of the file in binary mode
                mode = mode + 'b'
        try:
            file = open(filename, mode, buffering)
        except:
            file = file_handle
        if encoding is None:
            return file
        info = codecs.lookup(encoding)
        srw = codecs.StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
        # Add attributes to simplify introspection
        srw.encoding = encoding
        return srw

Save was changed as follows (get the same)

 def _save_notebook(self, path, nb):
        self.log.debug('_save_notebook: %s', locals())

        k = boto.s3.key.Key(self.bucket)
        k.key = self._path_to_s3_key(path)

        try:
            with tempfile.NamedTemporaryFile() as t:
                with self._codecs_open(t.name, t, mode='w', encoding='utf-8') as f:
                    # write tempfile with utf-8 encoding
                    nbformat.write(nb, f, version=nbformat.NO_CONVERT)
                    # upload as bytes (t's fp didn't advance)
                    t.seek(0)
                    k.set_contents_from_file(t)
        except Exception as e:
            raise web.HTTPError(400, u"Unexpected Error Writing Notebook: %s %s" % (path, e))
@graphaelli
Copy link

That seems like strange behavior but https://bugs.python.org/issue14243 and others suggest that some special handling is indeed necessary on windows. Can you please submit a pull request with code that works on windows and we can iterate from there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants