Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2fd4f37

Browse filesBrowse files
committed
Merged revisions 59212-59225 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r59218 | georg.brandl | 2007-11-29 09:01:20 -0800 (Thu, 29 Nov 2007) | 2 lines Fix reference target. ........ r59219 | georg.brandl | 2007-11-29 09:02:34 -0800 (Thu, 29 Nov 2007) | 4 lines Add examples to the ConfigParser documentation. Credits go to Thomas Lamb, who wrote this as a task in the GHOP contest. ........ r59223 | guido.van.rossum | 2007-11-29 10:25:12 -0800 (Thu, 29 Nov 2007) | 2 lines Fix bug #1517, a segfault in lookdict(). ........ r59224 | georg.brandl | 2007-11-29 10:33:01 -0800 (Thu, 29 Nov 2007) | 2 lines Spaces vs. Tabs. ........
1 parent 5c10664 commit 2fd4f37
Copy full SHA for 2fd4f37

3 files changed

+92Lines changed: 92 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Doc/ACKS.txt‎

Copy file name to clipboardExpand all lines: Doc/ACKS.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ docs@python.org), and we'll be glad to correct the problem.
101101
* Andrew M. Kuchling
102102
* Dave Kuhlman
103103
* Erno Kuusela
104+
* Thomas Lamb
104105
* Detlef Lannert
105106
* Piers Lauder
106107
* Glyph Lefkowitz
Collapse file

‎Doc/library/configparser.rst‎

Copy file name to clipboardExpand all lines: Doc/library/configparser.rst
+87Lines changed: 87 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,90 @@ The :class:`SafeConfigParser` class implements the same extended interface as
333333
otherwise raise :exc:`NoSectionError`. *value* must be a string (:class:`str`
334334
or :class:`unicode`); if not, :exc:`TypeError` is raised.
335335

336+
337+
Examples
338+
--------
339+
340+
An example of writing to a configuration file::
341+
342+
import ConfigParser
343+
344+
config = ConfigParser.RawConfigParser()
345+
346+
# When adding sections or items, add them in the reverse order of
347+
# how you want them to be displayed in the actual file.
348+
# In addition, please note that using RawConfigParser's and the raw
349+
# mode of ConfigParser's respective set functions, you can assign
350+
# non-string values to keys internally, but will receive an error
351+
# when attempting to write to a file or when you get it in non-raw
352+
# mode. SafeConfigParser does not allow such assignments to take place.
353+
config.add_section('Section1')
354+
config.set('Section1', 'int', '15')
355+
config.set('Section1', 'bool', 'true')
356+
config.set('Section1', 'float', '3.1415')
357+
config.set('Section1', 'baz', 'fun')
358+
config.set('Section1', 'bar', 'Python')
359+
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
360+
361+
# Writing our configuration file to 'example.cfg'
362+
with open('example.cfg', 'wb') as configfile:
363+
config.write(configfile)
364+
365+
An example of reading the configuration file again::
366+
367+
import ConfigParser
368+
369+
config = ConfigParser.RawConfigParser()
370+
config.read('example.cfg')
371+
372+
# getfloat() raises an exception if the value is not a float
373+
# getint() and getboolean() also do this for their respective types
374+
float = config.getfloat('Section1', 'float')
375+
int = config.getint('Section1', 'int')
376+
print float + int
377+
378+
# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
379+
# This is because we are using a RawConfigParser().
380+
if config.getboolean('Section1', 'bool'):
381+
print config.get('Section1', 'foo')
382+
383+
To get interpolation, you will need to use a :class:`ConfigParser` or
384+
:class:`SafeConfigParser`::
385+
386+
import ConfigParser
387+
388+
config = ConfigParser.ConfigParser()
389+
config.read('example.cfg')
390+
391+
# Set the third, optional argument of get to 1 if you wish to use raw mode.
392+
print config.get('Section1', 'foo', 0) # -> "Python is fun!"
393+
print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!"
394+
395+
# The optional fourth argument is a dict with members that will take
396+
# precedence in interpolation.
397+
print config.get('Section1', 'foo', 0, {'bar': 'Documentation',
398+
'baz': 'evil'})
399+
400+
Defaults are available in all three types of ConfigParsers. They are used in
401+
interpolation if an option used is not defined elsewhere. ::
402+
403+
import ConfigParser
404+
405+
# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
406+
config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
407+
config.read('example.cfg')
408+
409+
print config.get('Section1', 'foo') # -> "Python is fun!"
410+
config.remove_option('Section1', 'bar')
411+
config.remove_option('Section1', 'baz')
412+
print config.get('Section1', 'foo') # -> "Life is hard!"
413+
414+
The function ``opt_move`` below can be used to move options between sections::
415+
416+
def opt_move(config, section1, section2, option):
417+
try:
418+
config.set(section2, option, config.get(section1, option, 1))
419+
except ConfigParser.NoSectionError:
420+
# Create non-existent section
421+
config.add_section(section2)
422+
opt_move(config, section1, section2, option)
Collapse file

‎Objects/dictobject.c‎

Copy file name to clipboardExpand all lines: Objects/dictobject.c
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ lookdict(PyDictObject *mp, PyObject *key, register long hash)
270270
else {
271271
if (ep->me_hash == hash) {
272272
startkey = ep->me_key;
273+
Py_INCREF(startkey);
273274
cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
275+
Py_DECREF(startkey);
274276
if (cmp < 0)
275277
return NULL;
276278
if (ep0 == mp->ma_table && ep->me_key == startkey) {
@@ -300,7 +302,9 @@ lookdict(PyDictObject *mp, PyObject *key, register long hash)
300302
return ep;
301303
if (ep->me_hash == hash && ep->me_key != dummy) {
302304
startkey = ep->me_key;
305+
Py_INCREF(startkey);
303306
cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
307+
Py_DECREF(startkey);
304308
if (cmp < 0)
305309
return NULL;
306310
if (ep0 == mp->ma_table && ep->me_key == startkey) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.