Skip to content

Commit a131ad1

Browse files
author
georg.brandl
committed
Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k ........ r67525 | georg.brandl | 2008-12-04 19:18:16 +0100 (Thu, 04 Dec 2008) | 2 lines #4527: fix "unicode or string". ........ r67526 | georg.brandl | 2008-12-04 19:19:41 +0100 (Thu, 04 Dec 2008) | 2 lines #4526: fix typo. ........ r67527 | georg.brandl | 2008-12-04 19:21:46 +0100 (Thu, 04 Dec 2008) | 2 lines #4521: fix getcwdu() vs getcwdb(). ........ r67529 | georg.brandl | 2008-12-04 19:28:38 +0100 (Thu, 04 Dec 2008) | 2 lines #4513: remove traces of zip() docstring from when it was izip(). ........ r67548 | guido.van.rossum | 2008-12-05 06:49:12 +0100 (Fri, 05 Dec 2008) | 3 lines Tweak verbiage and markup for relative/absolute import. Backport candidate. (I guess *all* changes to this file should be backported to the 3.0 branch.) ........ r67549 | martin.v.loewis | 2008-12-05 08:20:46 +0100 (Fri, 05 Dec 2008) | 2 lines Send mails to python-checkins now. ........ r67551 | georg.brandl | 2008-12-05 08:32:56 +0100 (Fri, 05 Dec 2008) | 2 lines #4355: refer to urlencode in the correct module. ........ r67552 | georg.brandl | 2008-12-05 08:45:54 +0100 (Fri, 05 Dec 2008) | 2 lines #4401: Re-add os.extsep. ........ r67555 | georg.brandl | 2008-12-05 08:57:51 +0100 (Fri, 05 Dec 2008) | 2 lines #4427: fix arguments for "y" BuildValue type. ........ r67578 | georg.brandl | 2008-12-05 16:12:15 +0100 (Fri, 05 Dec 2008) | 2 lines #4504, #4505: Update noddy examples in "Extending & Embedding". ........ r67586 | georg.brandl | 2008-12-05 16:55:41 +0100 (Fri, 05 Dec 2008) | 2 lines Manually merged r67583: move __import__ doc to the bottom. ........ r67593 | georg.brandl | 2008-12-05 19:04:41 +0100 (Fri, 05 Dec 2008) | 2 lines #4550: fix 2.x syntax in webservers howto. ........ r67594 | georg.brandl | 2008-12-05 19:06:58 +0100 (Fri, 05 Dec 2008) | 2 lines #4549: Mention nonlocal statement in tutorial section about scoping. ........ r67596 | georg.brandl | 2008-12-05 19:31:51 +0100 (Fri, 05 Dec 2008) | 2 lines Use :samp: role. ........ r67605 | georg.brandl | 2008-12-06 09:12:11 +0100 (Sat, 06 Dec 2008) | 2 lines #4557: remove 'c' typecode from array docs and add a note in whatsnew. ........ r67606 | georg.brandl | 2008-12-06 09:14:46 +0100 (Sat, 06 Dec 2008) | 2 lines cmp() is not really gone. ........ r67609 | georg.brandl | 2008-12-06 15:28:56 +0100 (Sat, 06 Dec 2008) | 2 lines #4562: fix zip() examples. ........ r67612 | skip.montanaro | 2008-12-06 18:25:02 +0100 (Sat, 06 Dec 2008) | 2 lines issue 4483 - _dbm build failures on systems with gdbm_compat lib. ........ r67616 | benjamin.peterson | 2008-12-06 19:32:13 +0100 (Sat, 06 Dec 2008) | 1 line there are only four asdl builtin types in py3k ........ r67617 | benjamin.peterson | 2008-12-06 19:33:20 +0100 (Sat, 06 Dec 2008) | 1 line update version number ........ r67632 | georg.brandl | 2008-12-07 15:09:20 +0100 (Sun, 07 Dec 2008) | 2 lines #4576: fix ob_type access. ........ r67635 | benjamin.peterson | 2008-12-07 15:58:03 +0100 (Sun, 07 Dec 2008) | 1 line document how to deal with binary standard streams ........ r67638 | benjamin.peterson | 2008-12-07 16:07:02 +0100 (Sun, 07 Dec 2008) | 1 line remove extraneous 'text' ........ git-svn-id: http://svn.python.org/projects/python/branches/release30-maint@67644 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d639da1 commit a131ad1

