gnu: Python: Fix CVE-2020-26116.
[jackhill/guix/guix.git] / gnu / packages / python.scm
CommitLineData
a01b6da7
NK
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
a5e3d59b 3;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
8e451885 4;;; Copyright © 2013, 2014, 2015, 2016 Andreas Enge <andreas@enge.fr>
e99f4211 5;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
32922190 6;;; Copyright © 2014, 2017, 2019 Eric Bavier <bavier@member.fsf.org>
da6ce3f1 7;;; Copyright © 2014, 2015 Federico Beffa <beffa@fbengineering.ch>
e99f4211 8;;; Copyright © 2015 Omar Radwan <toxemicsquire4@gmail.com>
d95a56c6 9;;; Copyright © 2015 Pierre-Antoine Rault <par@rigelk.eu>
9fb5771a 10;;; Copyright © 2015, 2016, 2017, 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
0bdc1671 11;;; Copyright © 2015, 2016 Christopher Allan Webber <cwebber@dustycloud.org>
345f0611 12;;; Copyright © 2015 Eric Dvorsak <eric@dvorsak.fr>
2b2f2fc1 13;;; Copyright © 2015, 2016 David Thompson <davet@gnu.org>
7637de23 14;;; Copyright © 2015, 2016, 2017 Leo Famulari <leo@famulari.name>
04fb4516 15;;; Copyright © 2015, 2017 Ben Woodcroft <donttrustben@gmail.com>
b9893908 16;;; Copyright © 2015, 2016 Erik Edrosa <erik.edrosa@gmail.com>
399d89b5 17;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Efraim Flashner <efraim@flashner.co.il>
b89e3c77 18;;; Copyright © 2015, 2017 Kyle Meyer <kyle@kyleam.com>
daeeea71 19;;; Copyright © 2015, 2016 Chris Marusich <cmmarusich@gmail.com>
b31fbea5 20;;; Copyright © 2016 Danny Milosavljevic <dannym+a@scratchpost.org>
88bb4197 21;;; Copyright © 2016 Lukas Gradl <lgradl@openmailbox.org>
34581f68 22;;; Copyright © 2016, 2018 Hartmut Goebel <h.goebel@crazy-compilers.com>
0de78c95 23;;; Copyright © 2016 Daniel Pimentel <d4n1@d4n1.org>
9250b0f3 24;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
bb3b4b4e 25;;; Copyright © 2016, 2017 Troy Sankey <sankeytms@gmail.com>
3c986a7d 26;;; Copyright © 2016, 2017 Nikita <nikita@n0.is>
6eb7af2a 27;;; Copyright © 2016 Dylan Jeffers <sapientech@sapientech@openmailbox.org>
3f641af0 28;;; Copyright © 2016 David Craven <david@craven.ch>
67ca82e6 29;;; Copyright © 2016, 2017, 2018, 2019, 2020 Marius Bakke <mbakke@fastmail.com>
ca0e62bf 30;;; Copyright © 2016, 2017 Stefan Reichör <stefan@xsteve.at>
b04a52a6 31;;; Copyright © 2016 Dylan Jeffers <sapientech@sapientech@openmailbox.org>
e65acb31 32;;; Copyright © 2016, 2017 Alex Vong <alexvong1995@gmail.com>
fbec83b8 33;;; Copyright © 2016, 2017, 2018 Arun Isaac <arunisaac@systemreboot.net>
ff349415 34;;; Copyright © 2016, 2017, 2018 Julien Lepiller <julien@lepiller.eu>
9e8401a5 35;;; Copyright © 2016, 2017, 2018 Tobias Geerinckx-Rice <me@tobias.gr>
ec074213 36;;; Copyright © 2016, 2017 Thomas Danckaert <post@thomasdanckaert.be>
916aafa4 37;;; Copyright © 2017 Carlo Zancanaro <carlo@zancanaro.id.au>
1a917fc9 38;;; Copyright © 2017 Frederick M. Muriithi <fredmanglis@gmail.com>
09b9cf73 39;;; Copyright © 2017, 2018 Adriano Peluso <catonano@gmail.com>
16fe516e 40;;; Copyright © 2017 Ben Sturmfels <ben@sturm.com.au>
57ec9861 41;;; Copyright © 2017, 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
3015d455 42;;; Copyright © 2017 José Miguel Sánchez García <jmi2k@openmailbox.org>
fa3f5f47 43;;; Copyright © 2017 Roel Janssen <roel@gnu.org>
44e4a621 44;;; Copyright © 2017, 2018 Kei Kebreau <kkebreau@posteo.net>
f817b4d9 45;;; Copyright © 2017 Rutger Helling <rhelling@mykolab.com>
3fee0039 46;;; Copyright © 2017 Muriithi Frederick Muriuki <fredmanglis@gmail.com>
dcfa0acd 47;;; Copyright © 2017 Brendan Tildesley <mail@brendan.scot>
956ba99c 48;;; Copyright © 2018 Ethan R. Jones <ethanrjones97@gmail.com
6055e88c 49;;; Copyright © 2018 Fis Trivial <ybbs.daans@hotmail.com>
385e8614 50;;; Copyright © 2018 Vijayalakshmi Vedantham <vijimay12@gmail.com>
95f3b626 51;;; Copyright © 2018 Mathieu Lirzin <mthl@gnu.org>
c2b2f0c8 52;;; Copyright © 2018 Adam Massmann <massmannak@gmail.com>
15ed90b9 53;;; Copyright © 2016, 2018 Tomáš Čech <sleep_walker@gnu.org>
bbc8c36f 54;;; Copyright © 2018 Nicolas Goaziou <mail@nicolasgoaziou.fr>
c0888cef 55;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
0dc57238 56;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
e337061b 57;;; Copyright © 2018, 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
16d21251 58;;; Copyright © 2018 Luther Thompson <lutheroto@gmail.com>
9f23f6bc 59;;; Copyright © 2018 Vagrant Cascadian <vagrant@debian.org>
af6a9fc2 60;;; Copyright © 2019 Tanguy Le Carrour <tanguy@bioneland.org>
189bfb55 61;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
c2b2f0c8 62;;;
a01b6da7
NK
63;;; This file is part of GNU Guix.
64;;;
65;;; GNU Guix is free software; you can redistribute it and/or modify it
66;;; under the terms of the GNU General Public License as published by
67;;; the Free Software Foundation; either version 3 of the License, or (at
68;;; your option) any later version.
69;;;
70;;; GNU Guix is distributed in the hope that it will be useful, but
71;;; WITHOUT ANY WARRANTY; without even the implied warranty of
72;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
73;;; GNU General Public License for more details.
74;;;
75;;; You should have received a copy of the GNU General Public License
76;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
77
1ffa7090 78(define-module (gnu packages python)
3f641af0 79 #:use-module ((guix licenses) #:prefix license:)
3fdc99da 80 #:use-module (gnu packages)
680bc34d 81 #:use-module (gnu packages base)
3969ca54 82 #:use-module (gnu packages bash)
625cdb05
LDB
83 #:use-module (gnu packages certs)
84 #:use-module (gnu packages check)
1ffa7090 85 #:use-module (gnu packages compression)
255d1bbe 86 #:use-module (gnu packages dbm)
189bfb55 87 #:use-module (gnu packages hurd)
b10ab723 88 #:use-module (gnu packages libffi)
625cdb05 89 #:use-module (gnu packages ncurses)
b10ab723 90 #:use-module (gnu packages pkg-config)
625cdb05 91 #:use-module (gnu packages python-xyz)
4ed20663 92 #:use-module (gnu packages readline)
625cdb05 93 #:use-module (gnu packages shells)
cd0322a3 94 #:use-module (gnu packages sqlite)
afa181ff 95 #:use-module (gnu packages tcl)
44d10b1f 96 #:use-module (gnu packages tls)
d1659c0f 97 #:use-module (gnu packages xml)
a01b6da7
NK
98 #:use-module (guix packages)
99 #:use-module (guix download)
11bb85a1 100 #:use-module (guix utils)
acc26ff1 101 #:use-module (guix build-system gnu)
67ca82e6
MB
102 #:use-module (guix build-system trivial)
103 #:use-module (srfi srfi-1)
104 #:use-module (srfi srfi-26))
a01b6da7 105
45848023 106(define-public python-2.7
a01b6da7 107 (package
a7714d42 108 (name "python2")
2c854762 109 (version "2.7.17")
a01b6da7
NK
110 (source
111 (origin
112 (method url-fetch)
9b43a0ff 113 (uri (string-append "https://www.python.org/ftp/python/"
a01b6da7
NK
114 version "/Python-" version ".tar.xz"))
115 (sha256
116 (base32
2c854762 117 "0hds28cg226m8j8sr394nm9yc4gxhvlv109w0avsf2mxrlrz0hsd"))
c3052d6b
ML
118 (patches (search-patches "python-2.7-search-paths.patch"
119 "python-2-deterministic-build-info.patch"
15e57f57 120 "python-2.7-site-prefixes.patch"
dc5cec21 121 "python-2.7-source-date-epoch.patch"
ceb9de75
MO
122 "python-2.7-adjust-tests.patch"
123 "python-cross-compile.patch"))
10a42aa2 124 (modules '((guix build utils)))
10a42aa2
EF
125 (snippet
126 '(begin
d1659c0f
MB
127 ;; Ensure the bundled copies of these libraries are not used.
128 (for-each delete-file-recursively
129 '("Modules/_ctypes/libffi" "Modules/expat" "Modules/zlib"))
130
131 (substitute* "Modules/Setup.dist"
132 ;; Link Expat instead of embedding the bundled one.
133 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
134
135 ;; Suboptimal to delete failing tests here, but if we delete them in
136 ;; the arguments then we need to make sure to strip out that phase
137 ;; when it gets inherited by python and python-minimal.
10a42aa2
EF
138 (for-each delete-file
139 '("Lib/test/test_compileall.py"
f0499100 140 "Lib/test/test_ctypes.py" ; fails on mips64el
10a42aa2
EF
141 "Lib/test/test_distutils.py"
142 "Lib/test/test_import.py"
143 "Lib/test/test_shutil.py"
144 "Lib/test/test_socket.py"
1bb163b0 145 "Lib/test/test_subprocess.py"))
10a42aa2 146 #t))))
02f0c3b2
LC
147 (outputs '("out"
148 "tk")) ;tkinter; adds 50 MiB to the closure
a01b6da7 149 (build-system gnu-build-system)
3fdc99da 150 (arguments
d6614607 151 `(#:test-target "test"
3fdc99da 152 #:configure-flags
6a20289d 153 (list "--enable-shared" ;allow embedding
d1659c0f 154 "--with-system-expat" ;for XML support
6a20289d 155 "--with-system-ffi" ;build ctypes
aaf75c89 156 "--with-ensurepip=install" ;install pip and setuptools
a282d7ff 157 "--enable-unicode=ucs4"
fcb54937
MB
158
159 ;; Prevent the installed _sysconfigdata.py from retaining a reference
160 ;; to coreutils.
161 "INSTALL=install -c"
162 "MKDIR_P=mkdir -p"
163
57ec9861
MO
164 ;; Disable runtime check failing if cross-compiling, see:
165 ;; https://lists.yoctoproject.org/pipermail/poky/2013-June/008997.html
166 ,@(if (%current-target-system)
167 '("ac_cv_buggy_getaddrinfo=no"
168 "ac_cv_file__dev_ptmx=no"
169 "ac_cv_file__dev_ptc=no")
170 '())
6a20289d
LC
171 (string-append "LDFLAGS=-Wl,-rpath="
172 (assoc-ref %outputs "out") "/lib"))
5b01b603
EB
173 ;; With no -j argument tests use all available cpus, so provide one.
174 #:make-flags
e337061b
MC
175 (list (string-append
176 (format #f "TESTOPTS=-j~d" (parallel-job-count))
177 ;; Exclude the following tests as they fail
178 ;; non-deterministically with "error: [Errno 104] Connection
179 ;; reset by peer." Python 3 seems unaffected. A potential fix,
180 ;; yet to be backported to Python 2, is available at:
181 ;; https://github.com/python/cpython/commit/529525fb5a8fd9b96ab4021311a598c77588b918.
182 " --exclude test_urllib2_localnet test_httplib"))
fd982732 183
d2cc9c7c
LC
184 #:modules ((ice-9 ftw) (ice-9 match)
185 (guix build utils) (guix build gnu-build-system))
fd982732 186 #:phases
46472ecd
MW
187 (modify-phases %standard-phases
188 (add-before
189 'configure 'patch-lib-shells
190 (lambda _
ceb9de75
MO
191 ;; This variable is used in setup.py to enable cross compilation
192 ;; specific switches. As it is not set properly by configure
193 ;; script, set it manually.
194 ,@(if (%current-target-system)
195 '((setenv "_PYTHON_HOST_PLATFORM" ""))
196 '())
46472ecd
MW
197 ;; Filter for existing files, since some may not exist in all
198 ;; versions of python that are built with this recipe.
199 (substitute* (filter file-exists?
200 '("Lib/subprocess.py"
201 "Lib/popen2.py"
202 "Lib/distutils/tests/test_spawn.py"
b18226c3 203 "Lib/test/support/__init__.py"
46472ecd
MW
204 "Lib/test/test_subprocess.py"))
205 (("/bin/sh") (which "sh")))
206 #t))
189bfb55
JN
207 ,@(if (hurd-system?)
208 `((add-before 'build 'patch-regen-for-hurd
209 (lambda* (#:key inputs #:allow-other-keys)
210 (let ((libc (assoc-ref inputs "libc")))
211 (substitute* "Lib/plat-generic/regen"
212 (("/usr/include/") (string-append libc "/include/")))
213 #t))))
214 '())
5b4e2791
LC
215 (add-before 'configure 'do-not-record-configure-flags
216 (lambda* (#:key configure-flags #:allow-other-keys)
217 ;; Remove configure flags from the installed '_sysconfigdata.py'
218 ;; and 'Makefile' so we don't end up keeping references to the
219 ;; build tools.
220 ;;
221 ;; Preserve at least '--with-system-ffi' since otherwise the
222 ;; thing tries to build libffi, fails, and we end up with a
223 ;; Python that lacks ctypes.
224 (substitute* "configure"
225 (("^CONFIG_ARGS=.*$")
226 (format #f "CONFIG_ARGS='~a'\n"
227 (if (member "--with-system-ffi" configure-flags)
228 "--with-system-ffi"
229 ""))))
230 #t))
46472ecd
MW
231 (add-before
232 'check 'pre-check
233 (lambda _
234 ;; 'Lib/test/test_site.py' needs a valid $HOME
235 (setenv "HOME" (getcwd))
236 #t))
237 (add-after
238 'unpack 'set-source-file-times-to-1980
239 ;; XXX One of the tests uses a ZIP library to pack up some of the
240 ;; source tree, and fails with "ZIP does not support timestamps
241 ;; before 1980". Work around this by setting the file times in the
242 ;; source tree to sometime in early 1980.
243 (lambda _
244 (let ((circa-1980 (* 10 366 24 60 60)))
245 (ftw "." (lambda (file stat flag)
246 (utime file circa-1980 circa-1980)
247 #t))
02f0c3b2 248 #t)))
9ffe61b0
LC
249 (add-after 'install 'remove-tests
250 ;; Remove 25 MiB of unneeded unit tests. Keep test_support.*
251 ;; because these files are used by some libraries out there.
252 (lambda* (#:key outputs #:allow-other-keys)
253 (let ((out (assoc-ref outputs "out")))
254 (match (scandir (string-append out "/lib")
255 (lambda (name)
256 (string-prefix? "python" name)))
257 ((pythonX.Y)
258 (let ((testdir (string-append out "/lib/" pythonX.Y
259 "/test")))
260 (with-directory-excursion testdir
261 (for-each delete-file-recursively
262 (scandir testdir
263 (match-lambda
264 ((or "." "..") #f)
d453b0e1 265 ("support" #f)
9ffe61b0
LC
266 (file
267 (not
268 (string-prefix? "test_support."
269 file))))))
270 (call-with-output-file "__init__.py" (const #t))
271 #t)))))))
d41f6394
RW
272 (add-after 'remove-tests 'rebuild-bytecode
273 (lambda* (#:key outputs #:allow-other-keys)
274 (let ((out (assoc-ref outputs "out")))
275 ;; Disable hash randomization to ensure the generated .pycs
276 ;; are reproducible.
277 (setenv "PYTHONHASHSEED" "0")
278 (for-each
279 (lambda (opt)
280 (format #t "Compiling with optimization level: ~a\n"
281 (if (null? opt) "none" (car opt)))
282 (for-each (lambda (file)
283 (apply invoke
ceb9de75
MO
284 `(,,(if (%current-target-system)
285 "python2"
286 '(string-append out "/bin/python"))
d41f6394
RW
287 ,@opt
288 "-m" "compileall"
289 "-f" ; force rebuild
290 ;; Don't build lib2to3, because it contains Python 3 code.
291 "-x" "lib2to3/.*"
292 ,file)))
293 (find-files out "\\.py$")))
294 (list '() '("-O") '("-OO")))
295 #t)))
02f0c3b2
LC
296 (add-after 'install 'move-tk-inter
297 (lambda* (#:key outputs #:allow-other-keys)
298 ;; When Tkinter support is built move it to a separate output so
299 ;; that the main output doesn't contain a reference to Tcl/Tk.
300 (let ((out (assoc-ref outputs "out"))
301 (tk (assoc-ref outputs "tk")))
302 (when tk
303 (match (find-files out "tkinter.*\\.so")
304 ((tkinter.so)
305 ;; The .so is in OUT/lib/pythonX.Y/lib-dynload, but we
306 ;; want it under TK/lib/pythonX.Y/site-packages.
307 (let* ((len (string-length out))
308 (target (string-append
309 tk "/"
310 (string-drop
311 (dirname (dirname tkinter.so))
312 len)
313 "/site-packages")))
314 (install-file tkinter.so target)
315 (delete-file tkinter.so)))))
316 #t))))))
a01b6da7 317 (inputs
3fdc99da 318 `(("bzip2" ,bzip2)
d1659c0f 319 ("expat" ,expat)
3fdc99da 320 ("gdbm" ,gdbm)
b10ab723 321 ("libffi" ,libffi) ; for ctypes
b88e1b0a 322 ("sqlite" ,sqlite) ; for sqlite extension
a01b6da7 323 ("openssl" ,openssl)
3fdc99da 324 ("readline" ,readline)
afa181ff
LC
325 ("zlib" ,zlib)
326 ("tcl" ,tcl)
327 ("tk" ,tk))) ; for tkinter
b10ab723 328 (native-inputs
57ec9861
MO
329 `(("pkg-config" ,pkg-config)
330 ;; When cross-compiling, a native version of Python itself is needed.
331 ,@(if (%current-target-system)
ceb9de75 332 `(("python2" ,this-package)
57ec9861
MO
333 ("which" ,which))
334 '())))
9be8d7c8
LC
335 (native-search-paths
336 (list (search-path-specification
337 (variable "PYTHONPATH")
af070955 338 (files '("lib/python2.7/site-packages")))))
f0499100 339 (home-page "https://www.python.org")
afa181ff 340 (synopsis "High-level, dynamically-typed programming language")
a01b6da7
NK
341 (description
342 "Python is a remarkably powerful dynamic programming language that
343is used in a wide variety of application domains. Some of its key
344distinguishing features include: clear, readable syntax; strong
345introspection capabilities; intuitive object orientation; natural
346expression of procedural code; full modularity, supporting hierarchical
347packages; exception-based error handling; and very high level dynamic
348data types.")
cb13618b 349 (properties '((cpe-name . "python")))
3f641af0 350 (license license:psfl)))
acc26ff1 351
45848023
HG
352;; Current 2.x version.
353(define-public python-2 python-2.7)
354
b63f6c01
LC
355(define-public python2-called-python
356 ;; Both 2.x and 3.x used to be called "python". In commit
357 ;; a7714d42de2c3082f3609d1e63c83d703fb39cf9 (March 2018), we renamed the
358 ;; Python 2.x package to "python2".
359 (package
360 (inherit python-2)
361 (name "python")
362 (properties `((superseded . ,python-2)))))
363
b15d1c46 364(define-public python-3.8
b24d1cfc 365 (package (inherit python-2)
b4d0cf40 366 (name "python")
53453242 367 (version "3.8.2")
717003e3
LC
368 (source (origin
369 (method url-fetch)
370 (uri (string-append "https://www.python.org/ftp/python/"
371 version "/Python-" version ".tar.xz"))
3cff86db 372 (patches (search-patches
f936a300 373 "python-CVE-2020-26116.patch"
3cff86db
MB
374 "python-3-fix-tests.patch"
375 "python-3.8-fix-tests.patch"
376 "python-3-deterministic-build-info.patch"
377 "python-3-search-paths.patch"))
717003e3
LC
378 (sha256
379 (base32
53453242 380 "1ps5v323cp5czfshqjmbsqw7nvrdpcbk06f62jbzaqik4gfffii6"))
d1659c0f 381 (modules '((guix build utils)))
fd615918 382 (snippet
e46e9573 383 '(begin
d1659c0f
MB
384 ;; Delete the bundled copy of libexpat.
385 (delete-file-recursively "Modules/expat")
b15d1c46 386 (substitute* "Modules/Setup"
d1659c0f
MB
387 ;; Link Expat instead of embedding the bundled one.
388 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
e46e9573 389 #t))))
9b7e389d
RW
390 (arguments
391 (substitute-keyword-arguments (package-arguments python-2)
e337061b 392 ((#:make-flags _)
a5e9413c
MC
393 `(list (string-append
394 (format #f "TESTOPTS=-j~d" (parallel-job-count))
680bc34d
MB
395 ;; test_mmap fails on low-memory systems.
396 " --exclude test_mmap"
397 ;; test_socket may hang and eventually run out of memory
398 ;; on some systems: <https://bugs.python.org/issue34587>.
e6320ba2
JN
399 " test_socket"
400 ,@(if (hurd-target?)
401 '(" test_posix" ;multiple errors
402 " test_time"
403 " test_pty"
404 " test_shutil"
405 " test_tempfile" ;chflags: invalid argument:
406 ; tbv14c9t/dir0/dir0/dir0/test0.txt
407 " test_asyncio" ;runs over 10min
408 " test_os" ;stty: 'standard input':
409 ; Inappropriate ioctl for device
410 " test_openpty" ;No such file or directory
411 " test_selectors" ;assertEqual(NUM_FDS // 2, len(fds))
412 ; 32752 != 4
413 " test_compileall" ;multiple errors
414 " test_poll" ;list index out of range
415 " test_subprocess" ;runs over 10min
416 " test_asyncore" ;multiple errors
417 " test_threadsignals"
418 " test_eintr" ;Process return code is -14
419 " test_io" ;multiple errors
420 " test_logging"
421 " test_signal"
422 " test_threading" ;runs over 10min
423 " test_flags" ;ERROR
424 " test_bidirectional_pty"
425 " test_create_unix_connection"
426 " test_unix_sock_client_ops"
427 " test_open_unix_connection"
428 " test_open_unix_connection_error"
429 " test_read_pty_output"
430 " test_write_pty")
431 '()))))
9b7e389d 432 ((#:phases phases)
a1454169 433 `(modify-phases ,phases
189bfb55
JN
434 ,@(if (hurd-system?)
435 `((delete 'patch-regen-for-hurd)) ;regen was removed after 3.5.9
436 '())
c8a4e59a
MB
437 ,@(if (hurd-target?)
438 ;; The build system refuses to cross-compile for unknown targets
439 ;; even though it works fine. Add GNU/Hurd target.
440 ;; TODO: Make it a patch in a future rebuild cycle.
441 '((add-before 'configure 'support-hurd-cross-compile
442 (lambda _
443 (substitute* "configure"
444 (("\\*-\\*-vxworks.*" all)
445 (string-append "*-*-gnu)\nac_sys_system=GNU\n;;\n" all)))
446 #t)))
447 '())
680bc34d 448 (add-before 'check 'set-TZDIR
ceb9de75 449 (lambda* (#:key inputs native-inputs #:allow-other-keys)
680bc34d
MB
450 ;; test_email requires the Olson time zone database.
451 (setenv "TZDIR"
ceb9de75
MO
452 (string-append (assoc-ref
453 (or native-inputs inputs) "tzdata")
680bc34d
MB
454 "/share/zoneinfo"))
455 #t))
a1454169
MB
456 ;; Unset SOURCE_DATE_EPOCH while running the test-suite and set it
457 ;; again afterwards. See <https://bugs.python.org/issue34022>.
458 (add-before 'check 'unset-SOURCE_DATE_EPOCH
459 (lambda _ (unsetenv "SOURCE_DATE_EPOCH") #t))
460 (add-after 'check 'reset-SOURCE_DATE_EPOCH
461 (lambda _ (setenv "SOURCE_DATE_EPOCH" "1") #t))
d41f6394 462 (replace 'rebuild-bytecode
9b7e389d 463 (lambda* (#:key outputs #:allow-other-keys)
9b7e389d 464 (let ((out (assoc-ref outputs "out")))
a422e247
MB
465 ;; Disable hash randomization to ensure the generated .pycs
466 ;; are reproducible.
467 (setenv "PYTHONHASHSEED" "0")
9b7e389d
RW
468 (for-each
469 (lambda (opt)
470 (format #t "Compiling with optimization level: ~a\n"
471 (if (null? opt) "none" (car opt)))
472 (for-each (lambda (file)
473 (apply invoke
57ec9861
MO
474 `(,,(if (%current-target-system)
475 "python3"
476 '(string-append out
477 "/bin/python3"))
478 ,@opt
479 "-m" "compileall"
480 "-f" ; force rebuild
481 ;; Don't build lib2to3, because it's Python 2 code.
482 "-x" "lib2to3/.*"
483 ,file)))
9b7e389d 484 (find-files out "\\.py$")))
b4291db2 485 (list '() '("-O") '("-OO")))
3cff86db
MB
486 #t)))
487 ;; XXX: Apply patch on ARM platforms only to avoid a full rebuild.
488 ;; Remove this phase in the next rebuild cycle.
489 ,@(let ((system (or (%current-target-system)
490 (%current-system))))
491 (if (any (cute string-prefix? <> system)
492 '("arm" "aarch64"))
493 '((add-after 'unpack 'apply-alignment-patch
494 (lambda* (#:key native-inputs inputs #:allow-other-keys)
495 (invoke "patch" "-p1" "--force" "--input"
496 (assoc-ref (or native-inputs inputs)
497 "arm-alignment.patch")))))
498 '()))))))
680bc34d
MB
499 (native-inputs
500 `(("tzdata" ,tzdata-for-tests)
3cff86db
MB
501
502 ;; Disable unaligned accesses in the sha3 module on ARM as
503 ;; it causes a test failure when building 32-bit Python on a
504 ;; 64-bit kernel. See <https://bugs.python.org/issue36515>.
505 ;; TODO: make this a regular patch in the next rebuild cycle.
506 ,@(let ((system (or (%current-target-system)
507 (%current-system))))
508 (if (any (cute string-prefix? <> system)
509 '("arm" "aarch64"))
510 `(("arm-alignment.patch" ,(search-patch "python-3-arm-alignment.patch")))
511 '()))
512
ceb9de75
MO
513 ,@(if (%current-target-system)
514 `(("python3" ,this-package))
515 '())
680bc34d 516 ,@(package-native-inputs python-2)))
1aebc0cb
AE
517 (native-search-paths
518 (list (search-path-specification
519 (variable "PYTHONPATH")
0e05d01e
SB
520 (files (list (string-append "lib/python"
521 (version-major+minor version)
522 "/site-packages"))))))))
f26a77ff 523
45848023 524;; Current 3.x version.
b15d1c46 525(define-public python-3 python-3.8)
45848023
HG
526
527;; Current major version.
528(define-public python python-3)
529
95288fcc
LC
530;; Minimal variants of Python, mostly used to break the cycle between Tk and
531;; Python (Tk -> libxcb -> Python.)
532
533(define-public python2-minimal
a55ebe2e 534 (package/inherit python-2
23c0d40e 535 (name "python2-minimal")
02f0c3b2 536 (outputs '("out"))
5927a843
LC
537
538 ;; Keep zlib, which is used by 'pip' (via the 'zipimport' module), which
539 ;; is invoked upon 'make install'. 'pip' also expects 'ctypes' and thus
d1659c0f
MB
540 ;; libffi. Expat is needed for XML support which is expected by a lot
541 ;; of libraries out there.
542 (inputs `(("expat" ,expat)
543 ("libffi" ,libffi)
5927a843 544 ("zlib" ,zlib)))))
95288fcc
LC
545
546(define-public python-minimal
90aeaee8 547 (package/inherit python
95288fcc 548 (name "python-minimal")
02f0c3b2 549 (outputs '("out"))
95288fcc 550
d0b73960 551 ;; Build fails due to missing ctypes without libffi.
95288fcc 552 ;; OpenSSL is a mandatory dependency of Python 3.x, for urllib;
d1659c0f
MB
553 ;; zlib is required by 'zipimport', used by pip. Expat is needed
554 ;; for XML support, which is generally expected to be available.
555 (inputs `(("expat" ,expat)
556 ("libffi" ,libffi)
d0b73960 557 ("openssl" ,openssl)
95288fcc
LC
558 ("zlib" ,zlib)))))
559
9c3f2c2e 560(define-public python-debug
90aeaee8 561 (package/inherit python
9c3f2c2e
LC
562 (name "python-debug")
563 (outputs '("out" "debug"))
564 (build-system gnu-build-system)
565 (arguments
566 (substitute-keyword-arguments (package-arguments python)
567 ((#:configure-flags flags '())
568 `(cons "--with-pydebug" ,flags))))
569 (synopsis
570 "High-level, dynamically-typed programming language (for debugging)")
571 (description
572 "This variant of Python provides an interpreter built with
573@code{--with-pydebug} to help develop and debug extensions. See
574@url{https://pythonextensionpatterns.readthedocs.io/en/latest/debugging/debug.html},
575for more information.")))
576
64cb064c
LC
577(define* (wrap-python3 python
578 #:optional
579 (name (string-append (package-name python) "-wrapper")))
90aeaee8 580 (package/inherit python
95288fcc 581 (name name)
898238b9
AE
582 (source #f)
583 (build-system trivial-build-system)
02f0c3b2 584 (outputs '("out"))
3969ca54 585 (inputs `(("bash" ,bash)))
3c0f2329 586 (propagated-inputs `(("python" ,python)))
898238b9
AE
587 (arguments
588 `(#:modules ((guix build utils))
589 #:builder
590 (begin
591 (use-modules (guix build utils))
592 (let ((bin (string-append (assoc-ref %outputs "out") "/bin"))
593 (python (string-append (assoc-ref %build-inputs "python") "/bin/")))
594 (mkdir-p bin)
595 (for-each
596 (lambda (old new)
597 (symlink (string-append python old)
598 (string-append bin "/" new)))
3969ca54
SB
599 `("python3" ,"pydoc3" ,"idle3" ,"pip3")
600 `("python" ,"pydoc" ,"idle" ,"pip"))
601 ;; python-config outputs search paths based upon its location,
602 ;; use a bash wrapper to avoid changing its outputs.
603 (let ((bash (string-append (assoc-ref %build-inputs "bash")
604 "/bin/bash"))
605 (old (string-append python "python3-config"))
606 (new (string-append bin "/python-config")))
607 (with-output-to-file new
608 (lambda ()
609 (format #t "#!~a~%" bash)
610 (format #t "exec \"~a\" \"$@\"~%" old)
611 (chmod new #o755)
612 #t)))))))
0d56e3e1
LC
613 (synopsis "Wrapper for the Python 3 commands")
614 (description
615 "This package provides wrappers for the commands of Python@tie{}3.x such
616that they can be invoked under their usual name---e.g., @command{python}
24244f3f
PN
617instead of @command{python3} or @command{pip} instead of @command{pip3}.
618To function properly, this package should not be installed together with the
619@command{python} package.")))
0d56e3e1 620
95288fcc
LC
621(define-public python-wrapper (wrap-python3 python))
622(define-public python-minimal-wrapper (wrap-python3 python-minimal))
20801f9d
EF
623
624(define-public micropython
625 (package
626 (name "micropython")
7806e568 627 (version "1.13")
20801f9d
EF
628 (source
629 (origin
630 (method url-fetch)
631 (uri (string-append "https://github.com/micropython/micropython/"
632 "releases/download/v" version
633 "/micropython-" version ".tar.gz"))
634 (sha256
7806e568 635 (base32 "0lfl7dv5v9rqckslrjqy5swjri29x1nj5d79wxnjys4sq6r2xcws"))
20801f9d
EF
636 (modules '((guix build utils)))
637 (snippet
638 '(begin
639 (delete-file-recursively "ports/cc3200/FreeRTOS")
640 (with-directory-excursion "lib"
641 ;; TODO: Unbundle axtls and berkley-db-1.xx
642 (for-each delete-file-recursively
643 '("libffi" "lwip" "stm32lib" "nrfx")))
644 #t))))
645 (build-system gnu-build-system)
646 (arguments
647 `(#:phases
648 (modify-phases %standard-phases
399d89b5
EF
649 (add-before 'build 'build-mpy-cross
650 (lambda* (#:key make-flags #:allow-other-keys)
651 (with-directory-excursion "mpy-cross"
652 (apply invoke "make" make-flags))))
653 (add-after 'build-mpy-cross 'prepare-build
20801f9d
EF
654 (lambda _
655 (chdir "ports/unix")
656 ;; see: https://github.com/micropython/micropython/pull/4246
657 (substitute* "Makefile"
658 (("-Os") "-Os -ffp-contract=off"))
659 #t))
660 (replace 'install-license-files
661 ;; We don't build in the root directory so the file isn't found.
662 (lambda* (#:key outputs #:allow-other-keys)
663 (let* ((out (assoc-ref outputs "out"))
664 (dest (string-append out "/share/doc/" ,name "-" ,version "/")))
665 (install-file "../../LICENSE" dest))
666 #t))
667 (delete 'configure)) ; no configure
668 #:make-flags (list (string-append "PREFIX=" (assoc-ref %outputs "out"))
669 "V=1")
670 #:test-target "test"))
671 (native-inputs
672 `(("pkg-config" ,pkg-config)
673 ("python" ,python-wrapper)))
674 (inputs
675 `(("libffi" ,libffi)))
676 (home-page "https://micropython.org/")
677 (synopsis "Python implementation for microcontrollers and constrained systems")
678 (description "MicroPython is a lean and efficient implementation of the
679Python 3 programming language that includes a small subset of the Python
680standard library and is optimised to run on microcontrollers and in constrained
681environments. MicroPython is packed full of advanced features such as an
682interactive prompt, arbitrary precision integers, closures, list comprehension,
683generators, exception handling and more. Still it is compact enough to fit and
684run within just 256k of code space and 16k of RAM. MicroPython aims to be as
685compatible with normal Python as possible to allow you to transfer code with
686ease from the desktop to a microcontroller or embedded system.")
687 (license license:expat)))
625cdb05
LDB
688
689(define-public pypy3
690 (package
691 (name "pypy3")
692 (version "7.3.1")
693 (source (origin
694 (method url-fetch)
695 (uri (string-append "https://bitbucket.org/pypy/pypy/downloads/" ;
696 "pypy3.6-v" version "-src.tar.bz2"))
697 (sha256
698 (base32
699 "10zsk8jby8j6visk5mzikpb1cidvz27qq4pfpa26jv53klic6b0c"))
700 (patches (search-patches "pypy3-7.3.1-fix-tests.patch"))))
701 (build-system gnu-build-system)
702 (native-inputs
703 `(("python-2" ,python-2)
704 ("pkg-config" ,pkg-config)
705 ("tar" ,tar) ; Required for package.py
706 ("python2-pycparser" ,python2-pycparser)
707 ("python2-hypothesis" ,python2-hypothesis)
708 ("nss-certs" ,nss-certs) ; For ssl tests
709 ("gzip" ,gzip)))
710 (inputs
711 `(("libffi" ,libffi)
712 ("zlib" ,zlib)
713 ("ncurses" ,ncurses)
714 ("openssl" ,openssl)
715 ("expat" ,expat)
716 ("bzip2" ,bzip2)
717 ("sqlite" ,sqlite)
718 ("gdbm" ,gdbm)
719 ("tcl" ,tcl)
720 ("tk" ,tk)
721 ("glibc" ,glibc)
722 ("bash-minimal" ,bash-minimal) ; Used as /bin/sh
723 ("xz" ,xz))) ; liblzma
724 (arguments
725 `(#:tests? #f ;FIXME: Disabled for now, there are many tests failing.
726 #:modules ((ice-9 ftw) (ice-9 match)
727 (guix build utils) (guix build gnu-build-system))
728 #:phases (modify-phases %standard-phases
729 (delete 'configure)
730 (add-after 'unpack 'patch-source
731 (lambda* (#:key inputs outputs #:allow-other-keys)
732 (substitute* '("rpython/rlib/clibffi.py")
733 ;; find_library does not work for libc
734 (("ctypes\\.util\\.find_library\\('c'\\)") "'libc.so'"))
735 (substitute* '("lib_pypy/cffi/_pycparser/ply/cpp.py")
736 ;; Make reproducible (XXX: unused?)
737 (("time\\.localtime\\(\\)") "time.gmtime(0)"))
738 (substitute* '("pypy/module/sys/version.py")
739 ;; Make reproducible
740 (("t\\.gmtime\\(\\)") "t.gmtime(0)"))
741 (substitute* '("lib_pypy/_tkinter/tklib_build.py")
742 ;; Link to versioned libtcl and libtk
743 (("linklibs = \\['tcl', 'tk'\\]")
744 "linklibs = ['tcl8.6', 'tk8.6']")
745 (("incdirs = \\[\\]")
746 (string-append "incdirs = ['"
747 (assoc-ref inputs "tcl")
748 "/include', '"
749 (assoc-ref inputs "tk")
750 "/include']")))
751 (substitute* '("lib_pypy/_curses_build.py")
752 ;; Find curses
753 (("/usr/local") (assoc-ref inputs "ncurses")))
754 (substitute* '("lib_pypy/_sqlite3_build.py")
755 ;; Always use search paths
756 (("sys\\.platform\\.startswith\\('freebsd'\\)") "True")
757 ;; Find sqlite3
758 (("/usr/local") (assoc-ref inputs "sqlite"))
759 (("libname = 'sqlite3'")
760 (string-append "libname = '"
761 (assoc-ref inputs "sqlite")
762 "/lib/libsqlite3.so.0'")))
763 (substitute* '("lib-python/3/subprocess.py")
764 ;; Fix shell path
765 (("/bin/sh")
766 (string-append (assoc-ref inputs "bash-minimal") "/bin/sh")))
767 (substitute* '("lib-python/3/distutils/unixccompiler.py")
768 ;; gcc-toolchain does not provide symlink cc -> gcc
769 (("\"cc\"") "\"gcc\""))
770 #t))
771 (add-after
772 'unpack 'set-source-file-times-to-1980
773 ;; copied from python package, required by zip testcase
774 (lambda _
775 (let ((circa-1980 (* 10 366 24 60 60)))
776 (ftw "." (lambda (file stat flag)
777 (utime file circa-1980 circa-1980)
778 #t))
779 #t)))
780 (replace 'build
781 (lambda* (#:key inputs #:allow-other-keys)
782 (with-directory-excursion "pypy/goal"
783 ;; Build with jit optimization.
784 (invoke "python2"
785 "../../rpython/bin/rpython"
786 (string-append "--make-jobs="
787 (number->string (parallel-job-count)))
788 "-Ojit"
789 "targetpypystandalone"))
790 ;; Build c modules and package everything, so tests work.
791 (with-directory-excursion "pypy/tool/release"
792 (unsetenv "PYTHONPATH") ; Do not use the system’s python libs:
793 ; AttributeError: module 'enum' has no
794 ; attribute 'IntFlag'
795 (invoke "python2" "package.py"
796 "--archive-name" "pypy-dist"
797 "--builddir" (getcwd)))))
798 (replace 'check
799 (lambda* (#:key tests? #:allow-other-keys)
800 (if tests?
801 (begin
802 (setenv "HOME" "/tmp") ; test_with_pip tries to
803 ; access ~/.cache/pip
804 ;; Run library tests only (no interpreter unit
805 ;; tests). This is what Gentoo does.
806 (invoke
807 "python2"
808 "pypy/test_all.py"
809 "--pypy=pypy/tool/release/pypy-dist/bin/pypy3"
810 "lib-python"))
811 (format #t "test suite not run~%"))
812 #t))
813 (replace 'install
814 (lambda* (#:key inputs outputs #:allow-other-keys)
815 (with-directory-excursion "pypy/tool/release"
816 ;; Delete test data.
817 (for-each
818 (lambda (x)
819 (delete-file-recursively (string-append
820 "pypy-dist/lib-python/3/" x)))
821 '("tkinter/test"
822 "test"
823 "sqlite3/test"
824 "lib2to3/tests"
825 "idlelib/idle_test"
826 "distutils/tests"
827 "ctypes/test"
828 "unittest/test"))
829 ;; Patch shebang referencing python2
830 (substitute* '("pypy-dist/lib-python/3/cgi.py"
831 "pypy-dist/lib-python/3/encodings/rot_13.py")
832 (("#!.+/bin/python")
833 (string-append "#!" (assoc-ref outputs "out") "/bin/pypy3")))
834 (with-fluids ((%default-port-encoding "ISO-8859-1"))
835 (substitute* '("pypy-dist/lib_pypy/_md5.py"
836 "pypy-dist/lib_pypy/_sha1.py")
837 (("#!.+/bin/python")
838 (string-append "#!" (assoc-ref outputs "out") "/bin/pypy3"))))
839 (copy-recursively "pypy-dist" (assoc-ref outputs "out")))
840 #t)))))
841 (home-page "https://www.pypy.org/")
842 (synopsis "Python implementation with just-in-time compilation")
843 (description "PyPy is a faster, alternative implementation of the Python
844programming language employing a just-in-time compiler. It supports most
845Python code natively, including C extensions.")
846 (license (list license:expat ; pypy itself; _pytest/
847 license:psfl ; python standard library in lib-python/
848 license:asl2.0 ; dotviewer/font/ and some of lib-python/
849 license:gpl3+ ; ./rpython/rlib/rvmprof/src/shared/libbacktrace/dwarf2.*
850 license:bsd-3 ; lib_pypy/cffi/_pycparser/ply/
851 (license:non-copyleft
852 "http://www.unicode.org/copyright.html")))))
853