Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9bbc8b

Browse files
committed
Devil merge!
Merged revisions 66561,66564,66580,66610,66614,66618,66624-66625,66628-66629,66643,66645,66660-66665 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r66561 | benjamin.peterson | 2008-09-22 17:13:29 -0500 (Mon, 22 Sep 2008) | 1 line clean up docs for platform's linux_distribution and dist functions ........ r66564 | benjamin.peterson | 2008-09-23 08:32:46 -0500 (Tue, 23 Sep 2008) | 1 line mention how to override boolean evaluation ........ r66580 | georg.brandl | 2008-09-24 04:47:55 -0500 (Wed, 24 Sep 2008) | 2 lines Indentation normalization. ........ r66610 | andrew.kuchling | 2008-09-24 12:27:55 -0500 (Wed, 24 Sep 2008) | 1 line Improve wording ........ r66614 | benjamin.peterson | 2008-09-24 17:11:59 -0500 (Wed, 24 Sep 2008) | 4 lines #3950 fix missing scale factors in turtle.py reviewers: Georg, Benjamin ........ r66618 | benjamin.peterson | 2008-09-25 15:35:45 -0500 (Thu, 25 Sep 2008) | 1 line add a NEWs entry for r66614 ........ r66624 | raymond.hettinger | 2008-09-25 18:31:52 -0500 (Thu, 25 Sep 2008) | 1 line Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly if the field names were input in Unicode. ........ r66625 | benjamin.peterson | 2008-09-25 21:58:36 -0500 (Thu, 25 Sep 2008) | 1 line add the beginnings of a C-API 2 -> 3 porting guide ........ r66628 | benjamin.peterson | 2008-09-26 15:52:06 -0500 (Fri, 26 Sep 2008) | 1 line add an 'other options' section ........ r66629 | georg.brandl | 2008-09-26 16:15:21 -0500 (Fri, 26 Sep 2008) | 2 lines typos. ........ r66643 | andrew.kuchling | 2008-09-27 09:12:33 -0500 (Sat, 27 Sep 2008) | 1 line Add a last bunch of items ........ r66645 | benjamin.peterson | 2008-09-27 11:23:55 -0500 (Sat, 27 Sep 2008) | 1 line 2to3's api should be considered unstable ........ r66660 | andrew.kuchling | 2008-09-27 17:54:08 -0500 (Sat, 27 Sep 2008) | 1 line #3510: future-proof text ........ r66661 | benjamin.peterson | 2008-09-27 18:28:43 -0500 (Sat, 27 Sep 2008) | 1 line clarify a few things ........ r66662 | andrew.kuchling | 2008-09-27 19:15:27 -0500 (Sat, 27 Sep 2008) | 1 line #1579477: mention necessity to flush output before exec'ing ........ r66663 | andrew.kuchling | 2008-09-27 20:08:47 -0500 (Sat, 27 Sep 2008) | 1 line #1415508: Document two functions ........ r66664 | benjamin.peterson | 2008-09-27 20:51:36 -0500 (Sat, 27 Sep 2008) | 1 line better grammar ........ r66665 | benjamin.peterson | 2008-09-27 20:53:29 -0500 (Sat, 27 Sep 2008) | 1 line note the 2to3 -d could be useful for other refactoring ........
1 parent d61de7f commit e9bbc8b

12 files changed

Lines changed: 322 additions & 42 deletions

File tree

Doc/howto/cporting.rst

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
.. highlightlang:: c
2+
3+
********************************
4+
Porting Extension Modules to 3.0
5+
********************************
6+
7+
:author: Benjamin Peterson
8+
9+
10+
.. topic:: Abstract
11+
12+
Although changing the C-API was not one of Python 3.0's objectives, the many
13+
Python level changes made leaving 2.x's API intact impossible. In fact, some
14+
changes such as :func:`int` and :func:`long` unification are more obvious on
15+
the C level. This document endeavors to document incompatibilities and how
16+
they can be worked around.
17+
18+
19+
Conditional compilation
20+
=======================
21+
22+
The easiest way to compile only some code for 3.0 is to check if
23+
:cmacro:`PY_MAJOR_VERSION` is greater than or equal to 3. ::
24+
25+
#if PY_MAJOR_VERSION >= 3
26+
#define IS_PY3K
27+
#endif
28+
29+
API functions that are not present can be aliased to their equivalents within
30+
conditional blocks.
31+
32+
33+
Changes to Object APIs
34+
======================
35+
36+
Python 3.0 merged together some types with similar functions while cleanly
37+
separating others.
38+
39+
40+
str/unicode Unification
41+
-----------------------
42+
43+
44+
Python 3.0's :func:`str` (``PyString_*`` functions in C) type is equivalent to
45+
2.x's :func:`unicode` (``PyUnicode_*``). The old 8-bit string type has become
46+
:func:`bytes`. Python 2.6 and later provide a compatibility header,
47+
:file:`bytesobject.h`, mapping ``PyBytes`` names to ``PyString`` ones. For best
48+
compatibility with 3.0, :ctype:`PyUnicode` should be used for textual data and
49+
:ctype:`PyBytes` for binary data. It's also important to remember that
50+
:ctype:`PyBytes` and :ctype:`PyUnicode` in 3.0 are not interchangeable like
51+
:ctype:`PyString` and :ctype:`PyString` are in 2.x. The following example shows
52+
best practices with regards to :ctype:`PyUnicode`, :ctype:`PyString`, and
53+
:ctype:`PyBytes`. ::
54+
55+
#include "stdlib.h"
56+
#include "Python.h"
57+
#include "bytesobject.h"
58+
59+
/* text example */
60+
static PyObject *
61+
say_hello(PyObject *self, PyObject *args) {
62+
PyObject *name, *result;
63+
64+
if (!PyArg_ParseTuple(args, "U:say_hello", &name))
65+
return NULL;
66+
67+
result = PyUnicode_FromFormat("Hello, %S!", name);
68+
return result;
69+
}
70+
71+
/* just a forward */
72+
static char * do_encode(PyObject *);
73+
74+
/* bytes example */
75+
static PyObject *
76+
encode_object(PyObject *self, PyObject *args) {
77+
char *encoded;
78+
PyObject *result, *myobj;
79+
80+
if (!PyArg_ParseTuple(args, "O:encode_object", &myobj))
81+
return NULL;
82+
83+
encoded = do_encode(myobj);
84+
if (encoded == NULL)
85+
return NULL;
86+
result = PyBytes_FromString(encoded);
87+
free(encoded);
88+
return result;
89+
}
90+
91+
92+
long/int Unification
93+
--------------------
94+
95+
In Python 3.0, there is only one integer type. It is called :func:`int` on the
96+
Python level, but actually corresponds to 2.x's :func:`long` type. In the
97+
C-API, ``PyInt_*`` functions are replaced by their ``PyLong_*`` neighbors. The
98+
best course of action here is using the ``PyInt_*`` functions aliased to
99+
``PyLong_*`` found in :file:`intobject.h`. The the abstract ``PyNumber_*`` APIs
100+
can also be used in some cases. ::
101+
102+
#include "Python.h"
103+
#include "intobject.h"
104+
105+
static PyObject *
106+
add_ints(PyObject *self, PyObject *args) {
107+
int one, two;
108+
PyObject *result;
109+
110+
if (!PyArg_ParseTuple(args, "ii:add_ints", &one, &two))
111+
return NULL;
112+
113+
return PyInt_FromLong(one + two);
114+
}
115+
116+
117+
118+
Module initialization and state
119+
===============================
120+
121+
Python 3.0 has a revamped extension module initialization system. (See PEP
122+
:pep:`3121`.) Instead of storing module state in globals, they should be stored
123+
in an interpreter specific structure. Creating modules that act correctly in
124+
both 2.x and 3.0 is tricky. The following simple example demonstrates how. ::
125+
126+
#include "Python.h"
127+
128+
struct module_state {
129+
PyObject *error;
130+
};
131+
132+
#if PY_MAJOR_VERSION >= 3
133+
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
134+
#else
135+
#define GETSTATE(m) (&_state)
136+
static struct module_state _state;
137+
#endif
138+
139+
static PyObject *
140+
error_out(PyObject *m) {
141+
struct module_state *st = GETSTATE(m);
142+
PyErr_SetString(st->error, "something bad happened");
143+
return NULL;
144+
}
145+
146+
static PyMethodDef myextension_methods[] = {
147+
{"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
148+
{NULL, NULL}
149+
};
150+
151+
#if PY_MAJOR_VERSION >= 3
152+
153+
static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
154+
Py_VISIT(GETSTATE(m)->error);
155+
return 0;
156+
}
157+
158+
static int myextension_clear(PyObject *m) {
159+
Py_CLEAR(GETSTATE(m)->error);
160+
return 0;
161+
}
162+
163+
164+
static struct PyModuleDef moduledef = {
165+
PyModuleDef_HEAD_INIT,
166+
"myextension",
167+
NULL,
168+
sizeof(struct module_state),
169+
myextension_methods,
170+
NULL,
171+
myextension_traverse,
172+
myextension_clear,
173+
NULL
174+
};
175+
176+
#define INITERROR return NULL
177+
178+
PyObject *
179+
PyInit_myextension(void)
180+
181+
#else
182+
#define INITERROR return
183+
184+
void
185+
initmyextension(void)
186+
#endif
187+
{
188+
#if PY_MAJOR_VERSION >= 3
189+
PyObject *module = PyModule_Create(&moduledef);
190+
#else
191+
PyObject *module = Py_InitModule("myextension", myextension_methods);
192+
#endif
193+
194+
if (module == NULL)
195+
INITERROR;
196+
struct module_state *st = GETSTATE(module);
197+
198+
st->error = PyErr_NewException("myextension.Error", NULL, NULL);
199+
if (st->error == NULL) {
200+
Py_DECREF(module);
201+
INITERROR;
202+
}
203+
204+
#if PY_MAJOR_VERSION >= 3
205+
return module;
206+
#endif
207+
}
208+
209+
210+
Other options
211+
=============
212+
213+
If you are writing a new extension module, you might consider `Cython
214+
<http://www.cython.org>`_. It translates a Python-like language to C. The
215+
extension modules it creates are compatible with Python 3.x and 2.x.
216+

