Skip to content

TST: Use tempfiles in all tests. #5425

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

Merged
merged 2 commits into from
Nov 4, 2013
Merged

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Nov 3, 2013

related #5419

Includes @jreback's commits from #5422 and hdf_temp:

  • TST: make pytables tests go thru a temporary dir and file
  • TST/BUG: incorrect way of testing for r+ modes

TST: fix temporary files by using mktemp (rather than mkstemp) which opens them

Includes @jreback's commits from pandas-dev#5422 and hdf_temp:
* TST: make pytables tests go thru a temporary dir and file
* TST/BUG: incorrect way of testing for r+ modes

TST: fix temporary files by using mktemp (rather than mkstemp) which opens them
@jtratner
Copy link
Contributor

jtratner commented Nov 3, 2013

Why don't we just not use ensure_clean for hdf? Already has a custom
function anyways, could just create temp dir with mktemp in setUp then
remove in tearDown (module level)

@jreback
Copy link
Contributor Author

jreback commented Nov 3, 2013

don't actually use the ensure_clean very much, just a couple of time, it DOES have a custom one (that generates stores).....anyway fixed already

@jreback
Copy link
Contributor Author

jreback commented Nov 4, 2013

@jtratner ok now?

"""
create a temporary file. the caller is responsible for deleting the file
"""
return tempfile.mktemp(suffix=filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

no, you can't do this because it's deprecated, let's keep tempfile.mkstemp() in ensure_clean, and then move this create_temp_file function to pandas/io/tests/test_pytables.py with a note that the insecure version is required because of compatibility issues with hdf.

From the docs for Python 3:

tempfile.mktemp(suffix='', prefix='tmp', dir=None)
Deprecated since version 2.3: Use mkstemp() instead.

Return an absolute pathname of a file that did not exist at the time the call is made. The prefix, suffix, and dir arguments are the same as for mkstemp().

Warning Use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. mktemp() usage can be replaced easily with NamedTemporaryFile(), passing it the delete=False parameter:
>>> f = NamedTemporaryFile(delete=False)
>>> f
<open file '<fdopen>', mode 'w+b' at 0x384698>
>>> f.name
'/var/folders/5q/5qTPn6xq2RaWqk+1Ytw3-U+++TI/-Tmp-/tmpG7V1Y0'
>>> f.write("Hello World!\n")
>>> f.close()
>>> os.unlink(f.name)
>>> os.path.exists(f.name)
False

Not a huge deal for a test suite but we may as well avoid using deprecated/insecure files where we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's just dumb, ok....i'll change it back and use a private one for hdf....

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, somewhat dumb...but principled maybe? :P

Seems like it only fails for pytables, so may as well just special case it.

…y w/o

     opening a named temporary file
TST: named temporary file creation back to mkstemp in utils/testing.py/ensure_clean
@jreback
Copy link
Contributor Author

jreback commented Nov 4, 2013

@jtratner ok?


try:
filename = tempfile.mkstemp(suffix=filename)[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

good call putting this within the try/finally.

@jtratner
Copy link
Contributor

jtratner commented Nov 4, 2013

Yes, nice and principled :)

jreback added a commit that referenced this pull request Nov 4, 2013
TST: Use tempfiles in all tests.
@jreback jreback merged commit c435e72 into pandas-dev:master Nov 4, 2013
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

Successfully merging this pull request may close these issues.

2 participants