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