Doc/howto/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Currently, the HOWTOs are:
1414
:maxdepth: 1
1515

1616
advocacy.rst
17+
cporting.rst
1718
curses.rst
1819
doanddont.rst
1920
functional.rst

Doc/library/2to3.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ warning beneath the diff for a file. You should address the warning in order to
7474
have compliant 3.x code.
7575

7676
2to3 can also refactor doctests. To enable this mode, use the :option:`-d`
77-
flag. Note that *only* doctests will be refactored.
77+
flag. Note that *only* doctests will be refactored. This also doesn't require
78+
the module to be valid Python. For example, doctest like examples in a reST
79+
document could also be refactored with this option.
7880

7981
The :option:`-v` option enables the output of more information on the
8082
translation process.
@@ -95,4 +97,10 @@ true function call.
9597
.. moduleauthor:: Guido van Rossum
9698
.. moduleauthor:: Collin Winter
9799

100+
101+
.. warning::
102+
103+
The :mod:`lib2to3` API should be considered unstable and may change
104+
drastically in the future.
105+
98106
.. XXX What is the public interface anyway?

Doc/library/json.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ Encoders and decoders
370370
371371
def default(self, o):
372372
try:
373-
iterable = iter(o)
373+
iterable = iter(o)
374374
except TypeError:
375-
pass
375+
pass
376376
else:
377377
return list(iterable)
378378
return JSONEncoder.default(self, o)

Doc/library/optparse.rst

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,17 +1193,32 @@ traditional Unix exit status for command-line errors).
11931193
Querying and manipulating your option parser
11941194
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11951195

1196-
Sometimes, it's useful to poke around your option parser and see what's there.
1197-
OptionParser provides a couple of methods to help you out:
1198-
1199-
``has_option(opt_str)``
1200-
Return true if the OptionParser has an option with option string ``opt_str``
1201-
(e.g., ``"-q"`` or ``"--verbose"``).
1196+
The default behavior of the option parser can be customized slightly,
1197+
and you can also poke around your option parser and see what's there.
1198+
OptionParser provides several methods to help you out:
1199+
1200+
``disable_interspersed_args()``
1201+
Set parsing to stop on the first non-option. Use this if you have a
1202+
command processor which runs another command which has options of
1203+
its own and you want to make sure these options don't get
1204+
confused. For example, each command might have a different
1205+
set of options.
1206+
1207+
``enable_interspersed_args()``
1208+
Set parsing to not stop on the first non-option, allowing
1209+
interspersing switches with command arguments. For example,
1210+
``"-s arg1 --long arg2"`` would return ``["arg1", "arg2"]``
1211+
as the command arguments and ``-s, --long`` as options.
1212+
This is the default behavior.
12021213

