projects/guix·blob python-team · refs

1550 lines · 73.8 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013 Nikita Karetnikov <[email protected]>
3;;; Copyright © 2013-2018, 2021, 2023, 2025 Ludovic Courtès <[email protected]>
4;;; Copyright © 2013, 2014, 2015, 2016 Andreas Enge <[email protected]>
5;;; Copyright © 2014, 2015 Mark H Weaver <[email protected]>
6;;; Copyright © 2014, 2017, 2019 Eric Bavier <[email protected]>
7;;; Copyright © 2014, 2015 Federico Beffa <[email protected]>
8;;; Copyright © 2015 Omar Radwan <[email protected]>
9;;; Copyright © 2015 Pierre-Antoine Rault <[email protected]>
10;;; Copyright © 2015, 2016, 2017, 2018, 2019 Ricardo Wurmus <[email protected]>
11;;; Copyright © 2015, 2016 Christine Lemmer-Webber <[email protected]>
12;;; Copyright © 2015 Eric Dvorsak <[email protected]>
13;;; Copyright © 2015, 2016 David Thompson <[email protected]>
14;;; Copyright © 2015, 2016, 2017, 2021 Leo Famulari <[email protected]>
15;;; Copyright © 2015, 2017 Ben Woodcroft <[email protected]>
16;;; Copyright © 2015, 2016 Erik Edrosa <[email protected]>
17;;; Copyright © 2015-2020, 2023 Efraim Flashner <[email protected]>
18;;; Copyright © 2015, 2017 Kyle Meyer <[email protected]>
19;;; Copyright © 2015, 2016 Chris Marusich <[email protected]>
20;;; Copyright © 2016 Danny Milosavljevic <[email protected]>
21;;; Copyright © 2016 Lukas Gradl <[email protected]>
22;;; Copyright © 2016, 2018, 2025 Hartmut Goebel <[email protected]>
23;;; Copyright © 2016 Daniel Pimentel <[email protected]>
24;;; Copyright © 2016 Sou Bunnbu <[email protected]>
25;;; Copyright © 2016, 2017 Troy Sankey <[email protected]>
26;;; Copyright © 2016, 2017 Nikita <[email protected]>
27;;; Copyright © 2016 Dylan Jeffers <sapientech@[email protected]>
28;;; Copyright © 2016 David Craven <[email protected]>
29;;; Copyright © 2016-2022 Marius Bakke <[email protected]>
30;;; Copyright © 2016, 2017 Stefan Reichör <[email protected]>
31;;; Copyright © 2016, 2017 Alex Vong <[email protected]>
32;;; Copyright © 2016, 2017, 2018 Arun Isaac <[email protected]>
33;;; Copyright © 2016–2018, 2021, 2022 Tobias Geerinckx-Rice <[email protected]>
34;;; Copyright © 2016, 2017, 2018, 2021 Julien Lepiller <[email protected]>
35;;; Copyright © 2016, 2017 Thomas Danckaert <[email protected]>
36;;; Copyright © 2017 Carlo Zancanaro <[email protected]>
37;;; Copyright © 2017 Frederick M. Muriithi <[email protected]>
38;;; Copyright © 2017, 2018 Adriano Peluso <[email protected]>
39;;; Copyright © 2017 Ben Sturmfels <[email protected]>
40;;; Copyright © 2017, 2018, 2019 Mathieu Othacehe <[email protected]>
41;;; Copyright © 2017 José Miguel Sánchez García <[email protected]>
42;;; Copyright © 2017 Roel Janssen <[email protected]>
43;;; Copyright © 2017, 2018 Kei Kebreau <[email protected]>
44;;; Copyright © 2017 Rutger Helling <[email protected]>
45;;; Copyright © 2017 Brendan Tildesley <[email protected]>
46;;; Copyright © 2018 Ethan R. Jones <[email protected]
47;;; Copyright © 2018 Fis Trivial <[email protected]>
48;;; Copyright © 2018 Vijayalakshmi Vedantham <[email protected]>
49;;; Copyright © 2018 Mathieu Lirzin <[email protected]>
50;;; Copyright © 2018 Adam Massmann <[email protected]>
51;;; Copyright © 2016, 2018 Tomáš Čech <[email protected]>
52;;; Copyright © 2018 Nicolas Goaziou <[email protected]>
53;;; Copyright © 2018 Oleg Pykhalov <[email protected]>
54;;; Copyright © 2018 Clément Lassieur <[email protected]>
55;;; Copyright © 2018, 2019, 2020, 2021, 2025 Maxim Cournoyer <[email protected]>
56;;; Copyright © 2018 Luther Thompson <[email protected]>
57;;; Copyright © 2018 Vagrant Cascadian <[email protected]>
58;;; Copyright © 2019, 2024 Tanguy Le Carrour <[email protected]>
59;;; Copyright © 2020, 2023, 2025 Janneke Nieuwenhuizen <[email protected]>
60;;; Copyright © 2020, 2021 Greg Hogan <[email protected]>
61;;; Copyright © 2022 Philip McGrath <[email protected]>
62;;; Copyright © 2022 jgart <[email protected]>
63;;; Copyright © 2021 Lars-Dominik Braun <[email protected]>
64;;; Copyright © 2025 Hugo Buddelmeijer <[email protected]>
65;;; Copyright © 2025 Sharlatan Hellseher <[email protected]>
66;;; Copyright © 2025-2026 Nicolas Graves <[email protected]>
67;;;
68;;; This file is part of GNU Guix.
69;;;
70;;; GNU Guix is free software; you can redistribute it and/or modify it
71;;; under the terms of the GNU General Public License as published by
72;;; the Free Software Foundation; either version 3 of the License, or (at
73;;; your option) any later version.
74;;;
75;;; GNU Guix is distributed in the hope that it will be useful, but
76;;; WITHOUT ANY WARRANTY; without even the implied warranty of
77;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78;;; GNU General Public License for more details.
79;;;
80;;; You should have received a copy of the GNU General Public License
81;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
82
83(define-module (gnu packages python)
84 #:use-module ((guix licenses) #:prefix license:)
85 #:use-module (gnu packages)
86 #:use-module (gnu packages base)
87 #:use-module (gnu packages bash)
88 #:use-module (gnu packages check)
89 #:use-module (gnu packages compression)
90 #:use-module (gnu packages crypto)
91 #:use-module (gnu packages dbm)
92 #:use-module (gnu packages libffi)
93 #:use-module (gnu packages pkg-config)
94 #:use-module (gnu packages python-build)
95 #:use-module (gnu packages readline)
96 #:use-module (gnu packages sqlite)
97 #:use-module (gnu packages tcl)
98 #:use-module (gnu packages tls)
99 #:use-module (gnu packages xml)
100 #:use-module (guix gexp)
101 #:use-module (guix git-download)
102 #:use-module (guix packages)
103 #:use-module (guix download)
104 #:use-module (guix search-paths)
105 #:use-module (guix utils)
106 #:use-module (guix build-system copy)
107 #:use-module (guix build-system gnu)
108 #:use-module (guix build-system trivial)
109 #:use-module (ice-9 optargs)
110 #:use-module (srfi srfi-1)
111 #:use-module (srfi srfi-26)
112
113 #:export (customize-site
114 guix-pythonpath-search-path))
115
116(define* (customize-site version)
117 "Generate a install-sitecustomize.py phase, using VERSION."
118 `(lambda* (#:key native-inputs inputs outputs #:allow-other-keys)
119 (let* ((out (assoc-ref outputs "out"))
120 (site-packages (string-append
121 out "/lib/python"
122 ,(version-major+minor version)
123 "/site-packages"))
124 (sitecustomize.py (assoc-ref (or native-inputs inputs)
125 "sitecustomize.py"))
126 (dest (string-append site-packages "/sitecustomize.py")))
127 (mkdir-p site-packages)
128 (copy-file sitecustomize.py dest)
129 ;; Set the correct permissions on the installed file, else the byte
130 ;; compilation phase fails with a permission denied error.
131 (chmod dest #o644))))
132
133(define (guix-pythonpath-search-path version)
134 "Generate a GUIX_PYTHONPATH search path specification, using VERSION."
135 (search-path-specification (variable "GUIX_PYTHONPATH")
136 (files (list (string-append
137 "lib/python"
138 (version-major+minor version)
139 "/site-packages")))))
140
141;; Modifying this is a world-rebuild! If possible, modify phases in python
142;; packages themselves.
143(define (common-python-phases-edits)
144 #~((add-before 'configure 'patch-lib-shells
145 (lambda _
146 ;; This variable is used in setup.py to enable cross compilation
147 ;; specific switches. As it is not set properly by configure
148 ;; script, set it manually.
149 #$@(if (%current-target-system)
150 #~((setenv "_PYTHON_HOST_PLATFORM" ""))
151 #~())
152 ;; Filter for existing files, since some may not exist in all
153 ;; versions of python that are built with this recipe.
154 (substitute* (filter file-exists?
155 '("Lib/subprocess.py"
156 "Lib/popen2.py"
157 "Lib/distutils/tests/test_spawn.py"
158 "Lib/test/support/__init__.py"
159 "Lib/test/test_subprocess.py"))
160 (("/bin/sh") (which "sh")))))
161 (add-before 'configure 'do-not-record-configure-flags
162 (lambda* (#:key configure-flags #:allow-other-keys)
163 ;; Remove configure flags from the installed '_sysconfigdata.py'
164 ;; and 'Makefile' so we don't end up keeping references to the
165 ;; build tools.
166 ;;
167 ;; Preserve at least '--with-system-ffi' since otherwise the
168 ;; thing tries to build libffi, fails, and we end up with a
169 ;; Python that lacks ctypes.
170 (substitute* "configure"
171 (("^CONFIG_ARGS=.*$")
172 (format #f "CONFIG_ARGS='~a'\n"
173 (if (member "--with-system-ffi" configure-flags)
174 "--with-system-ffi"
175 ""))))))
176 (add-before 'check 'pre-check
177 (lambda _
178 ;; 'Lib/test/test_site.py' needs a valid $HOME
179 (setenv "HOME" (getcwd))))
180 (add-after 'unpack 'set-source-file-times-to-1980
181 ;; XXX One of the tests uses a ZIP library to pack up some of the
182 ;; source tree, and fails with "ZIP does not support timestamps
183 ;; before 1980". Work around this by setting the file times in the
184 ;; source tree to sometime in early 1980.
185 (lambda _
186 (let ((circa-1980 (* 10 366 24 60 60)))
187 (ftw "." (lambda (file stat flag)
188 (utime file circa-1980 circa-1980)
189 #t)))))
190 (add-after 'install 'add-libxcrypt-reference-pkgconfig
191 (lambda* (#:key inputs outputs #:allow-other-keys)
192 (define out (assoc-ref outputs "out"))
193 (define libxcrypt
194 (false-if-exception
195 (dirname (search-input-file inputs "lib/libcrypt.so.1"))))
196 (when libxcrypt
197 (substitute*
198 (find-files (string-append out "/lib/pkgconfig")
199 ".*\\.pc")
200 (("-lcrypt")
201 (string-append "-L" libxcrypt " -lcrypt"))))))
202 (add-after 'install 'remove-tests
203 ;; Remove 25 MiB of unneeded unit tests. Keep test_support.*
204 ;; because these files are used by some libraries out there.
205 (lambda* (#:key outputs #:allow-other-keys)
206 (let ((out (assoc-ref outputs "out")))
207 (match (scandir (string-append out "/lib")
208 (lambda (name)
209 (string-prefix? "python" name)))
210 ((pythonX.Y)
211 (let ((testdir (string-append out "/lib/" pythonX.Y
212 "/test")))
213 (with-directory-excursion testdir
214 (for-each delete-file-recursively
215 (scandir testdir
216 (match-lambda
217 ((or "." "..") #f)
218 ("support" #f)
219 (file
220 (not
221 (string-prefix? "test_support."
222 file))))))
223 (call-with-output-file "__init__.py" (const #t))))
224 (let ((libdir (string-append out "/lib/" pythonX.Y)))
225 (for-each
226 (lambda (directory)
227 (let ((dir (string-append libdir "/" directory)))
228 (when (file-exists? dir)
229 (delete-file-recursively dir))))
230 '("email/test" "ctypes/test" "unittest/test" "tkinter/test"
231 "sqlite3/test" "bsddb/test" "lib-tk/test" "json/tests"
232 "distutils/tests"))))))))
233 (add-after 'remove-tests 'move-tk-inter
234 (lambda* (#:key outputs #:allow-other-keys)
235 ;; When Tkinter support is built move it to a separate output so
236 ;; that the main output doesn't contain a reference to Tcl/Tk.
237 (let ((out (assoc-ref outputs "out"))
238 (tk (assoc-ref outputs "tk")))
239 (when tk
240 (match (find-files out "tkinter.*\\.so")
241 ((tkinter.so)
242 ;; The .so is in OUT/lib/pythonX.Y/lib-dynload, but we
243 ;; want it under TK/lib/pythonX.Y/site-packages.
244 (let* ((len (string-length out))
245 (target (string-append
246 tk "/"
247 (string-drop
248 (dirname (dirname tkinter.so))
249 len)
250 "/site-packages")))
251 (install-file tkinter.so target)
252 (delete-file tkinter.so))))))))
253 (add-after 'move-tk-inter 'move-idle
254 (lambda* (#:key outputs #:allow-other-keys)
255 ;; when idle is built, move it to a separate output to save some
256 ;; space (5MB)
257 (let ((out (assoc-ref outputs "out"))
258 (idle (assoc-ref outputs "idle")))
259 (when idle
260 (for-each
261 (lambda (file)
262 (let ((target (string-append idle "/bin/" (basename file))))
263 (install-file file (dirname target))
264 (delete-file file)))
265 (find-files (string-append out "/bin") "^idle"))
266 (match (find-files out "^idlelib$" #:directories? #t)
267 ((idlelib)
268 (let* ((len (string-length out))
269 (target (string-append idle "/"
270 (string-drop idlelib len)
271 "/site-packages")))
272 (mkdir-p (dirname target))
273 (rename-file idlelib target))))))))))
274
275;; Modifying this is a world-rebuild! If possible, modify phases in python
276;; packages themselves.
277(define (common-python3-phases-edits)
278 #~(#$@(common-python-phases-edits)
279 #$@(if (system-hurd?)
280 #~((add-after 'unpack 'disable-multi-processing
281 (lambda _
282 (substitute* "Makefile.pre.in"
283 (("-j0") "-j1")))))
284 #~())
285 (add-after 'unpack 'remove-windows-binaries
286 (lambda _
287 ;; Delete .exe from embedded .whl (zip) files
288 (for-each
289 (lambda (whl)
290 (let ((dir "whl-content")
291 (circa-1980 (* 10 366 24 60 60)))
292 (mkdir-p dir)
293 (with-directory-excursion dir
294 (let ((whl (string-append "../" whl)))
295 (invoke "unzip" whl)
296 (for-each delete-file (find-files "." "\\.exe$"))
297 (delete-file whl)
298 ;; Reset timestamps to prevent them from ending
299 ;; up in the Zip archive.
300 (ftw "." (lambda (file stat flag)
301 (utime file circa-1980 circa-1980)))
302 (apply invoke "zip" "-X" whl
303 (find-files "." #:directories? #t))))
304 (delete-file-recursively dir)))
305 (find-files "Lib/ensurepip" "\\.whl$"))))
306 (add-after 'move-tk-inter 'move-tk-inter:remove-explicit-store-path
307 (lambda* (#:key outputs inputs #:allow-other-keys)
308 ;; When Tkinter support is built move it to a separate output so
309 ;; that the main output doesn't contain a reference to Tcl/Tk.
310 (let ((out (assoc-ref outputs "out"))
311 (tk (assoc-ref outputs "tk")))
312 (when tk
313 ;; Remove explicit store path references.
314 (let ((tcl (assoc-ref inputs "tcl"))
315 (tk (assoc-ref inputs "tk")))
316 (substitute* (find-files (string-append out "/lib")
317 "^(_sysconfigdata_.*\\.py|Makefile)$")
318 (((string-append "-L" tk "/lib"))
319 "")
320 (((string-append "-L" tcl "/lib"))
321 "")))))))
322 (add-before 'check 'set-TZDIR
323 (lambda* (#:key inputs native-inputs #:allow-other-keys)
324 ;; test_email requires the Olson time zone database.
325 (setenv "TZDIR"
326 (string-append (assoc-ref (or native-inputs
327 inputs) "tzdata")
328 "/share/zoneinfo"))))
329 (add-after 'move-idle 'rebuild-bytecode
330 (lambda* (#:key outputs #:allow-other-keys)
331 ;; Disable hash randomization to ensure the generated .pycs
332 ;; are reproducible.
333 (setenv "PYTHONHASHSEED" "0")
334
335 (for-each (lambda (output)
336 ;; XXX: Delete existing pycs generated by the build
337 ;; system beforehand because the -f argument does
338 ;; not necessarily overwrite all files, leading to
339 ;; indeterministic results.
340 (for-each (lambda (pyc)
341 (delete-file pyc))
342 (find-files output "\\.pyc$"))
343
344 (apply invoke
345 #$(if (%current-target-system)
346 "python3"
347 #~(string-append #$output "/bin/python3"))
348 `("-m" "compileall"
349 "-o" "0"
350 "-o" "1"
351 "-o" "2"
352 "-f" ;force rebuild
353 "--invalidation-mode=unchecked-hash"
354 ;; Don't build lib2to3, because it's Python
355 ;; 2 code.
356 "-x" "lib2to3/.*"
357 ,output)))
358 (map cdr outputs))))))
359
360(define-public python-2.7
361 (package
362 (name "python2")
363 (version "2.7.18")
364 (source
365 (origin
366 (method url-fetch)
367 (uri (string-append "https://www.python.org/ftp/python/"
368 version "/Python-" version ".tar.xz"))
369 (sha256
370 (base32
371 "0hzgxl94hnflis0d6m4szjx0b52gah7wpmcg5g00q7am6xwhwb5n"))
372 (patches (search-patches "python-2.7-search-paths.patch"
373 "python-2-deterministic-build-info.patch"
374 "python-2.7-site-prefixes.patch"
375 "python-2.7-source-date-epoch.patch"
376 "python-2.7-adjust-tests.patch"
377 "python-cross-compile.patch"
378 "python-2.7-CVE-2021-3177.patch"
379 "python-2.7-expat-compat.patch"
380 "python-2.7-no-static-lib.patch"))
381 (modules '((guix build utils)))
382 (snippet
383 '(begin
384 ;; Ensure the bundled copies of these libraries are not used.
385 (for-each delete-file-recursively
386 '("Modules/_ctypes/libffi" "Modules/expat" "Modules/zlib"))
387
388 (substitute* "Modules/Setup.dist"
389 ;; Link Expat instead of embedding the bundled one.
390 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
391
392 ;; Suboptimal to delete failing tests here, but if we delete them in
393 ;; the arguments then we need to make sure to strip out that phase
394 ;; when it gets inherited by python and python-minimal.
395 (for-each delete-file
396 '("Lib/test/test_compileall.py"
397 "Lib/test/test_ctypes.py" ; fails on mips64el
398 "Lib/test/test_distutils.py"
399 "Lib/test/test_import.py"
400 "Lib/test/test_shutil.py"
401 "Lib/test/test_socket.py"
402 "Lib/test/test_subprocess.py"))))))
403 (outputs '("out"
404 "tk" ;tkinter; adds 50 MiB to the closure
405 "idle")) ;programming environment; weighs 5MB
406 (build-system gnu-build-system)
407 (arguments
408 (list
409 #:test-target "test"
410 #:configure-flags
411 #~(list ;; -fno-semantic-interposition reinstates some optimizations by gcc
412 ;; leading to around 15% speedup. This is the default starting from
413 ;; python 3.10.
414 ;; XXX FIXME: How to add "-Wno-error=implicit-function-declaration"
415 ;; *only* for *this* python-2 package? It's not needed for any
416 ;; package inheriting from us.
417 "CFLAGS=-Wno-error=incompatible-pointer-types -fno-semantic-interposition"
418 "--enable-shared" ;allow embedding
419 "--with-system-expat" ;for XML support
420 "--with-system-ffi" ;build ctypes
421 "--with-ensurepip=install" ;install pip and setuptools
422 "--with-computed-gotos" ;main interpreter loop optimization
423 "--enable-unicode=ucs4"
424
425 ;; FIXME: These flags makes Python significantly faster, but
426 ;; leads to non-reproducible binaries.
427 ;; "--with-lto" ;increase size by 20MB, but 15% speedup
428 ;; "--enable-optimizations"
429
430 ;; Prevent the installed _sysconfigdata.py from retaining a reference
431 ;; to coreutils.
432 "INSTALL=install -c"
433 "MKDIR_P=mkdir -p"
434
435 ;; Disable runtime check failing if cross-compiling, see:
436 ;; https://lists.yoctoproject.org/pipermail/poky/2013-June/008997.html
437 #$@(if (%current-target-system)
438 #~("ac_cv_buggy_getaddrinfo=no"
439 "ac_cv_file__dev_ptmx=no"
440 "ac_cv_file__dev_ptc=no")
441 #~())
442 (string-append "LDFLAGS=-Wl,-rpath="
443 (assoc-ref %outputs "out") "/lib"
444 " -fno-semantic-interposition")
445 ;; Add a reference to libxcrypt in LIBS so that the sysconfigdata
446 ;; file records it and propagates it to programs linking against
447 ;; Python.
448 (let ((libxcrypt
449 (false-if-exception
450 (dirname
451 (search-input-file %build-inputs
452 "lib/libcrypt.so.1")))))
453 (string-append
454 "LIBS="
455 (if libxcrypt
456 (string-append "-L" libxcrypt)
457 ""))))
458 ;; With no -j argument tests use all available cpus, so provide one.
459 #:make-flags
460 #~(list (string-append
461 (format #f "TESTOPTS=-j~d" (parallel-job-count))
462 ;; Exclude the following tests as they fail
463 ;; non-deterministically with "error: [Errno 104] Connection
464 ;; reset by peer." Python 3 seems unaffected. A potential fix,
465 ;; yet to be backported to Python 2, is available at:
466 ;; https://github.com/python/cpython/commit/529525fb5a8fd9b96ab4021311a598c77588b918.
467 " --exclude test_urllib2_localnet test_httplib"))
468
469 #:modules '((ice-9 ftw)
470 (ice-9 match)
471 (guix build utils)
472 (guix build gnu-build-system))
473 #:phases
474 #~(modify-phases %standard-phases
475 #$@(common-python-phases-edits)
476 #$@(if (system-hurd?)
477 #~((add-before 'build 'patch-regen-for-hurd
478 (lambda* (#:key inputs #:allow-other-keys)
479 (let ((libc (assoc-ref inputs "libc")))
480 (substitute* "Lib/plat-generic/regen"
481 (("/usr/include/") (string-append libc "/include/")))))))
482 #~())
483 (add-after 'move-idle 'rebuild-bytecode
484 (lambda* (#:key outputs #:allow-other-keys)
485 (let ((out (assoc-ref outputs "out")))
486 ;; Disable hash randomization to ensure the generated .pycs
487 ;; are reproducible.
488 (setenv "PYTHONHASHSEED" "0")
489 (for-each
490 (lambda (output)
491 (for-each (lambda (opt)
492 (format #t "Compiling with optimization level: ~a\n"
493 (if (null? opt) "none" (car opt)))
494 (apply invoke
495 `(,#$(if (%current-target-system)
496 "python2"
497 #~(string-append out "/bin/python"))
498 ,@opt
499 "-m" "compileall"
500 "-f" ; force rebuild
501 ;; Don't build lib2to3, because it contains
502 ;; Python 3 code.
503 "-x" "lib2to3/.*"
504 ,output)))
505 ;; Python 2 has a single file extension (.pyo) for the
506 ;; chosen level of optimization, so it doesn't make
507 ;; sense to byte compile with more than one level.
508 (list '() '("-OO"))))
509 (map cdr outputs)))))
510 (add-after 'install 'install-sitecustomize.py
511 #$(customize-site version)))))
512 (inputs
513 (list bzip2
514 expat
515 gdbm
516 libffi ; for ctypes
517 libxcrypt ; crypto module slated for removal in 3.13, re-enable
518 ; python tests of libxcrypt when that happens
519 sqlite ; for sqlite extension
520 openssl-1.1
521 readline
522 zlib
523 tcl
524 tk)) ; for tkinter
525 (native-inputs
526 `(("pkg-config" ,pkg-config)
527 ("sitecustomize.py" ,(local-file (search-auxiliary-file
528 "python/sitecustomize.py")))
529 ;; When cross-compiling, a native version of Python itself is needed.
530 ,@(if (%current-target-system)
531 `(("python2" ,this-package)
532 ("which" ,which))
533 '())))
534 (native-search-paths
535 (list (guix-pythonpath-search-path version)))
536 (home-page "https://www.python.org")
537 (synopsis "High-level, dynamically-typed programming language")
538 (description
539 "Python is a remarkably powerful dynamic programming language that
540is used in a wide variety of application domains. Some of its key
541distinguishing features include: clear, readable syntax; strong
542introspection capabilities; intuitive object orientation; natural
543expression of procedural code; full modularity, supporting hierarchical
544packages; exception-based error handling; and very high level dynamic
545data types.")
546 (properties '((cpe-name . "python")
547 (cpe-vendor . "python")
548 ;; CVE-2023-36632 is not a bug according to
549 ;; https://github.com/python/cpython/issues/103800
550 (lint-hidden-cve . ("CVE-2023-36632"))))
551 (license license:psfl)))
552
553;; Current 2.x version.
554(define-public python-2 python-2.7)
555
556(define-public python-3.10
557 (package
558 (inherit python-2)
559 (name "python")
560 (version "3.10.22")
561 (source (origin
562 (method url-fetch)
563 (uri (string-append "https://www.python.org/ftp/python/"
564 version "/Python-" version ".tar.xz"))
565 (patches (search-patches
566 "python-3-arm-alignment.patch"
567 "python-3-deterministic-build-info.patch"
568 "python-3-fix-tests.patch"
569 "python-3-hurd-configure.patch"
570 "python-3-reproducible-build.patch"
571 "python-3-search-paths.patch"))
572 (sha256
573 (base32
574 "0hr5rgybvxdb4390cdlym0jm72ipghc69rl80c5jmv57l6qyj7y6"))
575 (modules '((guix build utils)))
576 (snippet
577 '(begin
578 ;; Delete the bundled copy of libexpat.
579 (delete-file-recursively "Modules/expat")
580 (substitute* "Modules/Setup"
581 ;; Link Expat instead of embedding the bundled one.
582 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
583 ;; Delete windows binaries
584 (for-each delete-file
585 (find-files "Lib/distutils/command" "\\.exe$"))))))
586 (build-system gnu-build-system)
587 (arguments
588 (list
589 #:test-target "test"
590 #:configure-flags
591 ;; Avoid inheriting phases, but configure-flags is fine, as
592 ;; it doesn't propagate to later python version.
593 (let-keywords (package-arguments python-2) #t
594 ((configure-flags #~(list)))
595 #~(append #$configure-flags
596 ;; XXX Use quote to avoid world rebuild at this time
597 '("--without-static-libpython")))
598 #:make-flags
599 #~(list (string-append
600 (format #f "TESTOPTS=-j~d" (parallel-job-count))
601 ;; test_mmap fails on low-memory systems
602 " --exclude test_mmap test_socket"
603 " test_ssl" ; started failing with [email protected]
604 #$@(if (system-hurd?)
605 #~(" test_posix" ;multiple errors
606 " test_time"
607 " test_pty"
608 " test_shutil"
609 " test_tempfile" ;chflags: invalid argument:
610 ; tbv14c9t/dir0/dir0/dir0/test0.txt
611 " test_asyncio" ;runs over 10min
612 " test_os" ;stty: 'standard input':
613 ; Inappropriate ioctl for device
614 " test_openpty" ;No such file or directory
615 " test_selectors" ;assertEqual(NUM_FDS // 2, len(fds))
616 ; 32752 != 4
617 " test_compileall" ;multiple errors
618 " test_poll" ;list index out of range
619 " test_subprocess" ;runs over 10min
620 " test_asyncore" ;multiple errors
621 " test_threadsignals"
622 " test_eintr" ;Process return code is -14
623 " test_io" ;multiple errors
624 " test_logging"
625 " test_signal"
626 " test_threading" ;runs over 10min
627 " test_flags" ;ERROR
628 " test_bidirectional_pty"
629 " test_create_unix_connection"
630 " test_unix_sock_client_ops"
631 " test_open_unix_connection"
632 " test_open_unix_connection_error"
633 " test_read_pty_output"
634 " test_write_pty"
635 " test_concurrent_futures" ;freeze
636 " test_venv" ;freeze
637 " test_multiprocessing_forkserver" ;runs over 10min
638 " test_multiprocessing_spawn" ;runs over 10min
639 " test_builtin"
640 " test_capi"
641 " test_dbm_ndbm"
642 " test_exceptions"
643 " test_faulthandler"
644 " test_getopt"
645 " test_importlib"
646 " test_json"
647 " test_multiprocessing_fork"
648 " test_multiprocessing_main_handling"
649 " test_pdb "
650 " test_regrtest"
651 " test_sqlite")
652 #~())))
653 #:modules '((ice-9 ftw)
654 (ice-9 match)
655 (guix build utils)
656 (guix build gnu-build-system))
657 #:phases
658 #~(modify-phases %standard-phases
659 #$@(common-python3-phases-edits)
660 (replace 'remove-windows-binaries
661 (lambda _
662 ;; Delete .exe from embedded .whl (zip) files
663 (for-each
664 (lambda (whl)
665 (let ((dir "whl-content")
666 (circa-1980 (* 10 366 24 60 60)))
667 (mkdir-p dir)
668 (with-directory-excursion dir
669 (let ((whl (string-append "../" whl)))
670 (invoke "unzip" whl)
671 (for-each delete-file
672 (find-files "." "\\.exe$"))
673 (delete-file whl)
674
675 ;; Search for cacert.pem, delete it, and rewrite the
676 ;; file which directs python to look for it.
677 (let ((cacert (find-files "." "cacert\\.pem")))
678 (unless (null? cacert)
679 (let ((certifi (dirname (car cacert))))
680 (delete-file (string-append certifi "/cacert.pem"))
681 (delete-file (string-append certifi "/core.py"))
682 (with-output-to-file (string-append certifi "/core.py")
683 (lambda _
684 (display "\"\"\"
685certifi.py
686~~~~~~~~~~
687This file is a Guix-specific version of core.py.
688
689This module returns the installation location of SSL_CERT_FILE or
690/etc/ssl/certs/ca-certificates.crt, or its contents.
691\"\"\"
692import os
693
694_CA_CERTS = None
695
696try:
697 _CA_CERTS = os.environ [\"SSL_CERT_FILE\"]
698except:
699 _CA_CERTS = os.path.join(\"/etc\", \"ssl\", \"certs\", \"ca-certificates.crt\")
700
701def where() -> str:
702 return _CA_CERTS
703
704def contents() -> str:
705 with open(where(), \"r\", encoding=\"ascii\") as data:
706 return data.read()"))))))
707
708 ;; Reset timestamps to prevent them from ending
709 ;; up in the Zip archive.
710 (ftw "." (lambda (file stat flag)
711 (utime file circa-1980 circa-1980)
712 #t))
713 (apply invoke "zip" "-X" whl
714 (find-files "." #:directories? #t))))
715 (delete-file-recursively dir)))
716 (find-files "Lib/ensurepip" "\\.whl$"))))
717 (add-after 'install 'install-sitecustomize.py
718 #$(customize-site version)))))
719 (inputs
720 (modify-inputs (package-inputs python-2.7)
721 (replace "openssl" openssl)))
722 (native-inputs
723 (let ((inputs (modify-inputs native-inputs
724 (prepend tzdata-for-tests
725 unzip
726 (@ (gnu packages compression) zip)))))
727 (if (%current-target-system)
728 (modify-inputs inputs (prepend this-package))
729 inputs)))
730 (native-search-paths
731 (list (guix-pythonpath-search-path version)
732 ;; Used to locate tzdata by the zoneinfo module introduced in
733 ;; Python 3.9.
734 (search-path-specification
735 (variable "PYTHONTZPATH")
736 (files (list "share/zoneinfo")))))))
737
738(define-public python-3.11
739 (package
740 (inherit python-3.10)
741 (name "python")
742 (version "3.11.17")
743 (source (origin
744 (method url-fetch)
745 (uri (string-append "https://www.python.org/ftp/python/"
746 version "/Python-" version ".tar.xz"))
747 (patches (search-patches
748 "python-3-deterministic-build-info.patch"
749 "python-3.11-fix-tests.patch"
750 "python-3-hurd-configure.patch"
751 "python-3-search-paths.patch"))
752 (sha256
753 (base32
754 "168ml5nn9dfdn70cylb8qn99j3z012s4l4qga7d7rqpskv9lmdxz"))
755 (modules '((guix build utils)))
756 (snippet
757 '(begin
758 ;; Delete the bundled copy of libexpat.
759 (delete-file-recursively "Modules/expat")
760 (substitute* "Modules/Setup"
761 ;; Link Expat instead of embedding the bundled one.
762 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
763 ;; Delete windows binaries
764 (for-each delete-file
765 (find-files "Lib/distutils/command" "\\.exe$"))))))
766 (arguments
767 (list
768 #:test-target "test"
769 #:configure-flags
770 #~(list "--enable-shared" ;allow embedding
771 "--with-system-expat" ;for XML support
772 "--with-system-ffi" ;build ctypes
773 "--with-ensurepip=install" ;install pip and setuptools
774 "--with-computed-gotos" ;main interpreter loop optimization
775 "--enable-unicode=ucs4"
776 "--without-static-libpython"
777 "--enable-loadable-sqlite-extensions"
778
779 ;; FIXME: These flags makes Python significantly faster,
780 ;; but leads to non-reproducible binaries.
781 ;; "--with-lto" ;increase size by 20MB, but 15% speedup
782 ;; "--enable-optimizations"
783
784 ;; Prevent the installed _sysconfigdata.py from retaining
785 ;; a reference to coreutils.
786 "INSTALL=install -c"
787 "MKDIR_P=mkdir -p"
788
789 ;; Add a reference to libxcrypt in LIBS so that the
790 ;; sysconfigdata file records it and propagates it to programs
791 ;; linking against Python.
792 (let ((libxcrypt
793 (false-if-exception
794 (dirname
795 (search-input-file %build-inputs
796 "lib/libcrypt.so.1")))))
797 (string-append
798 "LIBS="
799 (if libxcrypt
800 (string-append "-L" libxcrypt)
801 "")))
802
803 ;; Disable runtime check failing if cross-compiling, see:
804 ;; https://lists.yoctoproject.org/pipermail/poky/2013-June/008997.html
805 #$@(if (%current-target-system)
806 #~("ac_cv_buggy_getaddrinfo=no"
807 "ac_cv_file__dev_ptmx=no"
808 "ac_cv_file__dev_ptc=no"
809 (string-append "--with-build-python="
810 #+(this-package-native-input "python")
811 "/bin/python3"))
812 '())
813 ;; -fno-semantic-interposition reinstates some
814 ;; optimizations by gcc leading to around 15% speedup.
815 ;; This is the default starting from python 3.10.
816 "CFLAGS=-fno-semantic-interposition"
817 (string-append "LDFLAGS=-Wl,-rpath=" #$output "/lib"
818 " -fno-semantic-interposition"))
819 ;; With no -j argument tests use all available cpus, so provide one.
820 #:make-flags
821 `(list (string-append (format #f "TESTOPTS=-j~d"
822 (parallel-job-count))
823 ;; those tests fail on low-memory systems
824 " --exclude"
825 " test_mmap"
826 " test_socket"
827 " test_threading"
828 " test_asyncio"
829 " test_shutdown"
830 " test_ssl" ; started failing with [email protected]
831 ,@(if (target-ppc32?)
832 '(" test_ssl")
833 '())
834 ,@(if (system-hurd?)
835 '(" test_posix" ;multiple errors
836 " test_time"
837 " test_pty"
838 " test_shutil"
839 " test_tempfile" ;chflags: invalid argument:
840 ;; tbv14c9t/dir0/dir0/dir0/test0.txt
841 " test_os" ;stty: 'standard input':
842 ;; Inappropriate ioctl for device
843 " test_openpty" ;No such file or directory
844 " test_selectors" ;assertEqual(NUM_FDS // 2, len(fds))
845 ;; 32752 != 4
846 " test_compileall" ;multiple errors
847 " test_poll" ;list index out of range
848 " test_subprocess" ;runs over 10min
849 " test_asyncore" ;multiple errors
850 " test_threadsignals"
851 " test_eintr" ;Process return code is -14
852 " test_io" ;multiple errors
853 " test_logging"
854 " test_signal"
855 " test_flags" ;ERROR
856 " test_bidirectional_pty"
857 " test_create_unix_connection"
858 " test_unix_sock_client_ops"
859 " test_open_unix_connection"
860 " test_open_unix_connection_error"
861 " test_read_pty_output"
862 " test_write_pty"
863 " test_concurrent_futures" ;freeze
864 " test_venv" ;freeze
865 " test_multiprocessing_forkserver" ;runs over 10min
866 " test_multiprocessing_spawn" ;runs over 10min
867 " test_glob" ;did not finish in 10h
868 " test_site" ;Invalid argument
869 " test_termios" ;os.openpty() Operation not permitted
870 " test_tty" ;os.openpty() Operation not permitted
871 " test_sqlite3" ;; sqlite3.OperationalError: database is locked
872 " test_tarfile" ;; ELOOP vs ENAMETOOLONG
873 " test_builtin"
874 " test_capi"
875 " test_dbm_ndbm"
876 " test_exceptions"
877 " test_faulthandler"
878 " test_getopt"
879 " test_importlib"
880 " test_json"
881 " test_multiprocessing_fork"
882 " test_multiprocessing_main_handling"
883 " test_pdb "
884 " test_regrtest"
885 " test_ssl" ;; Fails with openssl 3.5
886 " test_sqlite")
887 '())
888 ,@(if (system-hurd64?)
889 '(" test_largefile") ;; hangs
890 '())))
891
892 #:modules
893 '((ice-9 ftw)
894 (ice-9 match)
895 (guix build utils)
896 (guix build gnu-build-system))
897
898 #:phases
899 #~(modify-phases %standard-phases
900 #$@(common-python3-phases-edits)
901 (add-after 'install 'install-sitecustomize.py
902 #$(customize-site version)))))
903 (inputs (list bzip2
904 expat
905 gdbm
906 libffi ;for ctypes
907 libxcrypt ;crypto module slated for removal in 3.13
908 sqlite ;for sqlite extension
909 openssl
910 readline
911 zlib
912 tcl
913 tk)) ;for tkinter
914 (native-inputs `(("tzdata" ,tzdata-for-tests)
915 ("unzip" ,unzip)
916 ("zip" ,(@ (gnu packages compression) zip))
917 ("pkg-config" ,pkg-config)
918 ("sitecustomize.py" ,(local-file (search-auxiliary-file
919 "python/sitecustomize.py")))
920 ;; When cross-compiling, a native version of Python itself is needed.
921 ,@(if (%current-target-system)
922 `(("python" ,this-package)
923 ("which" ,which))
924 '())))
925 (native-search-paths
926 (list (guix-pythonpath-search-path version)
927 ;; Used to locate tzdata by the zoneinfo module introduced in
928 ;; Python 3.9.
929 (search-path-specification
930 (variable "PYTHONTZPATH")
931 (files (list "share/zoneinfo")))))))
932
933(define-public python-3.12
934 (package
935 (name "python")
936 (version "3.12.15")
937 (source
938 (origin
939 (method url-fetch)
940 (uri (string-append "https://www.python.org/ftp/python/" version
941 "/Python-" version ".tar.xz"))
942 (sha256
943 (base32 "0npxjh86qc4ym4z8g4kr9jcv6ahwab7wxq36knczpc7sc4ck5i62"))
944 (patches (search-patches "python-3-deterministic-build-info.patch"
945 "python-3.12-fix-tests.patch"
946 "python-3-hurd-configure.patch"))
947 (modules '((guix build utils)))
948 (snippet '(begin
949 ;; Delete the bundled copy of libexpat.
950 (delete-file-recursively "Modules/expat")
951 (substitute* "Modules/Setup"
952 ;; Link Expat instead of embedding the bundled one.
953 (("^#pyexpat.*")
954 "pyexpat pyexpat.c -lexpat\n"))
955 ;; Delete windows binaries
956 (for-each delete-file
957 (find-files "Lib/distutils/command" "\\.exe$"))))))
958 (outputs '("out" "tk" ;tkinter; adds 50 MiB to the closure
959 "idle")) ;programming environment; weighs 5MB
960 (build-system gnu-build-system)
961 (arguments
962 (list
963 #:test-target "test"
964 #:configure-flags
965 #~(list "--enable-shared" ;allow embedding
966 "--with-system-expat" ;for XML support
967 "--with-system-ffi" ;build ctypes
968 "--with-ensurepip=install" ;install pip and setuptools
969 "--with-computed-gotos" ;main interpreter loop optimization
970 "--enable-unicode=ucs4"
971 "--without-static-libpython"
972 "--enable-loadable-sqlite-extensions"
973
974 ;; FIXME: These flags makes Python significantly faster,
975 ;; but leads to non-reproducible binaries.
976 ;; "--with-lto" ;increase size by 20MB, but 15% speedup
977 ;; "--enable-optimizations"
978
979 ;; Prevent the installed _sysconfigdata.py from retaining
980 ;; a reference to coreutils.
981 "INSTALL=install -c"
982 "MKDIR_P=mkdir -p"
983
984 ;; Disable runtime check failing if cross-compiling, see:
985 ;; https://lists.yoctoproject.org/pipermail/poky/2013-June/008997.html
986 #$@(if (%current-target-system)
987 #~("ac_cv_buggy_getaddrinfo=no"
988 "ac_cv_file__dev_ptmx=no"
989 "ac_cv_file__dev_ptc=no"
990 (string-append "--with-build-python="
991 #+(this-package-native-input "python")
992 "/bin/python3"))
993 #~())
994 ;; -fno-semantic-interposition reinstates some
995 ;; optimizations by gcc leading to around 15% speedup.
996 ;; This is the default starting from python 3.10.
997 "CFLAGS=-fno-semantic-interposition"
998 (string-append "LDFLAGS=-Wl,-rpath="
999 (assoc-ref %outputs "out")
1000 "/lib"
1001 " -fno-semantic-interposition"))
1002 ;; With no -j argument tests use all available cpus, so provide one.
1003 #:make-flags
1004 #~(list (string-append (format #f "TESTOPTS=-j~d"
1005 (parallel-job-count))
1006 ;; those tests fail on low-memory systems
1007 " --exclude"
1008 " test_mmap"
1009 " test_socket"
1010 " test_threading"
1011 " test_asyncio"
1012 " test_shutdown"
1013 #$@(if (target-ppc32?)
1014 #~(" test_ssl")
1015 #~())
1016 #$@(if (system-hurd?)
1017 #~(" test_posix" ;multiple errors
1018 " test_time"
1019 " test_pty"
1020 " test_shutil"
1021 " test_tempfile" ;chflags: invalid argument:
1022 ;; tbv14c9t/dir0/dir0/dir0/test0.txt
1023 " test_os" ;stty: 'standard input':
1024 ;; Inappropriate ioctl for device
1025 " test_openpty" ;No such file or directory
1026 " test_selectors" ;assertEqual(NUM_FDS // 2, len(fds))
1027 ;; 32752 != 4
1028 " test_compileall" ;multiple errors
1029 " test_poll" ;list index out of range
1030 " test_subprocess" ;runs over 10min
1031 " test_asyncore" ;multiple errors
1032 " test_threadsignals"
1033 " test_eintr" ;Process return code is -14
1034 " test_io" ;multiple errors
1035 " test_logging"
1036 " test_signal"
1037 " test_flags" ;ERROR
1038 " test_bidirectional_pty"
1039 " test_create_unix_connection"
1040 " test_unix_sock_client_ops"
1041 " test_open_unix_connection"
1042 " test_open_unix_connection_error"
1043 " test_read_pty_output"
1044 " test_write_pty"
1045 " test_concurrent_futures" ;freeze
1046 " test_venv" ;freeze
1047 " test_multiprocessing_forkserver" ;runs over 10min
1048 " test_multiprocessing_spawn" ;runs over 10min
1049 " test_glob" ;did not finish in 10h
1050 " test_site" ;Invalid argument
1051 " test_termios" ;os.openpty() Operation not permitted
1052 " test_tty" ;os.openpty() Operation not permitted
1053 " test_sqlite3" ;; sqlite3.OperationalError: database is locked
1054 " test_tarfile" ;; ELOOP vs ENAMETOOLONG
1055 " test_builtin"
1056 " test_capi"
1057 " test_dbm_ndbm"
1058 " test_exceptions"
1059 " test_faulthandler"
1060 " test_getopt"
1061 " test_importlib"
1062 " test_json"
1063 " test_multiprocessing_fork"
1064 " test_multiprocessing_main_handling"
1065 " test_pdb "
1066 " test_regrtest"
1067 " test_ssl" ;; Fails with openssl 3.5
1068 " test_sqlite")
1069 #~())
1070 #$@(if (system-hurd64?)
1071 #~(" test_largefile") ;; hangs
1072 #~())))
1073
1074 #:modules '((ice-9 ftw)
1075 (ice-9 match)
1076 (guix build utils)
1077 (guix build gnu-build-system))
1078
1079 #:phases
1080 #~(modify-phases %standard-phases
1081 #$@(common-python3-phases-edits)
1082 (add-after 'install 'install-sitecustomize.py
1083 #$(customize-site version)))))
1084 (inputs (list bzip2
1085 expat
1086 gdbm
1087 libffi ;for ctypes
1088 sqlite ;for sqlite extension
1089 openssl
1090 readline
1091 zlib
1092 tcl
1093 tk)) ;for tkinter
1094 (native-inputs `(("tzdata" ,tzdata-for-tests)
1095 ("unzip" ,unzip)
1096 ("zip" ,(@ (gnu packages compression) zip))
1097 ("pkg-config" ,pkg-config)
1098 ("sitecustomize.py" ,(local-file (search-auxiliary-file
1099 "python/sitecustomize.py")))
1100 ;; When cross-compiling, a native version of Python itself is needed.
1101 ,@(if (%current-target-system)
1102 `(("python" ,this-package)
1103 ("which" ,which))
1104 '())))
1105 (native-search-paths
1106 (list (guix-pythonpath-search-path version)
1107 ;; Used to locate tzdata by the zoneinfo module introduced in
1108 ;; Python 3.9.
1109 (search-path-specification
1110 (variable "PYTHONTZPATH")
1111 (files (list "share/zoneinfo")))))
1112 (home-page "https://www.python.org")
1113 (synopsis "High-level, dynamically-typed programming language")
1114 (description
1115 "Python is a remarkably powerful dynamic programming language that
1116is used in a wide variety of application domains. Some of its key
1117distinguishing features include: clear, readable syntax; strong
1118introspection capabilities; intuitive object orientation; natural
1119expression of procedural code; full modularity, supporting hierarchical
1120packages; exception-based error handling; and very high level dynamic
1121data types.")
1122 (properties '((cpe-name . "python")
1123 (cpe-vendor . "python")))
1124 (license license:psfl)))
1125
1126(define-public python-3.13
1127 (package
1128 (name "python-next")
1129 (version "3.13.16")
1130 (source
1131 (origin
1132 (method url-fetch)
1133 (uri (string-append "https://www.python.org/ftp/python/" version
1134 "/Python-" version ".tar.xz"))
1135 (sha256
1136 (base32 "0pdkdb5vc9jziy6q4yfnnkdc1d33858152i2ildv2nwvqyrvzcgl"))
1137 (patches (search-patches "python-3-deterministic-build-info.patch"
1138 "python-3.13-fix-tests.patch"
1139 "python-3-hurd-configure.patch"))
1140 (modules '((guix build utils)))
1141 (snippet '(begin
1142 ;; Delete the bundled copy of libexpat.
1143 (delete-file-recursively "Modules/expat")
1144 (substitute* "Modules/Setup"
1145 ;; Link Expat instead of embedding the bundled one.
1146 (("^#pyexpat.*")
1147 "pyexpat pyexpat.c -lexpat\n"))
1148 ;; Delete windows binaries
1149 (for-each delete-file
1150 (find-files "Lib/distutils/command" "\\.exe$"))))))
1151 (outputs '("out" "tk" ;tkinter; adds 50 MiB to the closure
1152 "idle")) ;programming environment; weighs 5MB
1153 (build-system gnu-build-system)
1154 (arguments
1155 (list
1156 #:test-target "test"
1157 #:configure-flags
1158 #~(list "--enable-shared" ;allow embedding
1159 "--with-system-expat" ;for XML support
1160 "--with-system-ffi" ;build ctypes
1161 "--with-ensurepip=install" ;install pip and setuptools
1162 "--with-computed-gotos" ;main interpreter loop optimization
1163 "--enable-unicode=ucs4"
1164 "--without-static-libpython"
1165 "--enable-loadable-sqlite-extensions"
1166
1167 ;; FIXME: These flags makes Python significantly faster,
1168 ;; but leads to non-reproducible binaries.
1169 ;; "--with-lto" ;increase size by 20MB, but 15% speedup
1170 ;; "--enable-optimizations"
1171
1172 ;; Prevent the installed _sysconfigdata.py from retaining
1173 ;; a reference to coreutils.
1174 "INSTALL=install -c"
1175 "MKDIR_P=mkdir -p"
1176
1177 ;; Disable runtime check failing if cross-compiling, see:
1178 ;; https://lists.yoctoproject.org/pipermail/poky/2013-June/008997.html
1179 #$@(if (%current-target-system)
1180 #~("ac_cv_buggy_getaddrinfo=no"
1181 "ac_cv_file__dev_ptmx=no"
1182 "ac_cv_file__dev_ptc=no"
1183 (string-append "--with-build-python="
1184 #+(this-package-native-input "python")
1185 "/bin/python3"))
1186 #~())
1187 ;; -fno-semantic-interposition reinstates some
1188 ;; optimizations by gcc leading to around 15% speedup.
1189 ;; This is the default starting from python 3.10.
1190 "CFLAGS=-fno-semantic-interposition"
1191 (string-append "LDFLAGS=-Wl,-rpath="
1192 (assoc-ref %outputs "out")
1193 "/lib"
1194 " -fno-semantic-interposition"))
1195 ;; With no -j argument tests use all available cpus, so provide one.
1196 #:make-flags
1197 #~(list (string-append (format #f "TESTOPTS=-j~d"
1198 (parallel-job-count))
1199 ;; those tests fail on low-memory systems
1200 " --exclude"
1201 " test_mmap"
1202 " test_socket"
1203 " test_threading"
1204 " test_asyncio"
1205 " test_shutdown"
1206 " test_pdb" ;doctest fails
1207 #$@(if (target-ppc32?)
1208 #~(" test_ssl")
1209 #~())
1210 #$@(if (system-hurd?)
1211 #~(" test_posix" ;multiple errors
1212 " test_time"
1213 " test_pty"
1214 " test_shutil"
1215 " test_tempfile" ;chflags: invalid argument:
1216 ;; tbv14c9t/dir0/dir0/dir0/test0.txt
1217 " test_os" ;stty: 'standard input':
1218 ;; Inappropriate ioctl for device
1219 " test_openpty" ;No such file or directory
1220 " test_selectors" ;assertEqual(NUM_FDS // 2, len(fds))
1221 ;; 32752 != 4
1222 " test_compileall" ;multiple errors
1223 " test_poll" ;list index out of range
1224 " test_subprocess" ;runs over 10min
1225 " test_asyncore" ;multiple errors
1226 " test_threadsignals"
1227 " test_eintr" ;Process return code is -14
1228 " test_io" ;multiple errors
1229 " test_logging"
1230 " test_signal"
1231 " test_flags" ;ERROR
1232 " test_bidirectional_pty"
1233 " test_create_unix_connection"
1234 " test_unix_sock_client_ops"
1235 " test_open_unix_connection"
1236 " test_open_unix_connection_error"
1237 " test_read_pty_output"
1238 " test_write_pty"
1239 " test_concurrent_futures" ;freeze
1240 " test_venv" ;freeze
1241 " test_multiprocessing_forkserver" ;runs over 10min
1242 " test_multiprocessing_spawn" ;runs over 10min
1243 " test_glob" ;did not finish in 10h
1244 " test_site" ;Invalid argument
1245 " test_termios" ;os.openpty() Operation not permitted
1246 " test_tty" ;os.openpty() Operation not permitted
1247 " test_sqlite3" ;; sqlite3.OperationalError: database is locked
1248 " test_tarfile" ;; ELOOP vs ENAMETOOLONG
1249 " test_builtin"
1250 " test_capi"
1251 " test_dbm_ndbm"
1252 " test_exceptions"
1253 " test_faulthandler"
1254 " test_getopt"
1255 " test_importlib"
1256 " test_json"
1257 " test_multiprocessing_fork"
1258 " test_multiprocessing_main_handling"
1259 " test_regrtest"
1260 " test_ssl" ;; Fails with openssl 3.5
1261 " test_sqlite")
1262 #~())
1263 #$@(if (system-hurd64?)
1264 #~(" test_largefile") ;; hangs
1265 #~())))
1266
1267 #:modules '((ice-9 ftw)
1268 (ice-9 match)
1269 (guix build utils)
1270 (guix build gnu-build-system))
1271
1272 #:phases
1273 #~(modify-phases %standard-phases
1274 #$@(common-python3-phases-edits)
1275 (add-after 'install 'install-sitecustomize.py
1276 #$(customize-site version)))))
1277 (inputs (list bzip2
1278 expat
1279 gdbm
1280 libffi ;for ctypes
1281 sqlite ;for sqlite extension
1282 openssl
1283 readline
1284 zlib
1285 tcl
1286 tk)) ;for tkinter
1287 (native-inputs `(("tzdata" ,tzdata-for-tests)
1288 ("unzip" ,unzip)
1289 ("zip" ,(@ (gnu packages compression) zip))
1290 ("pkg-config" ,pkg-config)
1291 ("sitecustomize.py" ,(local-file (search-auxiliary-file
1292 "python/sitecustomize.py")))
1293 ;; When cross-compiling, a native version of Python itself is needed.
1294 ,@(if (%current-target-system)
1295 `(("python" ,this-package)
1296 ("which" ,which))
1297 '())))
1298 (native-search-paths
1299 (list (guix-pythonpath-search-path version)
1300 ;; Used to locate tzdata by the zoneinfo module introduced in
1301 ;; Python 3.9.
1302 (search-path-specification
1303 (variable "PYTHONTZPATH")
1304 (files (list "share/zoneinfo")))))
1305 (home-page "https://www.python.org")
1306 (synopsis "High-level, dynamically-typed programming language")
1307 (description
1308 "Python is a remarkably powerful dynamic programming language that
1309is used in a wide variety of application domains. Some of its key
1310distinguishing features include: clear, readable syntax; strong
1311introspection capabilities; intuitive object orientation; natural
1312expression of procedural code; full modularity, supporting hierarchical
1313packages; exception-based error handling; and very high level dynamic
1314data types.")
1315 (properties '((cpe-name . "python")
1316 (cpe-vendor . "python")))
1317 (license license:psfl)))
1318
1319;; Next 3.x version.
1320;; Make sure that the name of the package is altered as well to avoid
1321;; conflicts, see: <https://codeberg.org/guix/guix/issues/9255>.
1322(define-public python-next python-3.13)
1323
1324;; Current 3.x version.
1325(define-public python-3 python-3.12)
1326
1327;; Current major version.
1328(define-public python python-3)
1329
1330;; Minimal variant of Python, mostly used to break the cycle between Tk and
1331;; Python (Tk -> libxcb -> Python.)
1332
1333;; XXX: Consider to slim down tests and #:make-flags or break inheritance from
1334;; current default variant. Any change to default python affect python-minimal
1335;; and tigers the whole bootstrap chain rebuild.
1336
1337(define-public python-minimal
1338 (package/inherit python
1339 (name "python-minimal")
1340 (outputs '("out"))
1341
1342 ;; Build fails due to missing ctypes without libffi.
1343 ;; OpenSSL is a mandatory dependency of Python 3.x, for urllib;
1344 ;; zlib is required by 'zipimport', used by pip. Expat is needed
1345 ;; for XML support, which is generally expected to be available.
1346 (inputs (list expat libffi openssl zlib))))
1347
1348(define-public python-next-minimal
1349 (package/inherit python-next
1350 (name "python-next-minimal")
1351 (outputs '("out"))
1352 (inputs (list expat libffi openssl zlib))))
1353
1354(define-public python-debug
1355 (package/inherit python
1356 (name "python-debug")
1357 (outputs '("out" "debug"))
1358 (build-system gnu-build-system)
1359 (arguments
1360 (substitute-keyword-arguments arguments
1361 ((#:configure-flags configure-flags)
1362 #~(append #$configure-flags
1363 (list "--with-pydebug")))))
1364 (synopsis "Python with the debug hooks enabled")
1365 (description
1366 "This variant of Python provides an interpreter built with
1367@code{--with-pydebug} to help develop and debug extensions. See
1368@url{https://pythonextensionpatterns.readthedocs.io/en/latest/debugging/debug.html},
1369for more information.")))
1370
1371(define* (wrap-python3 python
1372 #:optional
1373 (name (string-append (package-name python) "-wrapper")))
1374 (package/inherit python
1375 (name name)
1376 (source #f)
1377 (build-system trivial-build-system)
1378 (outputs '("out"))
1379 (inputs `(("bash" ,bash)))
1380 (propagated-inputs `(("python" ,python)))
1381 (arguments
1382 (list #:modules '((guix build utils))
1383 #:builder
1384 #~(begin
1385 (use-modules (guix build utils))
1386 (let ((bin (string-append #$output "/bin"))
1387 (python (string-append
1388 ;; XXX: '%build-inputs' contains the native
1389 ;; Python when cross-compiling.
1390 #$(if (%current-target-system)
1391 (this-package-input "python")
1392 #~(assoc-ref %build-inputs "python"))
1393 "/bin/")))
1394 (mkdir-p bin)
1395 (for-each
1396 (lambda (old new)
1397 (symlink (string-append python old)
1398 (string-append bin "/" new)))
1399 `("python3" ,"pydoc3" ,"pip3")
1400 `("python" ,"pydoc" ,"pip"))
1401 ;; python-config outputs search paths based upon its location,
1402 ;; use a bash wrapper to avoid changing its outputs.
1403 (let ((bash (string-append (assoc-ref %build-inputs "bash")
1404 "/bin/bash"))
1405 (old (string-append python "python3-config"))
1406 (new (string-append bin "/python-config")))
1407 (with-output-to-file new
1408 (lambda ()
1409 (format #t "#!~a~%" bash)
1410 (format #t "exec \"~a\" \"$@\"~%" old)
1411 (chmod new #o755))))))))
1412 (synopsis "Wrapper for the Python 3 commands")
1413 (description
1414 "This package provides wrappers for the commands of Python@tie{}3.x such
1415that they can also be invoked under their usual names---e.g., @command{python}
1416instead of @command{python3} or @command{pip} instead of @command{pip3}.
1417
1418To function properly, this package should not be installed together with the
1419@code{python} package: this package uses the @code{python} package as a
1420propagated input, so installing this package already makes both the versioned
1421and the unversioned commands available.")))
1422
1423(define-public python-wrapper (wrap-python3 python))
1424(define-public python-minimal-wrapper (wrap-python3 python-minimal))
1425
1426(define-public python-next-wrapper (wrap-python3 python-next))
1427(define-public python-next-minimal-wrapper (wrap-python3 python-next-minimal))
1428
1429;; The Python used in pyproject-build-system.
1430(define-public (remove-pip python)
1431 (hidden-package
1432 (package/inherit python
1433 (arguments
1434 (substitute-keyword-arguments arguments
1435 ((#:configure-flags flags #~())
1436 #~(append '("--with-ensurepip=no")
1437 (delete "--with-ensurepip=install" #$flags))))))))
1438
1439(define-public python-sans-pip
1440 (remove-pip python))
1441
1442(define-public python-sans-pip-wrapper
1443 (wrap-python3 python-sans-pip))
1444
1445(define-public python-next-sans-pip
1446 (remove-pip python-next))
1447
1448(define-public python-next-sans-pip-wrapper
1449 (wrap-python3 python-next-sans-pip))
1450
1451(define-public python-next-setuptools-bootstrap
1452 ((package-input-rewriting/spec
1453 `(("python" . ,(const python-next-sans-pip))))
1454 python-setuptools-bootstrap))
1455
1456(define-public with-python-next
1457 (package-input-rewriting
1458 `((,python . ,python-next)
1459 (,python-minimal . ,python-next-minimal)
1460 (,python-wrapper . ,python-next-wrapper)
1461 (,python-minimal-wrapper . ,python-next-minimal-wrapper)
1462 (,python-sans-pip . ,python-next-sans-pip)
1463 (,python-sans-pip-wrapper . ,python-next-sans-pip-wrapper)
1464 (,python-setuptools-bootstrap . ,python-next-setuptools-bootstrap)
1465 (,python-pytest-guix . ,python-next-pytest-guix))))
1466
1467(define-public pythoncapi-compat
1468 ;; No release nor tags: use the latest commit.
1469 (let ((commit "b8fe76f597af6593dfcb43a6dd388bab98938b91")
1470 (revision "2"))
1471 (package
1472 (name "pythoncapi-compat")
1473 (version (git-version "0.0.0" revision commit))
1474 (source (origin
1475 (method git-fetch)
1476 (uri (git-reference
1477 (url "https://github.com/python/pythoncapi-compat")
1478 (commit commit)))
1479 (file-name (git-file-name name version))
1480 (sha256
1481 (base32
1482 "1br8adfqr8m3lsjrmzxkhx2q4rxzlmvyww6cxn8m7q5gh1w61432"))))
1483 (build-system copy-build-system)
1484 (arguments (list #:install-plan
1485 #~'(("pythoncapi_compat.h" "include/"))))
1486 (home-page "https://github.com/python/pythoncapi-compat")
1487 (synopsis "Python C API compatibility")
1488 (description "The pythoncapi-compat project can be used to write a C or
1489C++ extension supporting a wide range of Python versions with a single code
1490base, via the @file{pythoncapi_compat.h} header file.")
1491 (license license:bsd-0))))
1492
1493(define-public micropython
1494 (package
1495 (name "micropython")
1496 (version "1.29.0")
1497 (source
1498 (origin
1499 (method url-fetch)
1500 (uri (string-append "https://github.com/micropython/micropython/"
1501 "releases/download/v" version
1502 "/micropython-" version ".tar.xz"))
1503 (sha256
1504 (base32 "187cxw8mfv1vazpd82ssqcf08dr2bsx8bcpw7pgjp6p7ck3af9fr"))
1505 (modules '((guix build utils)))
1506 (snippet
1507 '(begin
1508 (delete-file-recursively "ports/cc3200/FreeRTOS")
1509 (with-directory-excursion "lib"
1510 ;; TODO: Unbundle axtls and berkley-db-1.xx
1511 (for-each delete-file-recursively
1512 '("libffi" "lwip" "stm32lib" "nrfx")))))))
1513 (build-system gnu-build-system)
1514 (arguments
1515 (list #:phases
1516 #~(modify-phases %standard-phases
1517 (add-before 'build 'build-mpy-cross
1518 (lambda* (#:key make-flags #:allow-other-keys)
1519 (with-directory-excursion "mpy-cross"
1520 (apply invoke "make" make-flags))))
1521 (add-after 'build-mpy-cross 'prepare-build
1522 (lambda _
1523 (chdir "ports/unix")
1524 ;; see: https://github.com/micropython/micropython/pull/4246
1525 (substitute* "Makefile"
1526 (("-Os") "-Os -ffp-contract=off"))))
1527 (add-before 'install-license-files 'chdir-back
1528 ;; We don't build in the root directory so the file isn't found.
1529 (lambda _
1530 (chdir "../..")))
1531 (delete 'configure)) ; no configure
1532 #:make-flags
1533 #~(list (string-append "PREFIX=" #$output)
1534 "V=1")
1535 #:test-target "test"))
1536 (native-inputs (list pkg-config python-wrapper))
1537 (inputs
1538 (list libffi))
1539 (home-page "https://micropython.org/")
1540 (synopsis "Python implementation for microcontrollers and constrained systems")
1541 (description "MicroPython is a lean and efficient implementation of the
1542Python 3 programming language that includes a small subset of the Python
1543standard library and is optimised to run on microcontrollers and in constrained
1544environments. MicroPython is packed full of advanced features such as an
1545interactive prompt, arbitrary precision integers, closures, list comprehension,
1546generators, exception handling and more. Still it is compact enough to fit and
1547run within just 256k of code space and 16k of RAM. MicroPython aims to be as
1548compatible with normal Python as possible to allow you to transfer code with
1549ease from the desktop to a microcontroller or embedded system.")
1550 (license license:expat)))