Skip to content

Incorrect conversion of Python object to R, possibly due to integer overflow #1835

@TengMCing

Description

@TengMCing

Hi reticulate developers,

I ran into a weird issue using reticulate: when I compute the sum of squares of a sequence of large numbers and convert it back into R, it seems like an integer overflow occurs. There’s no warning and no automatic conversion to double, which can be confusing. It would be helpful if either a warning was shown or the data was automatically converted to double.

library(reticulate)
py_run_string('def cum_square(n): return sum([x**2 for x in range(n + 1)])')
py$cum_square(10000L)
#> [1] -1624114088

Created on 2025-09-10 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       macOS Sequoia 15.3
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Australia/Sydney
#>  date     2025-09-10
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.4.1)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.4.1)
#>  evaluate      1.0.3   2025-01-10 [1] CRAN (R 4.4.1)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.4.1)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.4.1)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  jsonlite      2.0.0   2025-03-27 [1] CRAN (R 4.4.1)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.4.1)
#>  lattice       0.22-6  2024-03-20 [1] CRAN (R 4.4.2)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  Matrix        1.7-1   2024-10-18 [1] CRAN (R 4.4.2)
#>  png           0.1-8   2022-11-29 [1] CRAN (R 4.4.0)
#>  Rcpp          1.0.14  2025-01-12 [1] CRAN (R 4.4.1)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.4.0)
#>  reticulate  * 1.43.0  2025-07-21 [1] CRAN (R 4.4.1)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.4.1)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.4.2)
#>  rstudioapi    0.17.1  2024-10-22 [1] CRAN (R 4.4.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.4.1)
#>  xfun          0.52    2025-04-02 [1] CRAN (R 4.4.1)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ─ Python configuration ───────────────────────────────────────────────────────
#>  python:         /Users/patrickli/.virtualenvs/tf/bin/python
#>  libpython:      /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/config-3.11-darwin/libpython3.11.dylib
#>  pythonhome:     /Users/patrickli/.virtualenvs/tf:/Users/patrickli/.virtualenvs/tf
#>  version:        3.11.9 (v3.11.9:de54cf5be3, Apr  2 2024, 07:12:50) [Clang 13.0.0 (clang-1300.0.29.30)]
#>  numpy:          /Users/patrickli/.virtualenvs/tf/lib/python3.11/site-packages/numpy
#>  numpy_version:  1.24.2
#>  
#>  NOTE: Python version was forced by VIRTUAL_ENV
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions