Skip to content

current workaround #4792 for appveyor python 3.5 does not work correctly - #4793

Merged
astrofrog merged 9 commits into
astropy:masterfrom
MSeifert04:io_fits_append_mode
Sep 29, 2016
Merged

astrofrog merged 9 commits into
astropy:masterfrom
MSeifert04:io_fits_append_mode

Conversation

@MSeifert04

Copy link
Copy Markdown
Contributor

I thought #4792 might be a good idea and at least the tests are passing but inside this workaround just hides the error message. So I dug a bit deeper and I found out that the function io.fits.util._is_append_mode_platform was just a workaround because python.io.FitsIO-Instances lost their "a"-mode. This has been fixed for a while and ported to all supported python versions 2.7 and 3.3+

Then I tried some things one could put into io.fits.open and noticed that on my machine the function call was unnecessary for all things I could think of (also including some things I wouldn't have thought of).

So this first commit is a test-case that fails with the "fix" from #4792 . It fails because depending on the built the ctypes.cdll.mscvrt is outdated and contains other constants than the msvcrt that python uses. These constants are hidden since msvc 14 and are unlikely to be exported again.

I'll submit another commit shortly that (hopefilly) shows that the extra-functions are unnecessary since there is no lost "a"-mode anymore and even if a file-handle would lose it it's unlikely to be recovered by the current function.

@MSeifert04 MSeifert04 changed the title proof that the current workaround does not work correctly proof that the current workaround for appveyor python 3.5 does not work correctly Apr 20, 2016
@MSeifert04

Copy link
Copy Markdown
Contributor Author

I've pushed a "fix" and included a test that the extra function calls are unnecessary. Since this might be objectionable I haven't deleted these functions yet but included a test with Warning in case I've missed some input that actually looses his "a" (append) mode.

Since I had problems setting up the tests with file-creation and teardown in the io.fits.tests.test_util.py file I've not included them yet. But I've tested the following cases:

from astropy.io import fits
# Normal fileio-instance opened in append-mode
with open('test.sth', 'ab') as fileobj:
    assert fits.util._is_append_mode_platform(fileobj)

# Strings and urls are passed through
print(fits.util.fileobj_mode('tmp1.fits.gz'))
# None
print(fits.util.fileobj_mode('http://ned.ipac.caltech.edu/dss1B2/By/RMH82_29:I:IIIaJ:dss1.fits'))
# None

# PIL-Images work:
from PIL import Image
fileobj = Image.open('C:/Users/Admin/Pictures/Saved Pictures/355432.jpg')
print(fits.util.fileobj_mode(fileobj))
# rb

# GZIP
import gzip

for num, mode in [(0, 'a'), (0, 'ab'),
                  (0, 'w'), (0, 'wb'),
                  (1, 'x'), (2, 'xb'),
                  (1, 'r'), (2, 'rb')]:
    with gzip.GzipFile('tmp{0}.fits.gz'.format(num), mode) as fileobj:
        print(mode, fits.util.fileobj_mode(fileobj))

import os
os.remove('tmp0.fits.gz')
os.remove('tmp1.fits.gz')
os.remove('tmp2.fits.gz')
# a ab
# ab ab
# w wb
# wb wb
# x xb
# xb xb
# r rb
# rb rb

# Normal file-handles:
for num, mode in [(0, 'ab'),
                  (0, 'wb'),
                  (1, 'xb'),
                  (1, 'rb')]:
    with open('test{0}.dat'.format(num), mode, buffering=0) as f:
        print(mode, fits.util.fileobj_mode(f))

for num, mode in [(0, 'a'), (0, 'ab'),
                  (0, 'w'), (0, 'wb'),
                  (1, 'x'), (2, 'xb'),
                  (1, 'r'), (2, 'rb')]:
    with open('test1{0}.dat'.format(num), mode) as f:
        print(mode, fits.util.fileobj_mode(f))

os.remove('test0.dat')
os.remove('test1.dat')
os.remove('test10.dat')
os.remove('test11.dat')
# ab ab
# wb wb
# xb xb
# rb rb
# a a
# ab ab
# w w
# wb wb
# x x
# xb xb
# r r
# rb rb

And I'm very sorry that the previous PR didn't work out. Maybe it would have worked out but since the calculation was flawed and I always got the result that it's not in append-mode it was certainly not a good PR.

@MSeifert04

Copy link
Copy Markdown
Contributor Author

That's amazing ... the first commit did fail because of an unrelated error. 😄 so much for the "proof"...

@MSeifert04 MSeifert04 changed the title proof that the current workaround for appveyor python 3.5 does not work correctly current workaround #4792 for appveyor python 3.5 does not work correctly Apr 20, 2016
Comment thread astropy/io/fits/tests/test_connect.py Outdated

def test_append_mode_is_determined_correctly(self, tmpdir):
# TODO: Move this test in test_util as soon as I figure out how to
# setup a class with tmpdir...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use temporary directory in astropy/vo/validator/tests/test_validate.py. Something like

import shutil
import tempfile

class TestConeSearchValidation(object):
    def setup_class(self):
        self.out_dir = tempfile.mkdtemp()
    def teardown_class(self):
        shutil.rmtree(self.out_dir)

Is this what you are looking for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks promising. I'll try to setup an own test-class for these test. Thank you.

@MSeifert04

Copy link
Copy Markdown
Contributor Author

I know this will be a very tough review - it took me quite a while to figure out what happens in there - but I would be very happy if anyone would volunteer. I've tried to make the first two comments of this PR as informative as possible but if something seems strange please ask and I'll try to explain.

Comment thread astropy/io/fits/util.py Outdated
# TODO: This is probably superfluous the old corner cases where FILEIO
# objects didn't keep track of their append-flag are over (?)
# At least the bugfix was ported to python 2.7 and 3.3+:
# http://bugs.python.org/issue18876

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for info, we do indeed only support Python 2.7 and 3.3+

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the warning is for cases I might have forgotten (the most common ones: pil, gzip, fileio, hdulist are covered in the tests) that still forget if they are opened in append-mode. These shouldn't exist anymore but better to have some kind of grace-period where users might get this warning.

@pllim

pllim commented May 2, 2016

Copy link
Copy Markdown
Member

@embray ?

@mhvk

mhvk commented May 3, 2016

Copy link
Copy Markdown
Contributor

I looked over the code and it seems OK to me, but I have to admit I don't understand the guts too well. I do think in general it is a good idea to try to simplify rather than leave code in place as we feel we do not understand it well enough to be sure we can remove it. If we are wrong, this is of course unfortunate, but then at least we can have a test case that ensures it keeps being right (and I think the new test cases here are quite exhaustive!).

@MSeifert04

Copy link
Copy Markdown
Contributor Author

@mhvk Thank you for reviewing it. So you would suggest removing the (probably) unnessary functions?

@mhvk

mhvk commented May 4, 2016

Copy link
Copy Markdown
Contributor

So you would suggest removing the (probably) unnecessary functions?

Yes.

@MSeifert04

MSeifert04 commented May 4, 2016 •

Copy link
Copy Markdown
Contributor Author

So you would suggest removing the (probably) unnecessary functions?

Yes

I'm not sure: Should I just go ahead and delete it or wait?

@mhvk

mhvk commented May 4, 2016

Copy link
Copy Markdown
Contributor

Ideally, @astrofrog pipes in too, but I think you added such good testing that I'm comfortable merging it (with -- I hope I won't come to regret it -- the unnecessary functions removed -- I think we should understand for all our code why it is there).

