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