GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: f2py.crackfortran mutates global re._MAXCACHE at import time · Issue #31104 · numpy/numpy

BUG: f2py.crackfortran mutates global re._MAXCACHE at import time #31104

New issue
Copy link
New issue
Copy link
Closed
#31105
Closed
BUG: f2py.crackfortran mutates global re._MAXCACHE at import time#31104
#31105
Copy link
Labels
00 - Bugcomponent: numpy.f2py

Description

@cmutrufflepig
cmutrufflepig
opened on Mar 30, 2026
Issue body actions

Describe the issue:

https://github.com/numpy/numpy/blob/main/numpy/f2py/crackfortran.py#L246
re._MAXCACHE = 50 takes effect when this file is in the import chain, e.g. when scipy exists
This causes really hard to locate performance issues for NLP workflows. (I don't even know this module exists yet it silently slows my pipeline down by 100x)
Regex's default cache is 512, and evicts LRU style. No reason to change it process-wide, please consider removing it.

Reproduce the code example:

import re
print(re._MAXCACHE)  # default 512
import nltk  # Doesn't even need numpy or regex, all you need is scipy installed or not
print(re._MAXCACHE)  # this now is 50

# if you don't have nltk, this does the trick too
import scipy.linalg

Python and NumPy Versions:

3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)]

But any version would work

Runtime Environment:

[{'numpy_version': '2.4.4',
  'python': '3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 '
            '(clang-1300.0.29.30)]',
  'uname': uname_result(system='Darwin', node='MacBook-Air.local', release='24.6.0', version='Darwin Kernel Version 24.6.0: Mon Jan 19 22:01:41 PST 2026; root:xnu-11417.140.69.708.3~1/RELEASE_ARM64_T8132', machine='arm64')},
 {'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
                      'found': ['ASIMDHP', 'ASIMDDP'],
                      'not_found': ['ASIMDFHM']}},
 {'ignore_floating_point_errors_in_matmul': True}]

How does this issue affect you or how did you find it:

Shrinking re._MAXCACHE silently made re.sub() 100 times slower if your code involves repeatedly calling with high frequency without regex compile.

In my case, I didn't even use numpy, I use NLTK the language pack. And by just including (not using) scipy in my docker image, my workflow is 100 times slower. Because f2py is involved somewhere in the import chain of import nltk.

Reacted by Joren Hammudoglu and Warren Weckesser

Activity

  1. cmutrufflepig
    added
    00 - Bug
    on Mar 30, 2026
  2. WarrenWeckesser
    added
    component: numpy.f2py
    on Mar 30, 2026
  3. WarrenWeckesser commented on Mar 30, 2026

    @WarrenWeckesser
    WarrenWeckesser
    on Mar 30, 2026
    Member
    More actions

    Thanks for reporting the issue, @cmutrufflepig.

    The line

    numpy/numpy/f2py/crackfortran.py

    Line 246 in 8568ed8

    re._MAXCACHE = 50

    has been there for more than 20 years. Since _MAXCACHE appears to be a private module attribute, modifying it like we do is a bad practice (although pointers to evidence to the contrary are welcome).

    I suspect we could simply remove that line and use the default. Computers generally have a lot more memory now than they did 20 years ago. If I remove that line locally, all the tests still pass.

    Reacted by cmutrufflepig
  4. WarrenWeckesser
    added a commit that references this issue on Mar 30, 2026

    MAINT: f2py: Stop setting re._MAXCACHE to 50.

    This issue will close once commit 57a2476 is merged into the 'master' branch.
    57a2476
  5. WarrenWeckesser commented on Mar 30, 2026

    @WarrenWeckesser
    WarrenWeckesser
    on Mar 30, 2026
    Member
    More actions

    PR to remove the line: #31105.

  6. cmutrufflepig commented on Mar 30, 2026

    @cmutrufflepig
    cmutrufflepig
    on Mar 30, 2026
    Author
    More actions

    Hi @WarrenWeckesser
    Thank you for the response. I thought about old hardware limitations, but truly didn't expect it to be 20 year old code.

    Removing that line was my first proposal in the issue too. I really can't think of any good reason for it to exist or deleting it posts any risk. I was gonna create a PR straight, but opened an issue just to be safe.

    When can I use this in a standard release? Right now we patch it by setting it back to 512 after every import.

  7. WarrenWeckesser commented on Mar 30, 2026

    @WarrenWeckesser
    WarrenWeckesser
    on Mar 30, 2026
    Member
    More actions

    When can I use this in a standard release? Right now we patch it by setting it back to 512 after every import.

    The PR will have to be reviewed, and if approved it will be merged into the main development branch. Then it would definitely be in NumPy 2.5.0. I don't know if it will be backported, but if so, it would end up in 2.4.5, since 2.4.4 was released just yesterday. There is no guarantee that there will be a 2.4.5, nor a definite schedule for its release if there is one.

  8. mattip
    closed this as completedin #31105on Mar 30, 2026
  9. charris
    added a commit that references this issue on Apr 10, 2026

    MAINT: f2py: Stop setting re._MAXCACHE to 50.

    This issue will close once commit c58520f is merged into the 'main' branch.
    c58520f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bugcomponent: numpy.f2py

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.