Re: 'generator ignored GeneratorExit''

2012-10-20 Thread MRAB

On 2012-10-20 21:03, Charles Hixson wrote:

If I run the following code in the same module, it works correctly, but
if I import it I get the message:
Exception RuntimeError: 'generator ignored GeneratorExit' in generator
object getNxtFile at 0x7f932f884f50 ignored

def getNxtFile (startDir, exts = [txt, utf8]):
  try:
  forpathingetNxtPath (startDir, exts):
  try:
  fil=open (path, encoding = utf-8-sig)
  yieldfil
  except:
  print (Could not read:  , path)
  exceptGeneratorExit:
  raiseStopIteration

The message appears to be purely informational, but I *would* like to
fix whatever problem it's reporting, and none of the changes that I've
tried have worked.  What *should* I be doing?


That code has a bare except, which is virtually always a bad idea
because it swallows _all_ exceptions. Maybe, in this case, it's
swallowing an exception that would tell you the cause of the problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: 'generator ignored GeneratorExit''

2012-10-20 Thread Ian Kelly
On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
charleshi...@earthlink.net wrote:
 If I run the following code in the same module, it works correctly, but if I
 import it I get the message:
 Exception RuntimeError: 'generator ignored GeneratorExit' in generator
 object getNxtFile at 0x7f932f884f50 ignored

 def getNxtFile (startDir, exts = [txt, utf8]):
 try:
 forpathingetNxtPath (startDir, exts):
 try:
 fil=open (path, encoding = utf-8-sig)
 yieldfil
 except:
 print (Could not read:  , path)
 exceptGeneratorExit:
 raiseStopIteration

 The message appears to be purely informational, but I *would* like to fix
 whatever problem it's reporting, and none of the changes that I've tried
 have worked.  What *should* I be doing?

The bare except is probably catching the GeneratorExit exception and
swallowing it.  Try catching a more specific exception like OSError or
even just Exception instead.

Also, you don't need to explicitly catch GeneratorExit just to raise
StopIteration.  The generator will normally stop on GeneratorExit,
provided the exception is actually able to propagate up.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 'generator ignored GeneratorExit''

2012-10-20 Thread Charles Hixson

On 10/20/2012 04:28 PM, Ian Kelly wrote:

On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
charleshi...@earthlink.net  wrote:

If I run the following code in the same module, it works correctly, but if I
import it I get the message:
Exception RuntimeError: 'generator ignored GeneratorExit' ingenerator
object getNxtFile at 0x7f932f884f50  ignored

def getNxtFile (startDir, exts = [txt, utf8]):
 try:
 forpathingetNxtPath (startDir, exts):
 try:
 fil=open (path, encoding = utf-8-sig)
 yieldfil
 except:
 print (Could not read:  , path)
 exceptGeneratorExit:
 raiseStopIteration

The message appears to be purely informational, but I *would* like to fix
whatever problem it's reporting, and none of the changes that I've tried
have worked.  What *should* I be doing?

The bare except is probably catching the GeneratorExit exception and
swallowing it.  Try catching a more specific exception like OSError or
even just Exception instead.

Also, you don't need to explicitly catch GeneratorExit just to raise
StopIteration.  The generator will normally stop on GeneratorExit,
provided the exception is actually able to propagate up.
Thank you.  That was, indeed the problem.  Removing all the try ... 
excepts made it work on my test case.  Now I've got to figure out what 
to catch in case it's not a utf8 file.  I guess that I'll hope that 
IOError will work, as nothing else sounds reasonable.  It's general 
enough that it ought to work.



--
Charles Hixson

--
http://mail.python.org/mailman/listinfo/python-list


Re: 'generator ignored GeneratorExit''

2012-10-20 Thread Cameron Simpson
On 20Oct2012 16:41, Charles Hixson charleshi...@earthlink.net wrote:
| On 10/20/2012 04:28 PM, Ian Kelly wrote:
|  On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
|   try:
|   fil=open (path, encoding = utf-8-sig)
|   yieldfil
|   except:
[...]
|  The bare except is probably catching the GeneratorExit exception and
|  swallowing it.  Try catching a more specific exception like OSError or
|  even just Exception instead. [...]
|
| Thank you.  That was, indeed the problem.  Removing all the try ... 
| excepts made it work on my test case.  Now I've got to figure out what 
| to catch in case it's not a utf8 file.  I guess that I'll hope that 
| IOError will work, as nothing else sounds reasonable.  It's general 
| enough that it ought to work.

No no no!

Feed it a bad file and _watch_ what exception it throws.

IIRC, it should throw a very specific UnicodeDecodingError; catch that
and only that.

Always try to catch the most specific exception possible or you're back in
the same zone your bare except was in - catching things other than the
specific problem you're handling.

General enough that it ought to work is the wrong approach; Exception
is general enough, and clearly the wrong approach. Catch exactly what
you have to catch. Catching the wrong thing (another exception type) and
_mishandling_ it (because you're treating it like a decoding error) is
not working, it is not working.

If you don't know the exception, let it bubble up and escape - Python
will report it (and abort, of course).

Cheers,
-- 

Performing random acts of moral ambiguity.
- Jeff Miller jxmi...@gonix.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 'generator ignored GeneratorExit''

2012-10-20 Thread Hans Mulder
On 21/10/12 01:41:37, Charles Hixson wrote:
 On 10/20/2012 04:28 PM, Ian Kelly wrote:
 On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
 charleshi...@earthlink.net  wrote:
 If I run the following code in the same module, it works correctly,
 but if I
 import it I get the message:
 Exception RuntimeError: 'generator ignored GeneratorExit' ingenerator
 object getNxtFile at 0x7f932f884f50  ignored

 def getNxtFile (startDir, exts = [txt, utf8]):
  try:
  forpathingetNxtPath (startDir, exts):
  try:
  fil=open (path, encoding = utf-8-sig)
  yieldfil
  except:
  print (Could not read:  , path)
  exceptGeneratorExit:
  raiseStopIteration

 The message appears to be purely informational, but I *would* like to
 fix
 whatever problem it's reporting, and none of the changes that I've tried
 have worked.  What *should* I be doing?
 The bare except is probably catching the GeneratorExit exception and
 swallowing it.  Try catching a more specific exception like OSError or
 even just Exception instead.

 Also, you don't need to explicitly catch GeneratorExit just to raise
 StopIteration.  The generator will normally stop on GeneratorExit,
 provided the exception is actually able to propagate up.
 Thank you.  That was, indeed the problem.  Removing all the try ...
 excepts made it work on my test case.  Now I've got to figure out what
 to catch in case it's not a utf8 file.  I guess that I'll hope that
 IOError will work, as nothing else sounds reasonable.  It's general
 enough that it ought to work.

I would expect a UnicideError, which is a subclass of ValueError,
but not of IOError.  And I'd expect it to be raised when you start
reading the file, not when you open it.

The easiest way to find out this sort of thing, is to not have any
except clauses in your code.  That way, the interpreter will tell
you exactly what was raised, and where.  Much easier than guessing.

 import sys
 fil = open(sys.executable, encoding=utf8)
 fil.readline()
Traceback (most recent call last):
  File stdin, line 1, in module
  File
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/codecs.py,
line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0:
invalid continuation byte

As I expected, opening a non-utf8 file succeeds, but readline() fails.

Hope this helps,

-- HansM


-- 
http://mail.python.org/mailman/listinfo/python-list