Thanks for reading. I was surprised to see that flask.Config.from_envvar raises an exception if the file does not exist even when the silent keyword argument is True. Does anyone else find this surprising? >>> import flask, os >>> app = flask.Flask(__name__) >>> app.config.from_pyfile('does not exist', silent=True) False >>> app.config.from_envvar('CONFIG_NAME', silent=True) False >>> os.environ['CONFIG_NAME'] = 'does not exist' >>> app.config.from_envvar('CONFIG_NAME', silent=True) Traceback (most recent call last): ... IOError: [Errno 2] Unable to load configuration file (No such file or directory):... I was expecting silent=True to force from_envvar to swallow the exception. This seems odd to me since I have to wrap my call to from_envvar in a "look before you leap" block or replicate the try:except and if condition buried in flask.Configuration.from_pyfile. Should I submit a patch to pass the silent keyword argument through from_envvar to from_pyfile? - dave --
Hi, On Tue, Feb 28, 2012 at 2:57 PM, David Shawley <daveshawley@gmail.com> wrote: > Thanks for reading. > > I was surprised to see that flask.Config.from_envvar raises an > exception if the file does not exist even when the silent keyword > argument is True. Does anyone else find this surprising? > >>>> import flask, os >>>> app = flask.Flask(__name__) >>>> app.config.from_pyfile('does not exist', silent=True) > False >>>> app.config.from_envvar('CONFIG_NAME', silent=True) > False >>>> os.environ['CONFIG_NAME'] = 'does not exist' >>>> app.config.from_envvar('CONFIG_NAME', silent=True) > Traceback (most recent call last): > ... > IOError: [Errno 2] Unable to load configuration file (No such file or > directory):... > > I was expecting silent=True to force from_envvar to swallow the > exception. This seems odd to me since I have to wrap my call to > from_envvar in a "look before you leap" block or replicate the > try:except and if condition buried in flask.Configuration.from_pyfile. > Should I submit a patch to pass the silent keyword argument through > from_envvar to from_pyfile? This is a bug. The documented behavior of silent=True does not hold, "set to `True` if you want silent failure for missing files." The fix is easy, but I'd like to add a test to make sure it doesn't regress. Could you file a bug? https://github.com/mitsuhiko/flask/issues Thanks, Ron PS - Related, but not a duplicate: https://github.com/mitsuhiko/flask/issues/198
On Wed, Feb 29, 2012 at 2:21 PM, Ron DuPlain <ron.duplain@gmail.com> wrote: > On Tue, Feb 28, 2012 at 2:57 PM, David Shawley <daveshawley@gmail.com> wrote: >> Thanks for reading. >> >> I was surprised to see that flask.Config.from_envvar raises an >> exception if the file does not exist even when the silent keyword >> argument is True. Does anyone else find this surprising? >> >>>>> import flask, os >>>>> app = flask.Flask(__name__) >>>>> app.config.from_pyfile('does not exist', silent=True) >> False >>>>> app.config.from_envvar('CONFIG_NAME', silent=True) >> False >>>>> os.environ['CONFIG_NAME'] = 'does not exist' >>>>> app.config.from_envvar('CONFIG_NAME', silent=True) >> Traceback (most recent call last): >> ... >> IOError: [Errno 2] Unable to load configuration file (No such file or >> directory):... >> >> I was expecting silent=True to force from_envvar to swallow the >> exception. This seems odd to me since I have to wrap my call to >> from_envvar in a "look before you leap" block or replicate the >> try:except and if condition buried in flask.Configuration.from_pyfile. >> Should I submit a patch to pass the silent keyword argument through >> from_envvar to from_pyfile? > > This is a bug. The documented behavior of silent=True does not hold, > "set to `True` if you want silent failure for missing files." The fix > is easy, but I'd like to add a test to make sure it doesn't regress. > > Could you file a bug? > https://github.com/mitsuhiko/flask/issues > > Thanks, > > Ron > > > PS - Related, but not a duplicate: > https://github.com/mitsuhiko/flask/issues/198 Thank you Dave Shawley for writing the test and fix: https://github.com/mitsuhiko/flask/pull/415 -Ron
On Tue, Feb 28, 2012 at 15:27, David Shawley <daveshawley@gmail.com> wrote: > I was surprised to see that flask.Config.from_envvar raises an > exception if the file does not exist even when the silent keyword > argument is True. Does anyone else find this surprising? > Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. *Explicit is better than implicit.* ... -- Juancarlo *Añez*
Maybe read a little further? ;) The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. .. Errors should never pass silently. Unless explicitly silenced. ----- Original Message ----- From: "Juancarlo Añez" <juancarlo.anez@gmail.com> To: flask@librelist.com Sent: Tuesday, February 28, 2012 4:11:42 PM Subject: Re: [flask] flask.Config.from_envvar raises when file doesn't exist? On Tue, Feb 28, 2012 at 16:31, Gregg Lind < gregg.lind@gmail.com > wrote: Passing "silent=True" sounds like an 'explicit' programmer action. Maybe the OP wants a wrapper like "config_from_everywhere" that would wrap a bit of the mess? Silent is usually the opposite of verbose. In this case it means to me "no writing to the logs". In the case of a missing configuration file, going on silently could be the source of very difficult-to-debug problems. If the application can function with no configuration, then the explicit thing to do is to check if the configuration file exists, and not to call "configure" if it doesn't. -- Juancarlo Añez
On Tue, Feb 28, 2012 at 16:49, Evan Tangman <etangman@goodegg.com> wrote: > Errors should never pass silently. > Unless explicitly silenced. > Exactly. -- Juancarlo *Añez*
On Feb 28, 2012, at 4:29 PM, "Juancarlo Añez" <juancarlo.anez@gmail.com> wrote: On Tue, Feb 28, 2012 at 16:49, Evan Tangman <etangman@goodegg.com> wrote: > Errors should never pass silently. > Unless explicitly silenced. > Exactly. -- Juancarlo *Añez* So "silent=True" is not "explicitly silenced"? My problem is that I expected that calling from_envvar with silent=True would be similar to calling from_pyfile with silent=True which it is not. I ended up taking the "look before you leap" approach since it isn't as coupled as catching the IOError that would gets ignored inside of from_pyfile. - dave
On Wed, Feb 29, 2012 at 14:32, David Shawley <daveshawley@gmail.com> wrote: > So "silent=True" is not "explicitly silenced"? My problem is that I > expected that calling from_envvar with silent=True would be similar to > calling from_pyfile with silent=True which it is not. I ended up taking > the "look before you leap" approach since it isn't as coupled as catching > the IOError that would gets ignored inside of from_pyfile. That is inconsistent, and Ron found it is not the documented behavior. It's a bug, and I've wasted your time... -- Juancarlo *Añez*
Passing "silent=True" sounds like an 'explicit' programmer action. Maybe the OP wants a wrapper like "config_from_everywhere" that would wrap a bit of the mess? On Tue, Feb 28, 2012 at 2:56 PM, Juancarlo Añez <juancarlo.anez@gmail.com> wrote: > > > On Tue, Feb 28, 2012 at 15:27, David Shawley <daveshawley@gmail.com> wrote: >> >> I was surprised to see that flask.Config.from_envvar raises an >> exception if the file does not exist even when the silent keyword >> argument is True. Does anyone else find this surprising? > > > Python 2.7.2+ (default, Oct 4 2011, 20:06:09) > [GCC 4.6.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import this > The Zen of Python, by Tim Peters > > Beautiful is better than ugly. > Explicit is better than implicit. > ... > > > > -- > Juancarlo Añez >
On Tue, Feb 28, 2012 at 16:31, Gregg Lind <gregg.lind@gmail.com> wrote: > Passing "silent=True" sounds like an 'explicit' programmer action. > > Maybe the OP wants a wrapper like "config_from_everywhere" that would > wrap a bit of the mess? > Silent is usually the opposite of verbose. In this case it means to me "no writing to the logs". In the case of a missing configuration file, going on silently could be the source of very difficult-to-debug problems. If the application can function with no configuration, then the explicit thing to do is to check if the configuration file exists, and not to call "configure" if it doesn't. -- Juancarlo *Añez*