[Mesa-dev] [PATCH v5 1/5] genxml: New generated header genX_bits.h (v5)

Dylan Baker dylan at pnwbakers.com
Tue Mar 28 17:25:35 UTC 2017


Quoting Chad Versace (2017-03-28 09:37:56)
> On Sat 25 Mar 2017, Dylan Baker wrote:
> > 
> > This is just a suggestion, feel free to ignore
> > 
> > I forgot to do something here, but this is one case where using
> > argparse.FileType might actually be the right thing to do.
> > 
> > it would like like:
> > p.add_argument('-o', '--output', type=argparse.FileType('wb'), default=sys.stdout, ...)
> > 
> > Then you could drop the hunk above and the 'with open(...)' in the main
> > function. It's also more portable.
> 
> I just tried the suggestion, and it behaves badly IMO for output files.  It's
> probably fine for input files. The problem is that ArgumentParser creates the
> file as soon as it parses the -o option, even when there is a usage error or
> the --help option is given.
> 
> Example 1:
>   $ ./gen_bits_header.py -o bad-output1 --bad-option
>   usage: gen_bits_header.py [-h] [-o OUTPUT] [--cpp-guard CPP_GUARD]
>                           XML_SOURCE [XML_SOURCE ...]
>   gen_bits_header.py: error: too few arguments
>   $ echo $?
>   2
>   $ ls
>   ... bad-output1
> 
> Example 2:
>   $ ./gen_bits_header.py -o bad-output2 --help
>   usage: gen_bits_header.py [-h] [-o OUTPUT] [--cpp-guard CPP_GUARD]
>                           XML_SOURCE [XML_SOURCE ...]
>   $ echo $?
>   0
>   $ ls
>   ... bad-output2

Yup, there are some painful corners to argparse.FileType that make me almost
never use it, which is why it was just a suggestion :)

Dylan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20170328/47c13035/attachment.sig>


More information about the mesa-dev mailing list