summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKurt B. Kaiser <kbk@shore.net>2011-05-14 17:31:54 -0400
committerKurt B. Kaiser <kbk@shore.net>2011-05-14 17:31:54 -0400
commit4b16ff2c44e709a7a7ea29ac0a6d41b8046c7f2d (patch)
treee4289ec953583a16b167b7c49e3146073c61f6d9
parentf369b8152d63072a5b819095cb97ab62ed8e8311 (diff)
parent154bdf92fce957f331a57a54e3cdcaeefeba25ff (diff)
downloadcpython-git-4b16ff2c44e709a7a7ea29ac0a6d41b8046c7f2d.tar.gz
Merge heads
-rw-r--r--Doc/library/pprint.rst130
-rw-r--r--Lib/test/test_zlib.py1
-rw-r--r--Modules/zlibmodule.c25
3 files changed, 111 insertions, 45 deletions
diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst
index 9ab12eea1d..fc54208d15 100644
--- a/Doc/library/pprint.rst
+++ b/Doc/library/pprint.rst
@@ -189,37 +189,105 @@ are converted to strings. The default implementation uses the internals of the
.. _pprint-example:
-pprint Example
---------------
+Example
+-------
-This example demonstrates several uses of the :func:`pprint` function and its
-parameters.
+To demonstrate several uses of the :func:`pprint` function and its parameters,
+let's fetch information about a package from PyPI::
+ >>> import json
>>> import pprint
- >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
- ... ('parrot', ('fresh fruit',))))))))
- >>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
- >>> pprint.pprint(stuff)
- ['aaaaaaaaaa',
- ('spam',
- ('eggs',
- ('lumberjack',
- ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
- >>> pprint.pprint(stuff, depth=3)
- ['aaaaaaaaaa',
- ('spam', ('eggs', (...))),
- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
- >>> pprint.pprint(stuff, width=60)
- ['aaaaaaaaaa',
- ('spam',
- ('eggs',
- ('lumberjack',
- ('knights',
- ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
- 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
-
+ >>> from urllib.request import urlopen
+ >>> with urlopen('http://pypi.python.org/pypi/configparser/json') as url:
+ ... http_info = url.info()
+ ... raw_data = url.read().decode(http_info.get_content_charset())
+ >>> package_data = json.loads(raw_data)
+ >>> result = {'headers': http_info.items(), 'body': package_data}
+
+In its basic form, :func:`pprint` shows the whole object::
+
+ >>> pprint.pprint(result)
+ {'body': {'info': {'_pypi_hidden': False,
+ '_pypi_ordering': 12,
+ 'classifiers': ['Development Status :: 4 - Beta',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: MIT License',
+ 'Natural Language :: English',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 2',
+ 'Programming Language :: Python :: 2.6',
+ 'Programming Language :: Python :: 2.7',
+ 'Topic :: Software Development :: Libraries',
+ 'Topic :: Software Development :: Libraries :: Python Modules'],
+ 'download_url': 'UNKNOWN',
+ 'home_page': 'http://docs.python.org/py3k/library/configparser.html',
+ 'keywords': 'configparser ini parsing conf cfg configuration file',
+ 'license': 'MIT',
+ 'name': 'configparser',
+ 'package_url': 'http://pypi.python.org/pypi/configparser',
+ 'platform': 'any',
+ 'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
+ 'requires_python': None,
+ 'stable_version': None,
+ 'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
+ 'version': '3.2.0r3'},
+ 'urls': [{'comment_text': '',
+ 'downloads': 47,
+ 'filename': 'configparser-3.2.0r3.tar.gz',
+ 'has_sig': False,
+ 'md5_digest': '8500fd87c61ac0de328fc996fce69b96',
+ 'packagetype': 'sdist',
+ 'python_version': 'source',
+ 'size': 32281,
+ 'upload_time': '2011-05-10T16:28:50',
+ 'url': 'http://pypi.python.org/packages/source/c/configparser/configparser-3.2.0r3.tar.gz'}]},
+ 'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
+ ('Server', 'Apache/2.2.16 (Debian)'),
+ ('Content-Disposition', 'inline'),
+ ('Connection', 'close'),
+ ('Transfer-Encoding', 'chunked'),
+ ('Content-Type', 'application/json; charset="UTF-8"')]}
+
+The result can be limited to a certain *depth* (ellipsis is used for deeper
+contents)::
+
+ >>> pprint.pprint(result, depth=3)
+ {'body': {'info': {'_pypi_hidden': False,
+ '_pypi_ordering': 12,
+ 'classifiers': [...],
+ 'download_url': 'UNKNOWN',
+ 'home_page': 'http://docs.python.org/py3k/library/configparser.html',
+ 'keywords': 'configparser ini parsing conf cfg configuration file',
+ 'license': 'MIT',
+ 'name': 'configparser',
+ 'package_url': 'http://pypi.python.org/pypi/configparser',
+ 'platform': 'any',
+ 'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
+ 'requires_python': None,
+ 'stable_version': None,
+ 'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
+ 'version': '3.2.0r3'},
+ 'urls': [{...}]},
+ 'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
+ ('Server', 'Apache/2.2.16 (Debian)'),
+ ('Content-Disposition', 'inline'),
+ ('Connection', 'close'),
+ ('Transfer-Encoding', 'chunked'),
+ ('Content-Type', 'application/json; charset="UTF-8"')]}
+
+Additionally, maximum *width* can be suggested. If a long object cannot be
+split, the specified width will be exceeded::
+
+ >>> pprint.pprint(result['headers'], width=30)
+ [('Date',
+ 'Sat, 14 May 2011 12:48:52 GMT'),
+ ('Server',
+ 'Apache/2.2.16 (Debian)'),
+ ('Content-Disposition',
+ 'inline'),
+ ('Connection', 'close'),
+ ('Transfer-Encoding',
+ 'chunked'),
+ ('Content-Type',
+ 'application/json; charset="UTF-8"')]
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 852857bb41..48d9f582cf 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -193,6 +193,7 @@ class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
data = b'x' * size
try:
self.assertRaises(OverflowError, zlib.compress, data, 1)
+ self.assertRaises(OverflowError, zlib.decompress, data)
finally:
data = None
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 969db4aa4d..fa07739a16 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -116,7 +116,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
{
PyObject *ReturnVal = NULL;
Py_buffer pinput;
- Byte *input, *output;
+ Byte *input, *output = NULL;
unsigned int length;
int level=Z_DEFAULT_COMPRESSION, err;
z_stream zst;
@@ -127,20 +127,19 @@ PyZlib_compress(PyObject *self, PyObject *args)
if (pinput.len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,
- "size does not fit in an unsigned int");
- return NULL;
+ "Size does not fit in an unsigned int");
+ goto error;
}
- length = pinput.len;
input = pinput.buf;
+ length = pinput.len;
zst.avail_out = length + length/1000 + 12 + 1;
output = (Byte*)malloc(zst.avail_out);
if (output == NULL) {
- PyBuffer_Release(&pinput);
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data");
- return NULL;
+ goto error;
}
/* Past the point of no return. From here on out, we need to make sure
@@ -203,7 +202,7 @@ PyDoc_STRVAR(decompress__doc__,
static PyObject *
PyZlib_decompress(PyObject *self, PyObject *args)
{
- PyObject *result_str;
+ PyObject *result_str = NULL;
Py_buffer pinput;
Byte *input;
unsigned int length;
@@ -218,11 +217,11 @@ PyZlib_decompress(PyObject *self, PyObject *args)
if (pinput.len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,
- "size does not fit in an unsigned int");
- return NULL;
+ "Size does not fit in an unsigned int");
+ goto error;
}
- length = pinput.len;
input = pinput.buf;
+ length = pinput.len;
if (r_strlen <= 0)
r_strlen = 1;
@@ -230,10 +229,8 @@ PyZlib_decompress(PyObject *self, PyObject *args)
zst.avail_in = length;
zst.avail_out = r_strlen;
- if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
- PyBuffer_Release(&pinput);
- return NULL;
- }
+ if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
+ goto error;
zst.zalloc = (alloc_func)NULL;
zst.zfree = (free_func)Z_NULL;