[Python-checkins] r72272 - in python/branches/pep-0383: Lib/test/test_codecs.py Lib/test/test_os.py Lib/test/test_pep383.py Modules/posixmodule.c Python/codecs.c
martin.v.loewis
python-checkins at python.org
Mon May 4 06:59:36 CEST 2009
Author: martin.v.loewis
Date: Mon May 4 06:59:36 2009
New Revision: 72272
Log:
Address comments of Rietveld issue 52095 patchset 1.
Removed:
python/branches/pep-0383/Lib/test/test_pep383.py
Modified:
python/branches/pep-0383/Lib/test/test_codecs.py
python/branches/pep-0383/Lib/test/test_os.py
python/branches/pep-0383/Modules/posixmodule.c
python/branches/pep-0383/Python/codecs.c
Modified: python/branches/pep-0383/Lib/test/test_codecs.py
==============================================================================
--- python/branches/pep-0383/Lib/test/test_codecs.py (original)
+++ python/branches/pep-0383/Lib/test/test_codecs.py Mon May 4 06:59:36 2009
@@ -1516,6 +1516,34 @@
self.assertEquals(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6))
self.assertEquals(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6))
+class Utf8bTest(unittest.TestCase):
+
+ def test_utf8(self):
+ # Bad byte
+ self.assertEqual(b"foo\x80bar".decode("utf-8", "utf8b"),
+ "foo\udc80bar")
+ self.assertEqual("foo\udc80bar".encode("utf-8", "utf8b"),
+ b"foo\x80bar")
+ # bad-utf-8 encoded surrogate
+ self.assertEqual(b"\xed\xb0\x80".decode("utf-8", "utf8b"),
+ "\udced\udcb0\udc80")
+ self.assertEqual("\udced\udcb0\udc80".encode("utf-8", "utf8b"),
+ b"\xed\xb0\x80")
+
+ def test_ascii(self):
+ # bad byte
+ self.assertEqual(b"foo\x80bar".decode("ascii", "utf8b"),
+ "foo\udc80bar")
+ self.assertEqual("foo\udc80bar".encode("ascii", "utf8b"),
+ b"foo\x80bar")
+
+ def test_charmap(self):
+ # bad byte: \xa5 is unmapped in iso-8859-3
+ self.assertEqual(b"foo\xa5bar".decode("iso-8859-3", "utf8b"),
+ "foo\udca5bar")
+ self.assertEqual("foo\udca5bar".encode("iso-8859-4", "utf8b"),
+ b"foo\xa5bar")
+
def test_main():
support.run_unittest(
@@ -1543,6 +1571,7 @@
CharmapTest,
WithStmtTest,
TypesTest,
+ Utf8bTest,
)
Modified: python/branches/pep-0383/Lib/test/test_os.py
==============================================================================
--- python/branches/pep-0383/Lib/test/test_os.py (original)
+++ python/branches/pep-0383/Lib/test/test_os.py Mon May 4 06:59:36 2009
@@ -7,6 +7,7 @@
import unittest
import warnings
import sys
+import shutil
from test import support
# Tests creating TESTFN
@@ -698,9 +699,44 @@
self.assertRaises(os.error, os.setregid, 0, 0)
self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
+
+ class Pep383Tests(unittest.TestCase):
+ filenames = [b'foo\xf6bar', b'foo\xf6bar']
+
+ def setUp(self):
+ self.fsencoding = sys.getfilesystemencoding()
+ sys.setfilesystemencoding("utf-8")
+ self.dir = support.TESTFN
+ self.bdir = self.dir.encode("utf-8", "utf8b")
+ os.mkdir(self.dir)
+ self.unicodefn = []
+ for fn in self.filenames:
+ f = open(os.path.join(self.bdir, fn), "w")
+ f.close()
+ self.unicodefn.append(fn.decode("utf-8", "utf8b"))
+
+ def tearDown(self):
+ shutil.rmtree(self.dir)
+ sys.setfilesystemencoding(self.fsencoding)
+
+ def test_listdir(self):
+ expected = set(self.unicodefn)
+ found = set(os.listdir(support.TESTFN))
+ self.assertEquals(found, expected)
+
+ def test_open(self):
+ for fn in self.unicodefn:
+ f = open(os.path.join(self.dir, fn))
+ f.close()
+
+ def test_stat(self):
+ for fn in self.unicodefn:
+ os.stat(os.path.join(self.dir, fn))
else:
class PosixUidGidTests(unittest.TestCase):
pass
+ class Pep383Tests(unittest.TestCase):
+ pass
def test_main():
support.run_unittest(
@@ -714,7 +750,8 @@
ExecTests,
Win32ErrorTests,
TestInvalidFD,
- PosixUidGidTests
+ PosixUidGidTests,
+ Pep383Tests
)
if __name__ == "__main__":
Deleted: python/branches/pep-0383/Lib/test/test_pep383.py
==============================================================================
--- python/branches/pep-0383/Lib/test/test_pep383.py Mon May 4 06:59:36 2009
+++ (empty file)
@@ -1,76 +0,0 @@
-from test import support
-import unittest, shutil, os, sys
-
-class Utf8bTest(unittest.TestCase):
-
- def test_utf8(self):
- # Bad byte
- self.assertEqual(b"foo\x80bar".decode("utf-8", "utf8b"),
- "foo\udc80bar")
- self.assertEqual("foo\udc80bar".encode("utf-8", "utf8b"),
- b"foo\x80bar")
- # bad-utf-8 encoded surrogate
- self.assertEqual(b"\xed\xb0\x80".decode("utf-8", "utf8b"),
- "\udced\udcb0\udc80")
- self.assertEqual("\udced\udcb0\udc80".encode("utf-8", "utf8b"),
- b"\xed\xb0\x80")
-
- def test_ascii(self):
- # bad byte
- self.assertEqual(b"foo\x80bar".decode("ascii", "utf8b"),
- "foo\udc80bar")
- self.assertEqual("foo\udc80bar".encode("ascii", "utf8b"),
- b"foo\x80bar")
-
- def test_charmap(self):
- # bad byte: \xa5 is unmapped in iso-8859-3
- self.assertEqual(b"foo\xa5bar".decode("iso-8859-3", "utf8b"),
- "foo\udca5bar")
- self.assertEqual("foo\udca5bar".encode("iso-8859-4", "utf8b"),
- b"foo\xa5bar")
-
-class FileTests(unittest.TestCase):
- if os.name != 'win32':
- filenames = [b'foo\xf6bar', b'foo\xf6bar']
- else:
- # PEP 383 has no effect on file name handling on Windows
- filenames = []
-
- def setUp(self):
- self.fsencoding = sys.getfilesystemencoding()
- sys.setfilesystemencoding("utf-8")
- self.dir = support.TESTFN
- self.bdir = self.dir.encode("utf-8", "utf8b")
- os.mkdir(self.dir)
- self.unicodefn = []
- for fn in self.filenames:
- f = open(self.bdir + b"/" + fn, "w")
- f.close()
- self.unicodefn.append(fn.decode("utf-8", "utf8b"))
-
- def tearDown(self):
- shutil.rmtree(self.dir)
- sys.setfilesystemencoding(self.fsencoding)
-
- def test_listdir(self):
- expected = set(self.unicodefn)
- found = set(os.listdir(support.TESTFN))
- self.assertEquals(found, expected)
-
- def test_open(self):
- for fn in self.unicodefn:
- f = open(os.path.join(self.dir, fn))
- f.close()
-
- def test_stat(self):
- for fn in self.unicodefn:
- os.stat(os.path.join(self.dir, fn))
-
-def test_main():
- support.run_unittest(
- Utf8bTest,
- FileTests)
-
-
-if __name__ == "__main__":
- test_main()
Modified: python/branches/pep-0383/Modules/posixmodule.c
==============================================================================
--- python/branches/pep-0383/Modules/posixmodule.c (original)
+++ python/branches/pep-0383/Modules/posixmodule.c Mon May 4 06:59:36 2009
@@ -545,15 +545,16 @@
if(PyBytes_Check(o))
return PyBytes_AsString(o);
else if(PyByteArray_Check(o)) {
- if (lock && o->ob_type->tp_as_buffer->bf_getbuffer(o, NULL, 0) < 0)
+ if (lock && PyObject_GetBuffer(o, NULL, 0) < 0)
/* On a bytearray, this should not fail. */
PyErr_BadInternalCall();
return PyByteArray_AsString(o);
} else {
/* The FS converter should have verified that this
is either bytes or bytearray. */
- PyErr_BadInternalCall();
- return NULL;
+ Py_FatalError("bad object passed to bytes2str");
+ /* not reached. */
+ return "";
}
}
Modified: python/branches/pep-0383/Python/codecs.c
==============================================================================
--- python/branches/pep-0383/Python/codecs.c (original)
+++ python/branches/pep-0383/Python/codecs.c Mon May 4 06:59:36 2009
@@ -846,8 +846,10 @@
return NULL;
startp = PyUnicode_AS_UNICODE(object);
res = PyBytes_FromStringAndSize(NULL, end-start);
- if (!res)
+ if (!res) {
+ Py_DECREF(object);
return NULL;
+ }
outp = PyBytes_AsString(res);
for (p = startp+start; p < startp+end; p++) {
Py_UNICODE ch = *p;
@@ -886,12 +888,12 @@
ch[consumed] = 0xdc00 + p[start+consumed];
consumed++;
}
+ Py_DECREF(object);
if (!consumed) {
/* codec complained about ASCII byte. */
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
return NULL;
}
- Py_DECREF(object);
return Py_BuildValue("(u#n)", ch, consumed, start+consumed);
}
else {
More information about the Python-checkins
mailing list