]> granicus.if.org Git - python/commitdiff
Add future_builtins.ascii().
authorGeorg Brandl <georg@python.org>
Wed, 11 Jun 2008 18:55:38 +0000 (18:55 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 11 Jun 2008 18:55:38 +0000 (18:55 +0000)
Doc/library/future_builtins.rst
Misc/NEWS
Modules/future_builtins.c

index d25aff780bace80cfdfae036afe9e2d08bd1b93a..6a1fdab5c57391505ab82a5d1ba88d1aa6ed4c1f 100644 (file)
@@ -28,6 +28,14 @@ this usage and leave the new builtins alone.
 
 Available builtins are:
 
+.. function:: ascii(object)
+
+   Returns the same as :func:`repr`.  In Python 3, :func:`repr` will return
+   printable Unicode characters unescaped, while :func:`ascii` will always
+   backslash-escape them.  Using :func:`future_builtins.ascii` instead of
+   :func:`repr` in 2.6 code makes it clear that you need a pure ASCII return
+   value.
+
 .. function:: filter(function, iterable)
 
    Works like :func:`itertools.ifilter`.
index 06b8b45a1a22f82fec62d38f498252036d6010fd..02c6cff0f886d25424a97da058e85c7d97b5adef 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6 beta 1?
 Core and Builtins
 -----------------
 
+- Add future_builtins.ascii().
+
 - Several set methods now accept multiple arguments:  update(), union(),
   intersection(), intersection_update(), difference(), and difference_update().
 
index 5baaa6051dd4ec282b7e4d1655e20cb745e62765..4c840fb161ed2380679064f6e15214eab141600d 100644 (file)
@@ -45,11 +45,25 @@ PyDoc_STRVAR(oct_doc,
 Return the octal representation of an integer or long integer.");
 
 
+static PyObject *
+builtin_ascii(PyObject *self, PyObject *v)
+{
+       return PyObject_Repr(v);
+}
+
+PyDoc_STRVAR(ascii_doc,
+"ascii(object) -> string\n\
+\n\
+Return the same as repr().  In Python 3.x, the repr() result will\n\
+contain printable characters unescaped, while the ascii() result\n\
+will have such characters backslash-escaped.");
+
 /* List of functions exported by this module */
 
 static PyMethodDef module_functions[] = {
        {"hex",         builtin_hex,        METH_O, hex_doc},
        {"oct",         builtin_oct,        METH_O, oct_doc},
+       {"ascii",       builtin_ascii,      METH_O, ascii_doc},
        {NULL,          NULL}   /* Sentinel */
 };