Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdd4cd55

Browse files
committed
Python 3 support in PL/Python
Behaves more or less unchanged compared to Python 2, but the new languagevariant is called plpython3u. Documentation describing the naming schemeis included.
1 parent21d11e7 commitdd4cd55

File tree

15 files changed

+929
-41
lines changed

15 files changed

+929
-41
lines changed

‎config/python.m4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# Autoconf macros for configuring the build of Python extension modules
33
#
4-
# $PostgreSQL: pgsql/config/python.m4,v 1.16 2009/10/14 21:59:15 petere Exp $
4+
# $PostgreSQL: pgsql/config/python.m4,v 1.17 2009/12/15 22:59:53 petere Exp $
55
#
66

77
# PGAC_PATH_PYTHON
@@ -30,10 +30,12 @@ else
3030
AC_MSG_ERROR([distutils module not found])
3131
fi
3232
AC_MSG_CHECKING([Python configuration directory])
33+
python_majorversion=`${PYTHON} -c "import sys; print(sys.version[[0]])"`
3334
python_version=`${PYTHON} -c "import sys; print(sys.version[[:3]])"`
3435
python_configdir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib as f; import os; print(os.path.join(f(plat_specific=1,standard_lib=1),'config'))"`
3536
python_includespec=`${PYTHON} -c "import distutils.sysconfig; print('-I'+distutils.sysconfig.get_python_inc())"`
3637
38+
AC_SUBST(python_majorversion)[]dnl
3739
AC_SUBST(python_version)[]dnl
3840
AC_SUBST(python_configdir)[]dnl
3941
AC_SUBST(python_includespec)[]dnl

‎configure

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ python_libdir
677677
python_includespec
678678
python_configdir
679679
python_version
680+
python_majorversion
680681
PYTHON
681682
perl_embed_ldflags
682683
perl_useshrplib
@@ -6964,6 +6965,7 @@ $as_echo "$as_me: error: distutils module not found" >&2;}
69646965
fi
69656966
{ $as_echo "$as_me:$LINENO: checking Python configuration directory" >&5
69666967
$as_echo_n "checking Python configuration directory... " >&6; }
6968+
python_majorversion=`${PYTHON} -c "import sys; print(sys.version[0])"`
69676969
python_version=`${PYTHON} -c "import sys; print(sys.version[:3])"`
69686970
python_configdir=`${PYTHON} -c "from distutils.sysconfig import get_python_lib as f; import os; print(os.path.join(f(plat_specific=1,standard_lib=1),'config'))"`
69696971
python_includespec=`${PYTHON} -c "import distutils.sysconfig; print('-I'+distutils.sysconfig.get_python_inc())"`

‎doc/src/sgml/installation.sgml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/installation.sgml,v 1.332 2009/12/09 16:16:34 mha Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/installation.sgml,v 1.333 2009/12/15 22:59:53 petere Exp $ -->
22

33
<chapter id="installation">
44
<title><![%standalone-include[<productname>PostgreSQL</>]]>
@@ -195,8 +195,12 @@ su - postgres
195195
<para>
196196
To build the <application>PL/Python</> server programming
197197
language, you need a <productname>Python</productname>
198-
installation with the header files and the <application>distutils</application> module.
199-
The minimum required version is <productname>Python</productname> 2.2.
198+
installation with the header files and
199+
the <application>distutils</application> module. The minimum
200+
required version is <productname>Python</productname>
201+
2.2. <productname>Python 3</productname> is supported with
202+
version 3.1 or later; but see <xref linkend="plpython-python23">
203+
when using Python 3.
200204
</para>
201205

202206
<para>

‎doc/src/sgml/plpython.sgml

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.41 2009/12/10 20:43:40 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.42 2009/12/15 22:59:53 petere Exp $ -->
22

33
<chapter id="plpython">
44
<title>PL/Python - Python Procedural Language</title>
@@ -14,7 +14,8 @@
1414