22 files changed

Lines changed: 328 additions & 330 deletions

File tree

Doc/c-api/arg.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ and the following format units are left untouched.
438438
Convert a C string and its length to a Python object. If the C string pointer
439439
is *NULL*, the length is ignored and ``None`` is returned.
440440

441-
``y`` (bytes) [char \*, int]
441+
``y`` (bytes) [char \*]
442442
This converts a C string to a Python :func:`bytes` object. If the C
443443
string pointer is *NULL*, ``None`` is returned.
444444

Doc/extending/newtypes.rst

Lines changed: 82 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,25 @@ Moving on, we come to the crunch --- the type object. ::
7373

7474
static PyTypeObject noddy_NoddyType = {
7575
PyObject_HEAD_INIT(NULL)
76-
0, /*ob_size*/
77-
"noddy.Noddy", /*tp_name*/
78-
sizeof(noddy_NoddyObject), /*tp_basicsize*/
79-
0, /*tp_itemsize*/
80-
0, /*tp_dealloc*/
81-
0, /*tp_print*/
82-
0, /*tp_getattr*/
83-
0, /*tp_setattr*/
84-
0, /*tp_compare*/
85-
0, /*tp_repr*/
86-
0, /*tp_as_number*/
87-
0, /*tp_as_sequence*/
88-
0, /*tp_as_mapping*/
89-
0, /*tp_hash */
90-
0, /*tp_call*/
91-
0, /*tp_str*/
92-
0, /*tp_getattro*/
93-
0, /*tp_setattro*/
94-
0, /*tp_as_buffer*/
95-
Py_TPFLAGS_DEFAULT, /*tp_flags*/
76+
"noddy.Noddy", /* tp_name */
77+
sizeof(noddy_NoddyObject), /* tp_basicsize */
78+
0, /* tp_itemsize */
79+
0, /* tp_dealloc */
80+
0, /* tp_print */
81+
0, /* tp_getattr */
82+
0, /* tp_setattr */
83+
0, /* tp_compare */
84+
0, /* tp_repr */
85+
0, /* tp_as_number */
86+
0, /* tp_as_sequence */
87+
0, /* tp_as_mapping */
88+
0, /* tp_hash */
89+
0, /* tp_call */
90+
0, /* tp_str */
91+
0, /* tp_getattro */
92+
0, /* tp_setattro */
93+
0, /* tp_as_buffer */
94+
Py_TPFLAGS_DEFAULT, /* tp_flags */
9695
"Noddy objects", /* tp_doc */
9796
};
9897

@@ -114,13 +113,6 @@ as the type of a type object is "type", but this isn't strictly conforming C and
114113
some compilers complain. Fortunately, this member will be filled in for us by
115114
:cfunc:`PyType_Ready`. ::
116115

117-
0, /* ob_size */
118-
119-
The :attr:`ob_size` field of the header is not used; its presence in the type
120-
structure is a historical artifact that is maintained for binary compatibility
121-
with extension modules compiled for older versions of Python. Always set this
122-
field to zero. ::
123-
124116
"noddy.Noddy", /* tp_name */
125117

126118
The name of our type. This will appear in the default textual representation of
@@ -162,7 +154,7 @@ for now.
162154
Skipping a number of type methods that we don't provide, we set the class flags
163155
to :const:`Py_TPFLAGS_DEFAULT`. ::
164156

165-
Py_TPFLAGS_DEFAULT, /*tp_flags*/
157+
Py_TPFLAGS_DEFAULT, /* tp_flags */
166158

167159
All types should include this constant in their flags. It enables all of the
168160
members defined by the current version of Python.
@@ -193,7 +185,7 @@ All the other type methods are *NULL*, so we'll go over them later --- that's
193185
for a later section!
194186

195187
Everything else in the file should be familiar, except for some code in
196-
:cfunc:`initnoddy`::
188+
:cfunc:`PyInit_noddy`::
197189

