CLN: fix python26 invalid exception
State: closed


Labels:

Comments:
found this using my exception finding tool :) there's now a `--validate` option that let's you check for invalid format strings. invalid meaning those strings that have an unequal number of unique format string args to `str.format` args

:+1: 

i'm going to convert many of these to IOError

That makes sense...it's slightly hard to test for these because of the intricacies with network errors. I somewhat wish we could run these functions on pre-existing data (i.e., download them once and not need network connection for all tests), maybe have one test (potentially) that checks for elegant network handling and then that would be it. (maybe mock out the urllib library?)

probably also good to get rid of idioms like this

``` python
try:
    gnarly_function
except ValueError:
    raise
```

any reason to keep those? will just bubble up anyway...unless i'm missing something about exception semantics

yes, that's silly..._unless_ there's a second except statement later, then
it matters...i.e.

try:

```
some func
```

except ValueError:
    raise
except Exception as e:
    handle_other_exception(e)

On Sat, Jun 22, 2013 at 9:19 PM, Phillip Cloud notifications@github.comwrote:

> probably also good to get rid of idioms like this
> 
> try:
>     gnarly_functionexcept ValueError:
>     raise
> 
> any reason to keep those? will just bubble up anyway...unless i'm missing
> something about exception semantics
> 
> —
> Reply to this email directly or view it on GitHubhttps://github.com/pydata/pandas/pull/4000#issuecomment-19867495
> .

yep that would be why it's hard to spot those in my exception-finding script in a general way otherwise i would do it...i suppose you could say "if reraise is in an exception block and there are no other blocks then mark as invalid". i will see how difficult that is

Depending on how you're parsing the python, you can notice the except clause can only follow a try clause, and the next time something is at same level, then you're our of try clause.

i'll probably do this:

```
In [4]: ast.parse('try:\n    print "sdf"\nexcept ValueError:\n    print "yo momma"\nexcept Exception:\n    raise')
Out[4]: <_ast.Module at 0x679f450>

In [5]: mod = ast.parse('try:\n    print "sdf"\nexcept ValueError:\n    print "yo momma"\nexcept Exception:\n    raise')

In [6]: mod.body
Out[6]: [<_ast.TryExcept at 0x679f810>]

In [7]: mod.body[0]
Out[7]: <_ast.TryExcept at 0x679f810>

In [8]: t = mod.body[0]

In [9]: t
Out[9]: <_ast.TryExcept at 0x679f810>

In [10]: t.
t._attributes       t.col_offset        t.__doc__           t.__getattribute__  t.__init__          t.__new__           t.__reduce_ex__     t.__sizeof__        t.__weakref__
t.body              t.__delattr__       t._fields           t.handlers          t.lineno            t.orelse            t.__repr__          t.__str__
t.__class__         t.__dict__          t.__format__        t.__hash__          t.__module__        t.__reduce__        t.__setattr__       t.__subclasshook__

In [10]: t.body
Out[10]: [<_ast.Print at 0x679f850>]

In [11]: t.handlers
Out[11]: [<_ast.ExceptHandler at 0x679f890>, <_ast.ExceptHandler at 0x679fa10>]
```

and use `len(t.handlers)` to detect it