@MSeifert04

MSeifert04 commented May 5, 2016 •

Copy link
Copy Markdown
Contributor Author

Ok if no one disputs your opinion I'll remove the (probably) unnecessary function that manually checks if a file is opened in append mode and do a push sometime this weekend.

I hope you don't mind that I ping you: @embray @astrofrog @eteq

@MSeifert04

Copy link
Copy Markdown
Contributor Author

I think somewhere during this PR I've messed something up or did some dubious merge. I had to force push this commit. Probably because I did some local merges with test-branches.

How do I resolve such problems? Is there a general way?

@mhvk

mhvk commented May 7, 2016

Copy link
Copy Markdown
Contributor

@MSeifert04 - I don't think there is anything wrong with force push. At least, I do them all the time; I think one more or less has to if one rebases on master, or amends a commit.

@MSeifert04

Copy link
Copy Markdown
Contributor Author

@mhvk Okay, I was amending so that explains it. Thank you

Comment thread astropy/io/fits/tests/test_util.py Outdated
test_core.TestConvenienceFunctions.test_fileobj_mode_guessing
but added some low-level tests as well.

TODO: These are regression tests for

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this marked as a TODO?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because first I had no regression test for the workaround fail and made this TODO, then I inserted the regression test but after @mhvk said I should delete the "unnecessary" functions I deleted the test again. Long story short: I'll delete the TODO. I thought I caught all of these in the recent cleanup commit. Thanks