198190
if (PyType_Ready(&noddy_NoddyType) < 0)
199191
return;
@@ -273,7 +265,7 @@ allocation and deallocation. At a minimum, we need a deallocation method::
273265
{
274266
Py_XDECREF(self->first);
275267
Py_XDECREF(self->last);
276-
self->ob_type->tp_free((PyObject*)self);
268+
Py_TYPE(self)->tp_free((PyObject*)self);
277269
}
278270

279271
which is assigned to the :attr:`tp_dealloc` member::
@@ -523,8 +515,8 @@ object being created or used, so all we need to do is to add the
523515

524516
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
525517

526-
We rename :cfunc:`initnoddy` to :cfunc:`initnoddy2` and update the module name
527-
passed to :cfunc:`Py_InitModule3`.
518+
We rename :cfunc:`PyInit_noddy` to :cfunc:`PyInit_noddy2` and update the module
519+
name in the :ctype:`PyModuleDef` struct.
528520

529521
Finally, we update our :file:`setup.py` file to build the new module::
530522

@@ -767,7 +759,7 @@ to use it::
767759
Noddy_dealloc(Noddy* self)
768760
{
769761
Noddy_clear(self);
770-
self->ob_type->tp_free((PyObject*)self);
762+
Py_TYPE(self)->tp_free((PyObject*)self);
771763
}
772764

773765
Notice the use of a temporary variable in :cfunc:`Noddy_clear`. We use the
@@ -794,7 +786,7 @@ be simplified::
794786

795787
Finally, we add the :const:`Py_TPFLAGS_HAVE_GC` flag to the class flags::
796788

797-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
789+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
798790

799791
That's pretty much it. If we had written custom :attr:`tp_alloc` or
800792
:attr:`tp_free` slots, we'd need to modify them for cyclic-garbage collection.
@@ -865,20 +857,20 @@ fill that field directly with the :cfunc:`PyList_Type`; it can be done later in
865857
the module's :cfunc:`init` function. ::
866858

867859
PyMODINIT_FUNC
868-
initshoddy(void)
860+
PyInit_shoddy(void)
869861
{
870-
PyObject *m;
862+
PyObject *m;
871863

872-
ShoddyType.tp_base = &PyList_Type;
873-
if (PyType_Ready(&ShoddyType) < 0)
874-
return;
864+
ShoddyType.tp_base = &PyList_Type;
865+
if (PyType_Ready(&ShoddyType) < 0)
866+
return NULL;
875867

876-
m = Py_InitModule3("shoddy", NULL, "Shoddy module");
877-
if (m == NULL)
878-
return;
868+
m = PyModule_Create(&shoddymodule);
869+
if (m == NULL)
870+
return NULL;
879871

880-
Py_INCREF(&ShoddyType);
881-
PyModule_AddObject(m, "Shoddy", (PyObject *) &ShoddyType);
872+
Py_INCREF(&ShoddyType);
873+
PyModule_AddObject(m, "Shoddy", (PyObject *) &ShoddyType);
882874
}
883875

884876
Before calling :cfunc:`PyType_Ready`, the type structure must have the
@@ -960,7 +952,7 @@ needs to be freed here as well. Here is an example of this function::
960952
newdatatype_dealloc(newdatatypeobject * obj)
961953
{
962954
free(obj->obj_UnderlyingDatatypePtr);
963-
obj->ob_type->tp_free(obj);
955+
Py_TYPE(obj)->tp_free(obj);
964956
}
965957

966958
.. index::
@@ -1003,7 +995,7 @@ done. This can be done using the :cfunc:`PyErr_Fetch` and
1003995

1004996
Py_DECREF(self->my_callback);
1005997
}
1006-
obj->ob_type->tp_free((PyObject*)self);
998+
Py_TYPE(obj)->tp_free((PyObject*)self);
1007999
}
10081000

10091001

@@ -1241,49 +1233,50 @@ example that simply raises an exception; if this were really all you wanted, the
12411233
return -1;
12421234
}
12431235

