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