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

Change exception raised from fileno() (and others) to io.UnsupportedOperation #2276

Closed
metasyn opened this Issue Feb 24, 2017 · 13 comments

Comments

Projects
None yet
4 participants
@metasyn

metasyn commented Feb 24, 2017

In capture, we define a method fileno for the DontReadFromInput:
https://github.com/pytest-dev/pytest/blob/master/_pytest/capture.py#L450

However, python docs suggest this shouldn't be implemented:

file.fileno()

Return the integer “file descriptor” that is used by the underlying implementation to request I/O operations from the operating system. This can be useful for other, lower level interfaces that use file descriptors, such as the fcntl module or os.read() and friends.

Note File-like objects which do not have a real file descriptor should not provide this method!

https://docs.python.org/2/library/stdtypes.html

Is there a particular reason it is provided?

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Feb 25, 2017

@metasyn most likely to get better errors, but at close evaluation it looks a bit like its a bad idea for code thatd does checks - i wonder if there are other users that work that way

i checked io.StringIO.fileno and its implemented, so i believe its a good idea to check what is done, not what is said (and perhaps use a more specific exception

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Feb 25, 2017

Indeed:

Python 3.5.2 | packaged by conda-forge | (default, Jul 26 2016, 02:06:09) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> io.StringIO().fileno()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: fileno
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> io.StringIO().fileno()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: fileno

(Same for 2.6)

I guess to follow suit we should raise io.UnsupportedOperation as well, instead of the current ValueError?

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Feb 25, 2017

Yup

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Feb 25, 2017

For compat we should check/ensure its a valueerror subclass

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Feb 25, 2017

According to the docs:

exception io.UnsupportedOperation
An exception inheriting OSError and ValueError that is raised when an unsupported operation is called on a stream.

So we are all set. 👍

@nicoddemus nicoddemus changed the title from Should fileno be implemented on file-like objects that don't support file descriptors? to Change exception raised from fileno() (and others) to io.UnsupportedOperation Feb 25, 2017

@shubheksha

This comment has been minimized.

shubheksha commented Mar 1, 2017

I'd like to work on this! From what I understood by going through the above discussion is: I need to change the type of exception in fileno method from ValueError to io.UnsupportedOperation. Is that correct?

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Mar 1, 2017

yup, in case of trouble on python2.6 we will assist

@metasyn

This comment has been minimized.

metasyn commented Mar 13, 2017

Sorry slow response here - thanks for your thoughts ya'll. Sounds like a simple change.
@shubheksha are you still interested in doing this? If not, I'd be happy to make a PR.

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Mar 14, 2017

there is no PR so far, so just make one ^^

@metasyn

This comment has been minimized.

metasyn commented Mar 14, 2017

@nicoddemus I summited an initial PR for this but realized you suggested in the title rename "(and others)" - could you let me know what you meant (or what I'm missing?)

Thanks!

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Mar 14, 2017

Hey thanks for the PR!

I was thinking about the other methods in that class which raise exceptions, specially read and buffer (but we must first see if those methods also raise the same exception).

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Mar 14, 2017

A quick look seems like the current behavior of buffer is correct:

>>> from io import StringIO
>>> StringIO().buffer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_io.StringIO' object has no attribute 'buffer'

Not sure if we should change read() though.

nicoddemus added a commit to nicoddemus/pytest that referenced this issue Mar 14, 2017

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Mar 15, 2017

Resolved in #2313, thanks again @metasyn! 👍

This was referenced Mar 6, 2018

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