1244-
1245-
Object Comparison
1246-
-----------------
1247-
1248-
::
1249-
1250-
cmpfunc tp_compare;
1251-
1252-
The :attr:`tp_compare` handler is called when comparisons are needed and the
1253-
object does not implement the specific rich comparison method which matches the
1254-
requested comparison. (It is always used if defined and the
1255-
:cfunc:`PyObject_Compare` or :cfunc:`PyObject_Cmp` functions are used, or if
1256-
:func:`cmp` is used from Python.) It is analogous to the :meth:`__cmp__` method.
1257-
This function should return ``-1`` if *obj1* is less than *obj2*, ``0`` if they
1258-
are equal, and ``1`` if *obj1* is greater than *obj2*. (It was previously
1259-
allowed to return arbitrary negative or positive integers for less than and
1260-
greater than, respectively; as of Python 2.2, this is no longer allowed. In the
1261-
future, other return values may be assigned a different meaning.)
1262-
1263-
A :attr:`tp_compare` handler may raise an exception. In this case it should
1264-
return a negative value. The caller has to test for the exception using
1265-
:cfunc:`PyErr_Occurred`.
1266-
1267-
Here is a sample implementation::
1268-
1269-
static int
1270-
newdatatype_compare(newdatatypeobject * obj1, newdatatypeobject * obj2)
1271-
{
1272-
long result;
1273-
1274-
if (obj1->obj_UnderlyingDatatypePtr->size <
1275-
obj2->obj_UnderlyingDatatypePtr->size) {
1276-
result = -1;
1277-
}
1278-
else if (obj1->obj_UnderlyingDatatypePtr->size >
1279-
obj2->obj_UnderlyingDatatypePtr->size) {
1280-
result = 1;
1281-
}
1282-
else {
1283-
result = 0;
1284-
}
1285-
return result;
1286-
}
1236+
.. XXX tp_compare is dead; need to rewrite for tp_richcompare!
1237+
1238+
Object Comparison
1239+
-----------------
1240+
1241+
::
1242+
1243+
cmpfunc tp_compare;
1244+
1245+
The :attr:`tp_compare` handler is called when comparisons are needed and the
1246+
object does not implement the specific rich comparison method which matches the
1247+
requested comparison. (It is always used if defined and the
1248+
:cfunc:`PyObject_Compare` or :cfunc:`PyObject_Cmp` functions are used, or if
1249+
:func:`cmp` is used from Python.) It is analogous to the :meth:`__cmp__` method.
1250+
This function should return ``-1`` if *obj1* is less than *obj2*, ``0`` if they
1251+
are equal, and ``1`` if *obj1* is greater than *obj2*. (It was previously
1252+
allowed to return arbitrary negative or positive integers for less than and
1253+
greater than, respectively; as of Python 2.2, this is no longer allowed. In the
1254+
future, other return values may be assigned a different meaning.)
1255+
1256+
A :attr:`tp_compare` handler may raise an exception. In this case it should
1257+
return a negative value. The caller has to test for the exception using
1258+
:cfunc:`PyErr_Occurred`.
1259+
1260+
Here is a sample implementation::
1261+
1262+
static int
1263+
newdatatype_compare(newdatatypeobject * obj1, newdatatypeobject * obj2)
1264+
{
1265+
long result;
1266+
1267+
if (obj1->obj_UnderlyingDatatypePtr->size <
1268+
obj2->obj_UnderlyingDatatypePtr->size) {
1269+
result = -1;
1270+
}
1271+
else if (obj1->obj_UnderlyingDatatypePtr->size >
1272+
obj2->obj_UnderlyingDatatypePtr->size) {
1273+
result = 1;
1274+
}
1275+
else {
1276+
result = 0;
1277+
}
1278+
return result;
1279+
}
12871280
12881281
12891282
Abstract Protocol Support

Doc/howto/webservers.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ simple CGI program::
101101
# enable debugging
102102
import cgitb; cgitb.enable()
103103

104-
print "Content-Type: text/plain;charset=utf-8"
105-
print
104+
print("Content-Type: text/plain;charset=utf-8")
105+
print()
106106

107-
print "Hello World!"
107+
print("Hello World!")
108108

109109
You need to write this code into a file with a ``.py`` or ``.cgi`` extension,
110110
this depends on your web server configuration. Depending on your web server
@@ -278,8 +278,8 @@ following WSGI-application::
278278
#!/usr/bin/env python
279279
# -*- coding: UTF-8 -*-
280280

281-
from cgi import escape
282281
import sys, os
282+
from cgi import escape
283283
from flup.server.fcgi import WSGIServer
284284