@astrofrog

Copy link
Copy Markdown
Member

Apart from my comment above, I think this looks good.

@embray - would you be able to just confirm that this is ok?

@MSeifert04

Copy link
Copy Markdown
Contributor Author

@astrofrog do I need to write a Changelog? or for that matter will it be part of 1.2?

@MSeifert04

MSeifert04 commented May 20, 2016 •

Copy link
Copy Markdown
Contributor Author

@embray Do you think removing this workaround is ok or could there still be objects which not track their append-flag? I tested the most common ones of FileIO but also gzip, PIL and HDULists. I'm not sure if I should test with StringIO and BytesIO ... could they behave different?

@astrofrog I don't want to disturb you but will this be in 1.2 and does it need a changelog?

@astrofrog

Copy link
Copy Markdown
Member

Sorry that I let this slip through - I think this does need a quick look by @embray or someone else familiar with io.fits before we can merge.

@embray - does this look ok?

@MSeifert04

Copy link
Copy Markdown
Contributor Author

This has been "ready-for-final-review" for a while now. Anyone interested in doing that final review?

@MSeifert04

Copy link
Copy Markdown
Contributor Author

bump

@pllim

pllim commented Sep 1, 2016

Copy link
Copy Markdown
Member

I think we're still waiting for a final nod from @embray

@MSeifert04

Copy link
Copy Markdown
Contributor Author

Should I rebase this now or wait for the nod/review?

@pllim

pllim commented Sep 21, 2016

Copy link
Copy Markdown
Member

Maybe rebase first, in case you need to change something due to conflict?

@MSeifert04

Copy link
Copy Markdown
Contributor Author

was just a trivial change that triggered the conflict.

@MSeifert04

Copy link
Copy Markdown
Contributor Author

@pllim Could you restart the coverage test? It got stuck inside some vo test which is probably unrelated.

On a side note: I could have sworn I was able to restart jobs on Travis last week. 😕

@pllim

pllim commented Sep 21, 2016

Copy link
Copy Markdown
Member

@MSeifert04 , it is restarted. Are you logged into Travis? Sometimes you have to log in again. Otherwise, maybe @astrofrog can check if you still have access.

@astrofrog astrofrog left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been open for a while, and no one has objected. I agree with @mhvk that the testing is extensive enough that I think it's unlikely this will break anything on supported versions of Python. Even in the unlikely case that this produces regressions, we do still have the option of reverting the changes later on, so I think we should go ahead and merge.

@astrofrog
astrofrog merged commit c8080dd into astropy:master Sep 29, 2016
@astrobot

Copy link
Copy Markdown

@astrofrog - thanks for merging this! However, I noticed the following issues with this pull request:

  • The milestone has not been set
  • Changelog entry not present (or pull request number missing) and neither the Affects-dev nor the no-changelog-entry-needed label are set

Would it be possible to fix these? Thanks!

This is an experimental bot being written by @astrofrog - let me know if the message above is incorrect!

@astrofrog

Copy link
Copy Markdown
Member

Thanks @MSeifert04!

@astrofrog astrofrog added this to the v1.3.0 milestone Sep 29, 2016
@MSeifert04
MSeifert04 deleted the io_fits_append_mode branch September 29, 2016 10:56
astrofrog added a commit that referenced this pull request Sep 29, 2016
@astrofrog

Copy link
Copy Markdown
Member

Changelog entry added in f8aa9f8

@MSeifert04

Copy link
Copy Markdown
Contributor Author

👍 🎉

@astrofrog Thanks for adding the changelog entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants