Comments in ConfigParser module

  • Thread starter Joel Andres Granados
  • Start date
J

Joel Andres Granados

Hi list:

I have run across a situation with ConfigParser Module. It refers to
the comments in the configuration filed. According to the
http://docs.python.org/dev/lib/module-ConfigParser.html it states that
lines starting with "#" and ";" are ignored.
So lines like:
; comment
# comment
are considered comments. So far so good.

The module also allows the comments to appear in the same line as the
"name = value" constructs. The only difference being that this is only
possible with ";" and not with "#" character. I did not see this in the
documentation but this is how it is behaving.
So while the following line takes the comment as a comment.
*name = value ; comment*
the next line does not result in the same behavior.
*name = value # comment*
the name element would have "value # comment" as its value, when what I
really want is for "comment" to be taken as a comment.

QUESTION...So the question is:
Can you use "#" and ";" as comment characters? and if so why does the
"#" not apply for the same situations as the ";"? I'm working with fc7
(I synced it sometime last week) and python 2.5 (r25:51908, Feb 13 2007,
09:13:20)

Any comment greatly appreciated....


Just for reference:
On the RFC 822 <http://www.faqs.org/rfcs/rfc822.html> (a document
referenced in the documentation) there is a mention of ";" being used as
comment character but not necessarily at the beginning of the line.
So anything that comes after the ";" is ignored.
name = value ; comment (";comment" would be ignored)
 
G

Gabriel Genellina

Joel said:
The module also allows the comments to appear in the same line as the
"name = value" constructs. The only difference being that this is only
possible with ";" and not with "#" character. I did not see this in the
documentation but this is how it is behaving.

Yes, it's not documented. There is only a comment in the source code:

# ';' is a comment delimiter only if it
follows
# a spacing character

I think it's either a bug in the code or in the documentation.
QUESTION...So the question is:
Can you use "#" and ";" as comment characters? and if so why does the
"#" not apply for the same situations as the ";"?

If you follow the documentation, comments are ONLY allowed to start a
line.
Just for reference:
On the RFC 822 <http://www.faqs.org/rfcs/rfc822.html> (a document
referenced in the documentation) there is a mention of ";" being used as
comment character but not necessarily at the beginning of the line.

RFC822 uses ";" to include comments in the syntax rules, not for
comments in the actual message headers (parenthesis are used there).
 
J

James Stroud

Joel said:
Very clever.
--
http://mail.python.org/mailman/listinfo/python-list
<http://mail.python.org/mailman/listinfo/python-list>


clever ???
Well I would expect that the ";" character to behave the same way that
the "#", as they are both characters that come before a comment. But it
doesn't. So I'm missing something here. Why is the different behavior
clever?

I was referring to his asking for "comments" in a post about "comments".
I thought this was a clever way to conclude. I have no idea why the
behavior of the module is the way it is. Seems like a bug to me, but I
have not read the official .ini file specification, so I'm not sure its
working correctly.

Cheers,

James
 
G

Gabriel Genellina

En Sun, 08 Apr 2007 06:17:46 -0300, Joel Granados
Yep, IMO its a weirdness in the behavior that is not documented.

It was a bit hard, but I finally found the original Windows ini-file
specification at <http://www.microsoft.com/technet/archive/wfw/2_ch6.mspx>
where you can see ; used as comment delimiter.
ConfigParser tries to handle both RFC822-like headers (with name: value,
including continuation lines, but ignoring almost everything else in the
syntax) and ini files (with ; as comments), plus some unique features like
# comments and "rem" lines removal (?). That is, it tries to cover a lot
of formats without a clear specification.
Add wrong defaults usage, and the need for SafeConfigParser, and some
other gotchas... then switch to use alternative implementations like
ConfigObj <http://www.voidspace.org.uk/python/configobj.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top