1515
<para>
1616
To install PL/Python in a particular database, use
17-
<literal>createlang plpythonu <replaceable>dbname</></literal>.
17+
<literal>createlang plpythonu <replaceable>dbname</></literal> (but
18+
see also <xref linkend="plpython-python23">).
1819
</para>
1920

2021
<tip>
@@ -42,6 +43,112 @@
4243
</para>
4344
</note>
4445

46+
<sect1 id="plpython-python23">
47+
<title>Python 2 vs. Python 3</title>
48+
49+
<para>
50+
PL/Python supports both the Python 2 and Python 3 language
51+
variants. (The PostgreSQL installation instructions might contain
52+
more precise information about the exact supported minor versions
53+
of Python.) Because the Python 2 and Python 3 language variants
54+
are incompatible in some important aspects, the following naming
55+
and transitioning scheme is used by PL/Python to avoid mixing them:
56+
57+
<itemizedlist>
58+
<listitem>
59+
<para>
60+
The PostgreSQL language named <literal>plpython2u</literal>
61+
implements PL/Python based on the Python 2 language variant.
62+
</para>
63+
</listitem>
64+
65+
<listitem>
66+
<para>
67+
The PostgreSQL language named <literal>plpython3u</literal>
68+
implements PL/Python based on the Python 3 language variant.
69+
</para>
70+
</listitem>
71+
72+
<listitem>
73+
<para>
74+
The language named <literal>plpythonu</literal> implements
75+
PL/Python based on the default Python language variant, which is
76+
currently Python 2. (This default is independent of what any
77+
local Python installations might consider to be
78+
their <quote>default</quote>, for example,
79+
what <filename>/usr/bin/python</filename> might be.) The
80+
default will probably be changed to Python 3 in a distant future
81+
release of PostgreSQL, depending on the progress of the
82+
migration to Python 3 in the Python community.
83+
</para>
84+
</listitem>
85+
</itemizedlist>
86+
87+
It depends on the build configuration or the installed packages
88+
whether PL/Python for Python 2 or Python 3 or both are available.
89+
</para>
90+
91+
<para>
92+
This results in the following usage and migration strategy:
93+
94+
<itemizedlist>
95+
<listitem>
96+
<para>
97+
Existing users and users who are currently not interested in
98+
Python 3 use the language name <literal>plpythonu</literal> and
99+
don't have to change anything for the foreseeable future. It is
100+
recommended to gradually <quote>future-proof</quote> the code
101+
via migration to Python 2.6/2.7 to simplify the eventual
102+
migration to Python 3.
103+
</para>
104+
105+
<para>
106+
In practice, many PL/Python functions will migrate to Python 3
107+
with few or no changes.
108+
</para>
109+
</listitem>
110+
111+
<listitem>
112+
<para>
113+
Users who know that they have heavily Python 2 dependent code
114+
and don't plan to ever change it can make use of
115+
the <literal>plpython2u</literal> language name. This will
116+
continue to work into the very distant future, until Python 2
117+
support might be completely dropped by PostgreSQL.
118+
</para>
119+
</listitem>
120+
121+
<listitem>
122+
<para>
123+
Users who want to dive into Python 3 can use
124+
the <literal>plpython3u</literal> language name, which will keep
125+
working forever by today's standards. In the distant future,
126+
when Python 3 might become the default, they might like to
127+
remove the <quote>3</quote> for aesthetic reasons.
128+
</para>
129+
</listitem>
130+
131+
<listitem>
132+
<para>
133+
Daredevils, who want to build a Python-3-only operating system
134+
environment, can change the build scripts to
135+
make <literal>plpythonu</literal> be equivalent
136+
to <literal>plpython3u</literal>, keeping in mind that this
137+
would make their installation incompatible with most of the rest
138+
of the world.
139+
</para>
140+
</listitem>
141+
</itemizedlist>
142+
</para>
143+
144+
<para>
145+
See also the
146+
document <ulink url="http://docs.python.org/dev/3.0/whatsnew/3.0.html">What's
147+
New In Python 3.0</ulink> for more information about porting to
148+
Python 3.
149+
</para>
150+
</sect1>
151+
45152
<sect1 id="plpython-funcs">
46153
<title>PL/Python Functions</title>
47154