285285
def app(environ, start_response):
@@ -288,7 +288,8 @@ following WSGI-application::
288288
yield '<h1>FastCGI Environment</h1>'
289289
yield '<table>'
290290
for k, v in sorted(environ.items()):
291-
yield '<tr><th>%s</th><td>%s</td></tr>' % (escape(k), escape(v))
291+
yield '<tr><th>{0}</th><td>{1}</td></tr>'.format(
292+
escape(k), escape(v))
292293
yield '</table>'
293294

294295
WSGIServer(app).run()
@@ -476,8 +477,8 @@ placeholders.
476477
Python already includes such simple templates::
477478

478479
# a simple template
479-
template = "<html><body><h1>Hello %s!</h1></body></html>"
480-
print template % "Reader"
480+
template = "<html><body><h1>Hello {who}!</h1></body></html>"
481+
print(template.format(who="Reader"))
481482

482483
The Python standard library also includes some more advanced templates usable
483484
through :class:`string.Template`, but in HTML templates it is needed to use

Doc/includes/noddy.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,48 @@ typedef struct {
77

88
static PyTypeObject noddy_NoddyType = {
99
PyObject_HEAD_INIT(NULL)
10-
0, /*ob_size*/
11-
"noddy.Noddy", /*tp_name*/
12-
sizeof(noddy_NoddyObject), /*tp_basicsize*/
13-
0, /*tp_itemsize*/
14-
0, /*tp_dealloc*/
15-
0, /*tp_print*/
16-
0, /*tp_getattr*/
17-
0, /*tp_setattr*/
18-
0, /*tp_compare*/
19-
0, /*tp_repr*/
20-
0, /*tp_as_number*/
21-
0, /*tp_as_sequence*/
22-
0, /*tp_as_mapping*/
23-
0, /*tp_hash */
24-
0, /*tp_call*/
25-
0, /*tp_str*/
26-
0, /*tp_getattro*/
27-
0, /*tp_setattro*/
28-
0, /*tp_as_buffer*/
29-
Py_TPFLAGS_DEFAULT, /*tp_flags*/
10+
"noddy.Noddy", /* tp_name */
11+
sizeof(noddy_NoddyObject), /* tp_basicsize */
12+
0, /* tp_itemsize */
13+
0, /* tp_dealloc */
14+
0, /* tp_print */
15+
0, /* tp_getattr */
16+
0, /* tp_setattr */
17+
0, /* tp_compare */
18+
0, /* tp_repr */
19+
0, /* tp_as_number */
20+
0, /* tp_as_sequence */
21+
0, /* tp_as_mapping */
22+
0, /* tp_hash */
23+
0, /* tp_call */
24+
0, /* tp_str */
25+
0, /* tp_getattro */
26+
0, /* tp_setattro */
27+
0, /* tp_as_buffer */
28+
Py_TPFLAGS_DEFAULT, /* tp_flags */
3029
"Noddy objects", /* tp_doc */
3130
};
3231

33-
static PyMethodDef noddy_methods[] = {
34-
{NULL} /* Sentinel */
32+
static PyModuleDef noddymodule = {
33+
PyModuleDef_HEAD_INIT,
34+
"noddy",
35+
"Example module that creates an extension type.",
36+
-1,
37+
NULL, NULL, NULL, NULL, NULL
3538
};
3639

37-
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
38-
#define PyMODINIT_FUNC void
39-
#endif
4040
PyMODINIT_FUNC
41-
initnoddy(void)
41+
PyInit_noddy(void)
4242
{
4343
PyObject* m;
4444

4545
noddy_NoddyType.tp_new = PyType_GenericNew;
4646
if (PyType_Ready(&noddy_NoddyType) < 0)
47-
return;
47+
return NULL;
4848

49-
m = Py_InitModule3("noddy", noddy_methods,
50-
"Example module that creates an extension type.");
49+
m = PyModule_Create(&noddymodule);
50+
if (m == NULL)
51+
return NULL;
5152

5253
Py_INCREF(&noddy_NoddyType);
5354
PyModule_AddObject(m, "Noddy", (PyObject *)&noddy_NoddyType);

0 commit comments

Comments
 (0)