Kaydet (Commit) 2e7840fe authored tarafından Guido van Rossum's avatar Guido van Rossum

The usual.

üst 01b7ced8
...@@ -24,34 +24,37 @@ ConfigParser -- responsible for for parsing a list of ...@@ -24,34 +24,37 @@ ConfigParser -- responsible for for parsing a list of
methods: methods:
__init__(defaults=None) -- create the parser and specify a __init__(defaults=None)
dictionary of intrinsic defaults. The create the parser and specify a dictionary of intrinsic defaults. The
keys must be strings, the values must keys must be strings, the values must be appropriate for %()s string
be appropriate for %()s string interpolation. Note that `__name__' is always an intrinsic default;
interpolation. Note that `__name__' is it's value is the section's name.
always an intrinsic default; it's value
is the section's name.
sections() -- return all the configuration section names, sans DEFAULT sections()
return all the configuration section names, sans DEFAULT
options(section) -- return list of configuration options for the named options(section)
section return list of configuration options for the named section
read(*filenames) -- read and parse the list of named configuration files read(filenames)
read and parse the list of named configuration files
get(section, option, raw=0) -- return a string value for the named get(section, option, raw=0, vars=None)
option. All % interpolations are return a string value for the named option. All % interpolations are
expanded in the return values, based on expanded in the return values, based on the defaults passed into the
the defaults passed into the constructor constructor and the DEFAULT section. Additional substitutions may be
and the DEFAULT section. provided using the `vars' argument, which must be a dictionary whose
contents override any pre-existing defaults.
getint(section, options) -- like get(), but convert value to an integer getint(section, options)
like get(), but convert value to an integer
getfloat(section, options) -- like get(), but convert value to a float getfloat(section, options)
like get(), but convert value to a float
getboolean(section, options) -- like get(), but convert value to getboolean(section, options)
a boolean (currently defined as 0 like get(), but convert value to a boolean (currently defined as 0 or
or 1, only) 1, only)
""" """
import sys import sys
...@@ -173,12 +176,14 @@ class ConfigParser: ...@@ -173,12 +176,14 @@ class ConfigParser:
except IOError: except IOError:
pass pass
def get(self, section, option, raw=0): def get(self, section, option, raw=0, vars=None):
"""Get an option value for a given section. """Get an option value for a given section.
All % interpolations are expanded in the return values, based All % interpolations are expanded in the return values, based on the
on the defaults passed into the constructor, unless the optional defaults passed into the constructor, unless the optional argument
argument `raw' is true. `raw' is true. Additional substitutions may be provided using the
`vars' argument, which must be a dictionary whose contents overrides
any pre-existing defaults.
The section DEFAULT is special. The section DEFAULT is special.
""" """
...@@ -191,6 +196,9 @@ class ConfigParser: ...@@ -191,6 +196,9 @@ class ConfigParser:
raise NoSectionError(section) raise NoSectionError(section)
d = self.__defaults.copy() d = self.__defaults.copy()
d.update(sectdict) d.update(sectdict)
# Update with the entry specific variables
if vars:
d.update(vars)
option = string.lower(option) option = string.lower(option)
try: try:
rawval = d[option] rawval = d[option]
...@@ -199,11 +207,17 @@ class ConfigParser: ...@@ -199,11 +207,17 @@ class ConfigParser:
# do the string interpolation # do the string interpolation
if raw: if raw:
return rawval return rawval
try:
return rawval % d
except KeyError, key:
raise InterpolationError(key, option, section, rawval)
value = rawval # Make it a pretty variable name
while 1: # Loop through this until it's done
if not string.find(value, "%("):
try:
value = value % d
except KeyError, key:
raise InterpolationError(key, option, section, rawval)
else:
return value
def __get(self, section, conv, option): def __get(self, section, conv, option):
return conv(self.get(section, option)) return conv(self.get(section, option))
......
...@@ -367,3 +367,10 @@ def normpath(path): ...@@ -367,3 +367,10 @@ def normpath(path):
if not comps and not slashes: if not comps and not slashes:
comps.append('.') comps.append('.')
return slashes + string.joinfields(comps, '/') return slashes + string.joinfields(comps, '/')
# Return an absolute path.
def abspath(path):
if not isabs(path):
path = join(os.getcwd(), path)
return normpath(path)
...@@ -5,9 +5,14 @@ ...@@ -5,9 +5,14 @@
try: try:
class Empty(Exception): class Empty(Exception):
pass pass
class Full(Exception):
pass
except TypeError: except TypeError:
# string based exceptions # string based exceptions
Empty = 'Queue.Empty' # Exception raised by get_nowait() # exception raised by get(block=0)/get_nowait()
Empty = 'Queue.Empty'
# exception raised by put(block=0)/put_nowait()
Full = 'Queue.Full'
class Queue: class Queue:
def __init__(self, maxsize): def __init__(self, maxsize):
...@@ -23,32 +28,38 @@ class Queue: ...@@ -23,32 +28,38 @@ class Queue:
self.fsema = thread.allocate_lock() self.fsema = thread.allocate_lock()
def qsize(self): def qsize(self):
"""Returns the approximate size of the queue (not reliable!).""" """Return the approximate size of the queue (not reliable!)."""
self.mutex.acquire() self.mutex.acquire()
n = self._qsize() n = self._qsize()
self.mutex.release() self.mutex.release()
return n return n
def empty(self): def empty(self):
"""Returns 1 if the queue is empty, 0 otherwise (not reliable!).""" """Return 1 if the queue is empty, 0 otherwise (not reliable!)."""
self.mutex.acquire() self.mutex.acquire()
n = self._empty() n = self._empty()
self.mutex.release() self.mutex.release()
return n return n
def full(self): def full(self):
"""Returns 1 if the queue is full, 0 otherwise (not reliable!).""" """Return 1 if the queue is full, 0 otherwise (not reliable!)."""
self.mutex.acquire() self.mutex.acquire()
n = self._full() n = self._full()
self.mutex.release() self.mutex.release()
return n return n
def put(self, item): def put(self, item, block=1):
"""Put an item into the queue. """Put an item into the queue.
If the queue is full, block until a free slot is avaiable. If optional arg 'block' is 1 (the default), block if
""" necessary until a free slot is available. Otherwise (block
self.fsema.acquire() is 0), put an item on the queue if a free slot is immediately
available, else raise the Full exception.
"""
if block:
self.fsema.acquire()
elif not self.fsema.acquire(0):
raise Full
self.mutex.acquire() self.mutex.acquire()
was_empty = self._empty() was_empty = self._empty()
self._put(item) self._put(item)
...@@ -58,45 +69,27 @@ class Queue: ...@@ -58,45 +69,27 @@ class Queue:
self.fsema.release() self.fsema.release()
self.mutex.release() self.mutex.release()
def get(self): def put_nowait(self, item):
"""Gets and returns an item from the queue. """Put an item into the queue without blocking.
This method blocks if necessary until an item is available. Only enqueue the item if a free slot is immediately available.
Otherwise raise the Full exception.
""" """
self.esema.acquire() return self.put(item, 0)
self.mutex.acquire()
was_full = self._full()
item = self._get()
if was_full:
self.fsema.release()
if not self._empty():
self.esema.release()
self.mutex.release()
return item
# Get an item from the queue if one is immediately available, def get(self, block=1):
# raise Empty if the queue is empty or temporarily unavailable """Remove and return an item from the queue.
def get_nowait(self):
"""Gets and returns an item from the queue.
Only gets an item if one is immediately available, Otherwise If optional arg 'block' is 1 (the default), block if
this raises the Empty exception if the queue is empty or necessary until an item is available. Otherwise (block is 0),
temporarily unavailable. return an item if one is immediately available, else raise the
Empty exception.
""" """
locked = self.esema.acquire(0) if block:
self.mutex.acquire() self.esema.acquire()
if self._empty(): elif not self.esema.acquire(0):
# The queue is empty -- we can't have esema
self.mutex.release()
raise Empty raise Empty
if not locked: self.mutex.acquire()
locked = self.esema.acquire(0)
if not locked:
# Somebody else has esema
# but we have mutex --
# go out of their way
self.mutex.release()
raise Empty
was_full = self._full() was_full = self._full()
item = self._get() item = self._get()
if was_full: if was_full:
...@@ -106,8 +99,13 @@ class Queue: ...@@ -106,8 +99,13 @@ class Queue:
self.mutex.release() self.mutex.release()
return item return item
# XXX Need to define put_nowait() as well. def get_nowait(self):
"""Remove and return an item from the queue without blocking.
Only get an item if one is immediately available. Otherwise
raise the Empty exception.
"""
return self.get(0)
# Override these methods to implement other queue organizations # Override these methods to implement other queue organizations
# (e.g. stack or priority queue). # (e.g. stack or priority queue).
......
import ntpath
import string
errors = 0
def tester(fn, wantResult):
fn = string.replace(fn, "\\", "\\\\")
gotResult = eval(fn)
if wantResult != gotResult:
print "error!"
print "evaluated: " + str(fn)
print "should be: " + str(wantResult)
print " returned: " + str(gotResult)
print ""
global errors
errors = errors + 1
tester('ntpath.splitdrive("c:\\foo\\bar")', ('c:', '\\foo\\bar'))
tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar'))
tester('ntpath.splitdrive("c:/foo/bar")', ('c:', '/foo/bar'))
tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar'))
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint\\foo', 'bar'))
tester('ntpath.split("c:\\")', ('c:\\', ''))
tester('ntpath.split("\\\\conky\\mountpoint\\")', ('\\\\conky\\mountpoint\\', ''))
tester('ntpath.split("c:/")', ('c:/', ''))
tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
tester('ntpath.isabs("c:\\")', 1)
tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
tester('ntpath.isabs("\\foo")', 1)
tester('ntpath.isabs("\\foo\\bar")', 1)
if errors:
print str(errors) + " errors."
else:
print "No errors. Thank your lucky stars."
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment