community icon indicating copy to clipboard operation
community copied to clipboard

'NoneType' has no attribute 'tell' can be avoided by raising FileNotFoundError.

Open Poikilos opened this issue 3 years ago • 1 comments
trafficstars

Software Versions

  • Python: 3.9
  • OS: Linux (Devuan Chimera)
  • Kivy: 2.1.0
  • Kivy installation method: pip

Describe the bug When an image is not found, an obscure error appears: 'NoneType' has no attribute 'tell'

Expected behavior A meaningful exception (probably FileNotFoundError) should be raised before the problem becomes obscure in later code.

To Reproduce A short, runnable example that reproduces the issue with latest kivy master. example1.py in https://github.com/kivy/kivy3

Code and Logs and screenshots kivy_venv/lib/python3.9/site-packages/kivy/core/image/init.py

        filename = resource_find(filename)

Additional context There are several users who have been confused by the error:

  • kivy's fork of kivy3 currently has the problem (diagnosing the simple problem of a missing file was difficult since the error message was unclear; I am the user in this case).
  • https://github.com/viblo/pymunk/issues/166 (It confused more than one user in this case)
  • https://www.forexfactory.com/thread/614471-free-machine-learning-tool-with-a-graphical-user-interface?page=2 (This is not a kivy example but is in imagehdr.py which kivy uses).

To be clear, the error goes away in the example code if a png file named "empty.png" is manually placed in the folder (The fact that it is not there is a kivy3 issue). The kivy issue is merely that the error message is unclear, and the suggested fix provides a better error message.

suggested fix Before the resource_find call, add:

        original_path = str(filename)

After the resource_find call, add:

        if filename is None:
            raise FileNotFoundError(original_path + " was not found.")

Other considerations:

  • If you rather not have a crash when images are missing, maybe use kivy/default.png and simply show an error rather than raise an exception.

Poikilos avatar Mar 13 '22 15:03 Poikilos

I was working with images and got the ‘NoneType’… , I was very confused to say the least. This however brought me clarity. I personally think this is a very good suggestion which can avoid confusion to many people.

Vricken avatar Aug 17 '22 11:08 Vricken

I was going to mark this as easy, but I realised:

  • There is no mention of where the code that is calling tell - why isn't it checking?
  • What about all the other calls to resource_find?
  • resource.py's`resource_find already checks the filename is not Falsy.
  • Why is the filename None when the file doesn't exist?

This may be a bug, but I am not sure the proposed solution is tackling the root cause.

Julian-O avatar Oct 31 '23 13:10 Julian-O

Creating a simple test, I do not see the reported issue with Kivy. Kivy V2.2.1

from kivy.app import App
from kivy.uix.image import Image


class Reproduce7844App(App):
    def build(self):
        image = Image(source='missing_file.png')
        return image


Reproduce7844App().run()

The log contains the following error message: [ERROR ] [Image ] Not found <missing_file.png>

@Poikilos Is there a reproducer available for kivy? Does my example capture the use case you highlighted?

ElliotGarbus avatar Nov 02 '23 04:11 ElliotGarbus

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have the means to take action. Please reach out if you have or find the answers we need so that we can investigate further.

github-actions[bot] avatar Dec 14 '23 12:12 github-actions[bot]