gnu: micropython: Update to 1.14.
[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
370 (inherit python-2)
371 (name "python")
372 (properties `((superseded . ,python-2)))))
373
374 (define-public python-3.8
375 (package (inherit python-2)
376 (name "python")
377 (replacement python-3.8/fixed)
378 (version "3.8.2")
379 (source (origin
380 (method url-fetch)
381 (uri (string-append "https://www.python.org/ftp/python/"
382 version "/Python-" version ".tar.xz"))
383 (patches (search-patches
384 "python-CVE-2020-26116.patch"
385 "python-3-fix-tests.patch"
386 "python-3.8-fix-tests.patch"
387 "python-3-deterministic-build-info.patch"
388 "python-3-search-paths.patch"))
389 (sha256
390 (base32
391 "1ps5v323cp5czfshqjmbsqw7nvrdpcbk06f62jbzaqik4gfffii6"))
392 (modules '((guix build utils)))
393 (snippet
394 '(begin
395 ;; Delete the bundled copy of libexpat.
396 (delete-file-recursively "Modules/expat")
397 (substitute* "Modules/Setup"
398 ;; Link Expat instead of embedding the bundled one.
399 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
400 #t))))
401 (arguments
402 (substitute-keyword-arguments (package-arguments python-2)
403 ((#:make-flags _)
404 `(list (string-append
405 (format #f "TESTOPTS=-j~d" (parallel-job-count))
406 ;; test_mmap fails on low-memory systems.
407 " --exclude test_mmap"
408 ;; test_socket may hang and eventually run out of memory
409 ;; on some systems: <https://bugs.python.org/issue34587>.
410 " test_socket"
411 ,@(if (hurd-target?)
412 '(" test_posix" ;multiple errors
413 " test_time"
414 " test_pty"
415 " test_shutil"
416 " test_tempfile" ;chflags: invalid argument:
417 ; tbv14c9t/dir0/dir0/dir0/test0.txt
418 " test_asyncio" ;runs over 10min
419 " test_os" ;stty: 'standard input':
420 ; Inappropriate ioctl for device
421 " test_openpty" ;No such file or directory
422 " test_selectors" ;assertEqual(NUM_FDS // 2, len(fds))
423 ; 32752 != 4
424 " test_compileall" ;multiple errors
425 " test_poll" ;list index out of range
426 " test_subprocess" ;runs over 10min
427 " test_asyncore" ;multiple errors
428 " test_threadsignals"
429 " test_eintr" ;Process return code is -14
430 " test_io" ;multiple errors
431 " test_logging"
432 " test_signal"
433 " test_threading" ;runs over 10min
434 " test_flags" ;ERROR
435 " test_bidirectional_pty"
436 " test_create_unix_connection"
437 " test_unix_sock_client_ops"
438 " test_open_unix_connection"
439 " test_open_unix_connection_error"
440 " test_read_pty_output"
441 " test_write_pty")
442 '()))))
443 ((#:phases phases)
444 `(modify-phases ,phases
445 ,@(if (hurd-system?)
446 `((delete 'patch-regen-for-hurd)) ;regen was removed after 3.5.9
447 '())
448 ,@(if (hurd-target?)
449 ;; The build system refuses to cross-compile for unknown targets
450 ;; even though it works fine. Add GNU/Hurd target.
451 ;; TODO: Make it a patch in a future rebuild cycle.
452 '((add-before 'configure 'support-hurd-cross-compile
453 (lambda _
454 (substitute* "configure"
455 (("\\*-\\*-vxworks.*" all)
456 (string-append "*-*-gnu)\nac_sys_system=GNU\n;;\n" all)))
457 #t)))
458 '())
459 (add-before 'check 'set-TZDIR
460 (lambda* (#:key inputs native-inputs #:allow-other-keys)
461 ;; test_email requires the Olson time zone database.
462 (setenv "TZDIR"
463 (string-append (assoc-ref
464 (or native-inputs inputs) "tzdata")
465 "/share/zoneinfo"))
466 #t))
467 ;; Unset SOURCE_DATE_EPOCH while running the test-suite and set it
468 ;; again afterwards. See <https://bugs.python.org/issue34022>.
469 (add-before 'check 'unset-SOURCE_DATE_EPOCH
470 (lambda _ (unsetenv "SOURCE_DATE_EPOCH") #t))
471 (add-after 'check 'reset-SOURCE_DATE_EPOCH
472 (lambda _ (setenv "SOURCE_DATE_EPOCH" "1") #t))
473 (replace 'rebuild-bytecode
474 (lambda* (#:key outputs #:allow-other-keys)
475 (let ((out (assoc-ref outputs "out")))
476 ;; Disable hash randomization to ensure the generated .pycs
477 ;; are reproducible.
478 (setenv "PYTHONHASHSEED" "0")
479 (for-each
480 (lambda (opt)
481 (format #t "Compiling with optimization level: ~a\n"
482 (if (null? opt) "none" (car opt)))
483 (for-each (lambda (file)
484 (apply invoke
485 `(,,(if (%current-target-system)
486 "python3"
487 '(string-append out
488 "/bin/python3"))
489 ,@opt
490 "-m" "compileall"
491 "-f" ; force rebuild
492 ;; Don't build lib2to3, because it's Python 2 code.
493 "-x" "lib2to3/.*"
494 ,file)))
495 (find-files out "\\.py$")))
496 (list '() '("-O") '("-OO")))
497 #t)))
498 ;; XXX: Apply patch on ARM platforms only to avoid a full rebuild.
499 ;; Remove this phase in the next rebuild cycle.
500 ,@(let ((system (or (%current-target-system)
501 (%current-system))))
502 (if (any (cute string-prefix? <> system)
503 '("arm" "aarch64"))
504 '((add-after 'unpack 'apply-alignment-patch
505 (lambda* (#:key native-inputs inputs #:allow-other-keys)
506 (invoke "patch" "-p1" "--force" "--input"
507 (assoc-ref (or native-inputs inputs)
508 "arm-alignment.patch")))))
509 '()))))))
510 (native-inputs
511 `(("tzdata" ,tzdata-for-tests)
512
513 ;; Disable unaligned accesses in the sha3 module on ARM as
514 ;; it causes a test failure when building 32-bit Python on a
515 ;; 64-bit kernel. See <https://bugs.python.org/issue36515>.
516 ;; TODO: make this a regular patch in the next rebuild cycle.
517 ,@(let ((system (or (%current-target-system)
518 (%current-system))))
519 (if (any (cute string-prefix? <> system)
520 '("arm" "aarch64"))
521 `(("arm-alignment.patch" ,(search-patch "python-3-arm-alignment.patch")))
522 '()))
523
524 ,@(if (%current-target-system)
525 `(("python3" ,this-package))
526 '())
527 ,@(package-native-inputs python-2)))
528 (native-search-paths
529 (list (search-path-specification
530 (variable "PYTHONPATH")
531 (files (list (string-append "lib/python"
532 (version-major+minor version)
533 "/site-packages"))))))))
534
535 (define python-3.8/fixed
536 (package
537 (inherit python-3.8)
538 (source (origin
539 (inherit (package-source python-3.8))
540 (patches (append (search-patches "python-3.8-CVE-2021-3177.patch")
541 (origin-patches (package-source python-3.8))))))))
542
543 (define-public python-3.9
544 (package (inherit python-3.8)
545 (name "python-next")
546 (version "3.9.2")
547 (source (origin
548 (method url-fetch)
549 (uri (string-append "https://www.python.org/ftp/python/"
550 version "/Python-" version ".tar.xz"))
551 (patches (search-patches
552 "python-3.9-fix-tests.patch"
553 "python-3-deterministic-build-info.patch"
554 "python-3-search-paths.patch"))
555 (sha256
556 (base32
557 "0z94vv5qhlwvcgc4sy9sdiqs0220s84wx3b62vslh5419z2k881w"))
558 (modules '((guix build utils)))
559 (snippet
560 '(begin
561 ;; Delete the bundled copy of libexpat.
562 (delete-file-recursively "Modules/expat")
563 (substitute* "Modules/Setup"
564 ;; Link Expat instead of embedding the bundled one.
565 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
566 #t))))))
567
568 ;; Current 3.x version.
569 (define-public python-3 python-3.8)
570
571 ;; Current major version.
572 (define-public python python-3)
573
574 ;; Minimal variants of Python, mostly used to break the cycle between Tk and
575 ;; Python (Tk -> libxcb -> Python.)
576
577 (define-public python2-minimal
578 (package/inherit python-2
579 (name "python2-minimal")
580 (outputs '("out"))
581
582 ;; Keep zlib, which is used by 'pip' (via the 'zipimport' module), which
583 ;; is invoked upon 'make install'. 'pip' also expects 'ctypes' and thus
584 ;; libffi. Expat is needed for XML support which is expected by a lot
585 ;; of libraries out there.
586 (inputs `(("expat" ,expat)
587 ("libffi" ,libffi)
588 ("zlib" ,zlib)))))
589
590 (define-public python-minimal
591 (package/inherit python
592 (name "python-minimal")
593 (outputs '("out"))
594
595 ;; Build fails due to missing ctypes without libffi.
596 ;; OpenSSL is a mandatory dependency of Python 3.x, for urllib;
597 ;; zlib is required by 'zipimport', used by pip. Expat is needed
598 ;; for XML support, which is generally expected to be available.
599 (inputs `(("expat" ,expat)
600 ("libffi" ,libffi)
601 ("openssl" ,openssl)
602 ("zlib" ,zlib)))))
603
604 (define-public python-debug
605 (package/inherit python
606 (name "python-debug")
607 (outputs '("out" "debug"))
608 (build-system gnu-build-system)
609 (arguments
610 (substitute-keyword-arguments (package-arguments python)
611 ((#:configure-flags flags '())
612 `(cons "--with-pydebug" ,flags))))
613 (synopsis
614 "High-level, dynamically-typed programming language (for debugging)")
615 (description
616 "This variant of Python provides an interpreter built with
617 @code{--with-pydebug} to help develop and debug extensions. See
618 @url{https://pythonextensionpatterns.readthedocs.io/en/latest/debugging/debug.html},
619 for more information.")))
620
621 (define* (wrap-python3 python
622 #:optional
623 (name (string-append (package-name python) "-wrapper")))
624 (package/inherit python
625 (name name)
626 (source #f)
627 (build-system trivial-build-system)
628 (outputs '("out"))
629 (inputs `(("bash" ,bash)))
630 (propagated-inputs `(("python" ,python)))
631 (arguments
632 `(#:modules ((guix build utils))
633 #:builder
634 (begin
635 (use-modules (guix build utils))
636 (let ((bin (string-append (assoc-ref %outputs "out") "/bin"))
637 (python (string-append (assoc-ref %build-inputs "python") "/bin/")))
638 (mkdir-p bin)
639 (for-each
640 (lambda (old new)
641 (symlink (string-append python old)
642 (string-append bin "/" new)))
643 `("python3" ,"pydoc3" ,"idle3" ,"pip3")
644 `("python" ,"pydoc" ,"idle" ,"pip"))
645 ;; python-config outputs search paths based upon its location,
646 ;; use a bash wrapper to avoid changing its outputs.
647 (let ((bash (string-append (assoc-ref %build-inputs "bash")
648 "/bin/bash"))
649 (old (string-append python "python3-config"))
650 (new (string-append bin "/python-config")))
651 (with-output-to-file new
652 (lambda ()
653 (format #t "#!~a~%" bash)
654 (format #t "exec \"~a\" \"$@\"~%" old)
655 (chmod new #o755)
656 #t)))))))
657 (synopsis "Wrapper for the Python 3 commands")
658 (description
659 "This package provides wrappers for the commands of Python@tie{}3.x such
660 that they can be invoked under their usual name---e.g., @command{python}
661 instead of @command{python3} or @command{pip} instead of @command{pip3}.
662 To function properly, this package should not be installed together with the
663 @command{python} package.")))
664
665 (define-public python-wrapper (wrap-python3 python))
666 (define-public python-minimal-wrapper (wrap-python3 python-minimal))
667
668 (define-public micropython
669 (package
670 (name "micropython")
671 (version "1.14")
672 (source
673 (origin
674 (method url-fetch)
675 (uri (string-append "https://github.com/micropython/micropython/"
676 "releases/download/v" version
677 "/micropython-" version ".tar.xz"))
678 (sha256
679 (base32 "0k6ri3rxxnnmvcbi7z7x59r21f4vj9dcf9j64jhj1cgazmb62c4p"))
680 (modules '((guix build utils)))
681 (snippet
682 '(begin
683 (delete-file-recursively "ports/cc3200/FreeRTOS")
684 (with-directory-excursion "lib"
685 ;; TODO: Unbundle axtls and berkley-db-1.xx
686 (for-each delete-file-recursively
687 '("libffi" "lwip" "stm32lib" "nrfx")))
688 #t))))
689 (build-system gnu-build-system)
690 (arguments
691 `(#:phases
692 (modify-phases %standard-phases
693 (add-before 'build 'build-mpy-cross
694 (lambda* (#:key make-flags #:allow-other-keys)
695 (with-directory-excursion "mpy-cross"
696 (apply invoke "make" make-flags))))
697 (add-after 'build-mpy-cross 'prepare-build
698 (lambda _
699 (chdir "ports/unix")
700 ;; see: https://github.com/micropython/micropython/pull/4246
701 (substitute* "Makefile"
702 (("-Os") "-Os -ffp-contract=off"))
703 #t))
704 (replace 'install-license-files
705 ;; We don't build in the root directory so the file isn't found.
706 (lambda* (#:key outputs #:allow-other-keys)
707 (let* ((out (assoc-ref outputs "out"))
708 (dest (string-append out "/share/doc/" ,name "-" ,version "/")))
709 (install-file "../../LICENSE" dest))
710 #t))
711 (delete 'configure)) ; no configure
712 #:make-flags (list (string-append "PREFIX=" (assoc-ref %outputs "out"))
713 "V=1")
714 #:test-target "test"))
715 (native-inputs
716 `(("pkg-config" ,pkg-config)
717 ("python" ,python-wrapper)))
718 (inputs
719 `(("libffi" ,libffi)))
720 (home-page "https://micropython.org/")
721 (synopsis "Python implementation for microcontrollers and constrained systems")
722 (description "MicroPython is a lean and efficient implementation of the
723 Python 3 programming language that includes a small subset of the Python
724 standard library and is optimised to run on microcontrollers and in constrained
725 environments. MicroPython is packed full of advanced features such as an
726 interactive prompt, arbitrary precision integers, closures, list comprehension,
727 generators, exception handling and more. Still it is compact enough to fit and
728 run within just 256k of code space and 16k of RAM. MicroPython aims to be as
729 compatible with normal Python as possible to allow you to transfer code with
730 ease from the desktop to a microcontroller or embedded system.")
731 (license license:expat)))
732
733 (define-public pypy3
734 (package
735 (name "pypy3")
736 (version "7.3.1")
737 (source (origin
738 (method url-fetch)
739 (uri (string-append "https://bitbucket.org/pypy/pypy/downloads/" ;
740 "pypy3.6-v" version "-src.tar.bz2"))
741 (sha256
742 (base32
743 "10zsk8jby8j6visk5mzikpb1cidvz27qq4pfpa26jv53klic6b0c"))
744 (patches (search-patches "pypy3-7.3.1-fix-tests.patch"))))
745 (build-system gnu-build-system)
746 (native-inputs
747 `(("python-2" ,python-2)
748 ("pkg-config" ,pkg-config)
749 ("tar" ,tar) ; Required for package.py
750 ("python2-pycparser" ,python2-pycparser)
751 ("python2-hypothesis" ,python2-hypothesis)
752 ("nss-certs" ,nss-certs) ; For ssl tests
753 ("gzip" ,gzip)))
754 (inputs
755 `(("libffi" ,libffi)
756 ("zlib" ,zlib)
757 ("ncurses" ,ncurses)
758 ("openssl" ,openssl)
759 ("expat" ,expat)
760 ("bzip2" ,bzip2)
761 ("sqlite" ,sqlite)
762 ("gdbm" ,gdbm)
763 ("tcl" ,tcl)
764 ("tk" ,tk)
765 ("glibc" ,glibc)
766 ("bash-minimal" ,bash-minimal) ; Used as /bin/sh
767 ("xz" ,xz))) ; liblzma
768 (arguments
769 `(#:tests? #f ;FIXME: Disabled for now, there are many tests failing.
770 #:modules ((ice-9 ftw) (ice-9 match)
771 (guix build utils) (guix build gnu-build-system))
772 #:phases (modify-phases %standard-phases
773 (delete 'configure)
774 (add-after 'unpack 'patch-source
775 (lambda* (#:key inputs outputs #:allow-other-keys)
776 (substitute* '("rpython/rlib/clibffi.py")
777 ;; find_library does not work for libc
778 (("ctypes\\.util\\.find_library\\('c'\\)") "'libc.so'"))
779 (substitute* '("lib_pypy/cffi/_pycparser/ply/cpp.py")
780 ;; Make reproducible (XXX: unused?)
781 (("time\\.localtime\\(\\)") "time.gmtime(0)"))
782 (substitute* '("pypy/module/sys/version.py")
783 ;; Make reproducible
784 (("t\\.gmtime\\(\\)") "t.gmtime(0)"))
785 (substitute* '("lib_pypy/_tkinter/tklib_build.py")
786 ;; Link to versioned libtcl and libtk
787 (("linklibs = \\['tcl', 'tk'\\]")
788 "linklibs = ['tcl8.6', 'tk8.6']")
789 (("incdirs = \\[\\]")
790 (string-append "incdirs = ['"
791 (assoc-ref inputs "tcl")
792 "/include', '"
793 (assoc-ref inputs "tk")
794 "/include']")))
795 (substitute* '("lib_pypy/_curses_build.py")
796 ;; Find curses
797 (("/usr/local") (assoc-ref inputs "ncurses")))
798 (substitute* '("lib_pypy/_sqlite3_build.py")
799 ;; Always use search paths
800 (("sys\\.platform\\.startswith\\('freebsd'\\)") "True")
801 ;; Find sqlite3
802 (("/usr/local") (assoc-ref inputs "sqlite"))
803 (("libname = 'sqlite3'")
804 (string-append "libname = '"
805 (assoc-ref inputs "sqlite")
806 "/lib/libsqlite3.so.0'")))
807 (substitute* '("lib-python/3/subprocess.py")
808 ;; Fix shell path
809 (("/bin/sh")
810 (string-append (assoc-ref inputs "bash-minimal") "/bin/sh")))
811 (substitute* '("lib-python/3/distutils/unixccompiler.py")
812 ;; gcc-toolchain does not provide symlink cc -> gcc
813 (("\"cc\"") "\"gcc\""))
814 #t))
815 (add-after
816 'unpack 'set-source-file-times-to-1980
817 ;; copied from python package, required by zip testcase
818 (lambda _
819 (let ((circa-1980 (* 10 366 24 60 60)))
820 (ftw "." (lambda (file stat flag)
821 (utime file circa-1980 circa-1980)
822 #t))
823 #t)))
824 (replace 'build
825 (lambda* (#:key inputs #:allow-other-keys)
826 (with-directory-excursion "pypy/goal"
827 ;; Build with jit optimization.
828 (invoke "python2"
829 "../../rpython/bin/rpython"
830 (string-append "--make-jobs="
831 (number->string (parallel-job-count)))
832 "-Ojit"
833 "targetpypystandalone"))
834 ;; Build c modules and package everything, so tests work.
835 (with-directory-excursion "pypy/tool/release"
836 (unsetenv "PYTHONPATH") ; Do not use the system’s python libs:
837 ; AttributeError: module 'enum' has no
838 ; attribute 'IntFlag'
839 (invoke "python2" "package.py"
840 "--archive-name" "pypy-dist"
841 "--builddir" (getcwd)))))
842 (replace 'check
843 (lambda* (#:key tests? #:allow-other-keys)
844 (if tests?
845 (begin
846 (setenv "HOME" "/tmp") ; test_with_pip tries to
847 ; access ~/.cache/pip
848 ;; Run library tests only (no interpreter unit
849 ;; tests). This is what Gentoo does.
850 (invoke
851 "python2"
852 "pypy/test_all.py"
853 "--pypy=pypy/tool/release/pypy-dist/bin/pypy3"
854 "lib-python"))
855 (format #t "test suite not run~%"))
856 #t))
857 (replace 'install
858 (lambda* (#:key inputs outputs #:allow-other-keys)
859 (with-directory-excursion "pypy/tool/release"
860 ;; Delete test data.
861 (for-each
862 (lambda (x)
863 (delete-file-recursively (string-append
864 "pypy-dist/lib-python/3/" x)))
865 '("tkinter/test"
866 "test"
867 "sqlite3/test"
868 "lib2to3/tests"
869 "idlelib/idle_test"
870 "distutils/tests"
871 "ctypes/test"
872 "unittest/test"))
873 ;; Patch shebang referencing python2
874 (substitute* '("pypy-dist/lib-python/3/cgi.py"
875 "pypy-dist/lib-python/3/encodings/rot_13.py")
876 (("#!.+/bin/python")
877 (string-append "#!" (assoc-ref outputs "out") "/bin/pypy3")))
878 (with-fluids ((%default-port-encoding "ISO-8859-1"))
879 (substitute* '("pypy-dist/lib_pypy/_md5.py"
880 "pypy-dist/lib_pypy/_sha1.py")
881 (("#!.+/bin/python")
882 (string-append "#!" (assoc-ref outputs "out") "/bin/pypy3"))))
883 (copy-recursively "pypy-dist" (assoc-ref outputs "out")))
884 #t)))))
885 (home-page "https://www.pypy.org/")
886 (synopsis "Python implementation with just-in-time compilation")
887 (description "PyPy is a faster, alternative implementation of the Python
888 programming language employing a just-in-time compiler. It supports most
889 Python code natively, including C extensions.")
890 (license (list license:expat ; pypy itself; _pytest/
891 license:psfl ; python standard library in lib-python/
892 license:asl2.0 ; dotviewer/font/ and some of lib-python/
893 license:gpl3+ ; ./rpython/rlib/rvmprof/src/shared/libbacktrace/dwarf2.*
894 license:bsd-3 ; lib_pypy/cffi/_pycparser/ply/
895 (license:non-copyleft
896 "http://www.unicode.org/copyright.html")))))
897