summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2006-04-20 01:29:48 +0000
committerSkip Montanaro <skip@pobox.com>2006-04-20 01:29:48 +0000
commit94785ef14294e9d924c9ceee3d6f9082d4555f28 (patch)
tree5953b37130e2af4a1cd7ebd5f80784962e97b750
parentd0b8e83dc5b92e71d08d39298641d679be419dd8 (diff)
downloadcpython-git-94785ef14294e9d924c9ceee3d6f9082d4555f28.tar.gz
Correct implementation and documentation of os.confstr. Add a simple test
case. I've yet to figure out how to provoke a None return I can test.
-rw-r--r--Doc/lib/libos.tex6
-rw-r--r--Lib/test/test_posix.py5
-rw-r--r--Modules/posixmodule.c15
3 files changed, 17 insertions, 9 deletions
diff --git a/Doc/lib/libos.tex b/Doc/lib/libos.tex
index ebe30210e3..9ded3ae0ca 100644
--- a/Doc/lib/libos.tex
+++ b/Doc/lib/libos.tex
@@ -1844,14 +1844,14 @@ Return string-valued system configuration values.
string which is the name of a defined system value; these names are
specified in a number of standards (\POSIX, \UNIX{} 95, \UNIX{} 98, and
others). Some platforms define additional names as well. The names
-known to the host operating system are given in the
+known to the host operating system are given as the keys of the
\code{confstr_names} dictionary. For configuration variables not
included in that mapping, passing an integer for \var{name} is also
accepted.
Availability: Macintosh, \UNIX.
-If the configuration value specified by \var{name} isn't defined, the
-empty string is returned.
+If the configuration value specified by \var{name} isn't defined,
+\code{None} is returned.
If \var{name} is a string and is not known, \exception{ValueError} is
raised. If a specific value for \var{name} is not supported by the
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 1ccc62bdf2..f98c723193 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -73,6 +73,11 @@ class PosixTester(unittest.TestCase):
finally:
fp.close()
+ def test_confstr(self):
+ if hasattr(posix, 'confstr'):
+ self.assertRaises(ValueError, posix.confstr, "CS_garbage")
+ self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
+
def test_dup2(self):
if hasattr(posix, 'dup2'):
fp1 = open(test_support.TESTFN)
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index d91d8b5eec..4c462a039c 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -6817,15 +6817,18 @@ posix_confstr(PyObject *self, PyObject *args)
errno = 0;
len = confstr(name, buffer, sizeof(buffer));
- if (len == -1) {
- posix_error();
- }
- else if (len == 0) {
- result = PyString_FromString("");
+ if (len == 0) {
+ if (errno) {
+ posix_error();
+ }
+ else {
+ result = Py_None;
+ Py_INCREF(Py_None);
+ }
}
else {
if ((unsigned int)len >= sizeof(buffer)) {
- result = PyString_FromStringAndSize(NULL, len);
+ result = PyString_FromStringAndSize(NULL, len+1);
if (result != NULL)
confstr(name, PyString_AS_STRING(result), len+1);
}