12031214
``get_option(opt_str)``
12041215
Returns the Option instance with the option string ``opt_str``, or ``None`` if
12051216
no options have that option string.
12061217

1218+
``has_option(opt_str)``
1219+
Return true if the OptionParser has an option with option string ``opt_str``
1220+
(e.g., ``"-q"`` or ``"--verbose"``).
1221+
12071222
``remove_option(opt_str)``
12081223
If the OptionParser has an option corresponding to ``opt_str``, that option is
12091224
removed. If that option provided any other option strings, all of those option

Doc/library/os.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,13 @@ to be ignored.
12151215
These functions all execute a new program, replacing the current process; they
12161216
do not return. On Unix, the new executable is loaded into the current process,
12171217
and will have the same process id as the caller. Errors will be reported as
1218-
:exc:`OSError` exceptions.
1218+
:exc:`OSError` exceptions.
1219+
1220+
The current process is replaced immediately. Open file objects and
1221+
descriptors are not flushed, so if there may be data buffered
1222+
on these open files, you should flush them using
1223+
:func:`sys.stdout.flush` or :func:`os.fsync` before calling an
1224+
:func:`exec\*` function.
12191225

12201226
The "l" and "v" variants of the :func:`exec\*` functions differ in how
12211227
command-line arguments are passed. The "l" variants are perhaps the easiest
@@ -1241,8 +1247,9 @@ to be ignored.
12411247
used to define the environment variables for the new process (these are used
12421248
instead of the current process' environment); the functions :func:`execl`,
12431249
:func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
1244-
inherit the environment of the current process. Availability: Unix,
1245-
Windows.
1250+
inherit the environment of the current process.
1251+
1252+
Availability: Unix, Windows.
12461253

12471254

12481255
.. function:: _exit(n)

Doc/library/platform.rst

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,29 +226,23 @@ Unix Platforms
226226

227227
.. function:: dist(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...))
228228

229-
Tries to determine the name of the OS distribution name Returns a tuple
230-
``(distname, version, id)`` which defaults to the args given as parameters.
231-
232-
``supported_dists`` may be given to define the set of Linux
233-
distributions to look for. It defaults to a list of currently
234-
supported Linux distributions identified by their release file
235-
name.
229+
This is another name for :func:`linux_distribution`.
236230

237231
.. function:: linux_distribution(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...), full_distribution_name=1)
238232

239233
Tries to determine the name of the Linux OS distribution name.
240234

241-
``supported_dists`` may be given to define the set of Linux
242-
distributions to look for. It defaults to a list of currently
243-
supported Linux distributions identified by their release file
244-
name.
235+
``supported_dists`` may be given to define the set of Linux distributions to
236+
look for. It defaults to a list of currently supported Linux distributions
237+
identified by their release file name.
245238

246-
If ``full_distribution_name`` is true (default), the full
247-
distribution read from the OS is returned. Otherwise the short name
248-
taken from ``supported_dists`` is used.
239+
If ``full_distribution_name`` is true (default), the full distribution read
240+
from the OS is returned. Otherwise the short name taken from
241+
``supported_dists`` is used.
249242

250-
Returns a tuple ``(distname,version,id)`` which defaults to the
251-
args given as parameters.
243+
Returns a tuple ``(distname,version,id)`` which defaults to the args given as
244+
parameters. ``id`` is the item in parentheses after the version number. It
245+
is usually the version codename.
252246

253247
.. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=2048)
254248

Doc/library/site.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ and :file:`bar.pth` contains::
5959

6060
bar
6161

62-
Then the following directories are added to ``sys.path``, in this order::
62+
Then the following version-specific directories are added to
63+
``sys.path``, in this order::
6364

64-
/usr/local/lib/python3.0/site-packages/bar
65-
/usr/local/lib/python3.0/site-packages/foo
65+
/usr/local/lib/pythonX.Y/site-packages/bar
66+
/usr/local/lib/pythonX.Y/site-packages/foo
6667

6768
Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
6869
directory precedes the :file:`foo` directory because :file:`bar.pth` comes

0 commit comments

Comments
 (0)