python.scm 44 KB

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