‎src/Makefile.global.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*-makefile-*-
2-
# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.259 2009/11/03 21:28:10 petere Exp $
2+
# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.260 2009/12/15 22:59:54 petere Exp $
33

44
#------------------------------------------------------------------------------
55
# All PostgreSQL makefiles include this file and use the variables it sets,
@@ -171,6 +171,7 @@ python_libdir= @python_libdir@
171171
python_libspec= @python_libspec@
172172
python_additional_libs= @python_additional_libs@
173173
python_configdir= @python_configdir@
174+
python_majorversion= @python_majorversion@
174175
python_version= @python_version@
175176

176177
krb_srvtab = @krb_srvtab@

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.558 2009/12/1517:57:47 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.559 2009/12/1522:59:54 petere Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200912151
56+
#defineCATALOG_VERSION_NO200912161
5757

5858
#endif

‎src/include/catalog/pg_pltemplate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_pltemplate.h,v 1.9 2009/11/29 03:02:27 tgl Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_pltemplate.h,v 1.10 2009/12/15 22:59:54 petere Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -73,5 +73,7 @@ DATA(insert ( "pltclu"f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl"
7373
DATA(insert ("plperl"tt"plperl_call_handler""plperl_inline_handler""plperl_validator""$libdir/plperl"_null_ ));
7474
DATA(insert ("plperlu"ff"plperl_call_handler""plperl_inline_handler""plperl_validator""$libdir/plperl"_null_ ));
7575
DATA(insert ("plpythonu"ff"plpython_call_handler"_null__null_"$libdir/plpython"_null_ ));
76+
DATA(insert ("plpython2u"ff"plpython_call_handler"_null__null_"$libdir/plpython2"_null_ ));
77+
DATA(insert ("plpython3u"ff"plpython_call_handler"_null__null_"$libdir/plpython3"_null_ ));
7678

7779
#endif/* PG_PLTEMPLATE_H */

‎src/pl/plpython/Makefile

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $PostgreSQL: pgsql/src/pl/plpython/Makefile,v 1.34 2009/08/14 13:42:16 petere Exp $
1+
# $PostgreSQL: pgsql/src/pl/plpython/Makefile,v 1.35 2009/12/15 22:59:54 petere Exp $
22

33
subdir = src/pl/plpython
44
top_builddir = ../../..
@@ -36,7 +36,7 @@ override CPPFLAGS := -I$(srcdir) $(python_includespec) $(CPPFLAGS)
3636

3737
rpathdir =$(python_libdir)
3838

39-
NAME = plpython
39+
NAME = plpython$(python_majorversion)
4040
OBJS = plpython.o
4141

4242

@@ -56,7 +56,12 @@ endif
5656

5757
SHLIB_LINK =$(python_libspec)$(python_additional_libs)$(filter -lintl,$(LIBS))
5858

59-
REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-language=plpythonu
59+
REGRESS_OPTS = --dbname=$(PL_TESTDB)
60+
# Only load plpythonu with Python 2. The test files themselves load
61+
# the versioned language plpython(2|3)u.
62+
ifeq ($(python_majorversion),2)
63+
REGRESS_OPTS += --load-language=plpythonu
64+
endif
6065
REGRESS =\
6166
plpython_schema\
6267
plpython_populate\
@@ -83,13 +88,45 @@ include $(top_srcdir)/src/Makefile.shlib
8388
all: all-lib
8489

8590
install: all installdirs install-lib
91+
ifeq ($(python_majorversion),2)
92+
cd '$(DESTDIR)$(pkglibdir)' && rm -f plpython$(DLSUFFIX) && $(LN_S) $(shlib) plpython$(DLSUFFIX)
93+
endif
8694

8795
installdirs: installdirs-lib
8896

8997
uninstall: uninstall-lib
98+
ifeq ($(python_majorversion),2)
99+
rm -f '$(DESTDIR)$(pkglibdir)/plpython$(DLSUFFIX)'
100+
endif
90101

102+
ifeq ($(python_majorversion),3)
103+
# Adjust regression tests for Python 3 compatibility
104+
prep3:
105+
$(MKDIR_P) python3 python3/sql python3/expected
106+
forfilein$(srcdir)/sql/*.sql$(srcdir)/expected/*.out;do \
107+
sed -e's/except \([[:alpha:]][[:alpha:].]*\), *\([[:alpha:]][[:alpha:]]*\):/except \1 as \2:/g'\
108+
-e"s/<type 'exceptions\.\([[:alpha:]]*\)'>/<class '\1'>/g"\
109+
-e"s/<type 'long'>/<class 'int'>/g"\
110+
-e"s/\([0-9][0-9]*\)L/\1/g"\
111+
-e's/\([ [{]\)u"/\1"/g'\
112+
-e"s/\([ [{]\)u'/\1'/g"\
113+
-e"s/def next/def __next__/g"\
114+
-e"s/LANGUAGE plpythonu/LANGUAGE plpython3u/g"\
115+
-e"s/LANGUAGE plpython2u/LANGUAGE plpython3u/g"\
116+
$$file>`echo$$file| sed's,$(srcdir),python3,'`;\
117+
done
118+
119+
clean3:
120+
rm -rf python3/
121+
122+
installcheck: submake prep3
123+
$(top_builddir)/src/test/regress/pg_regress --inputdir=./python3 --outputdir=./python3 --psqldir=$(PSQLDIR)$(REGRESS_OPTS)$(REGRESS)
124+
125+
clean: clean3
126+
else
91127
installcheck: submake
92128
$(top_builddir)/src/test/regress/pg_regress --inputdir=$(srcdir) --psqldir=$(PSQLDIR)$(REGRESS_OPTS)$(REGRESS)
129+
endif
93130

94131
.PHONY: submake
95132
submake:

‎src/pl/plpython/expected/README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ plpython_unicode_0.outany version, when server encoding != SQL_ASCII and clien
88
plpython_unicode_2.outPython 2.2
99
plpython_unicode_3.outPython 2.3, 2.4
1010
plpython_unicode_5.outPython 2.5, 2.6
11+
12+
plpython_types_3.outPython 3.1

‎src/pl/plpython/expected/plpython_test.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- first some tests of basic functionality
2+
CREATE LANGUAGE plpython2u;
23
-- really stupid function just to get the module loaded
34
CREATE FUNCTION stupid() RETURNS text AS 'return "zarkon"' LANGUAGE plpythonu;
45
select stupid();
@@ -7,6 +8,14 @@ select stupid();
78
zarkon
89
(1 row)
910

11+
-- check 2/3 versioning
12+
CREATE FUNCTION stupidn() RETURNS text AS 'return "zarkon"' LANGUAGE plpython2u;
13+
select stupidn();
14+
stupidn
15+
---------
16+
zarkon
17+
(1 row)
18+
1019
-- test multiple arguments
1120
CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
1221
AS

‎src/pl/plpython/expected/plpython_trigger.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ SELECT * FROM users;
6767
-- dump trigger data
6868
CREATE TABLE trigger_test
6969
(i int, v text );
70-
CREATE FUNCTION trigger_data()returns triggerlanguage plpythonuas $$
70+
CREATE FUNCTION trigger_data()RETURNS triggerLANGUAGE plpythonuAS $$
7171

7272
if 'relid' in TD:
7373
TD['relid'] = "bogus:12345"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp