summaryrefslogtreecommitdiff
blob: 2843914f4a8795c3f8cd028fd1275aa499aafae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
https://github.com/PyTables/PyTables/pull/1013

From 9d2487eb53af940de3b5c79200c9f4c2b90f51f2 Mon Sep 17 00:00:00 2001
From: Sam James <sam@gentoo.org>
Date: Mon, 3 Apr 2023 02:07:47 +0100
Subject: [PATCH] Handle py-cpuinfo not being installed

Fallback gracefully if py-cpuinfo isn't installed. We already handle this in
setup.py but we need to avoid calling it in leaf.py too.

py-cpuinfo isn't available on all platforms and PyTables is needed to run
the test suite for some software, so we need to be able to run PyTables
in places where py-cpuinfo isn't yet ported.

Signed-off-by: Sam James <sam@gentoo.org>
--- a/tables/leaf.py
+++ b/tables/leaf.py
@@ -4,7 +4,11 @@ import warnings
 import math
 
 import numpy as np
-import cpuinfo
+try:
+    import cpuinfo
+    missing_cpuinfo = False
+except ImportError:
+    missing_cpuinfo = True
 
 from .flavor import (check_flavor, internal_flavor, toarray,
                      alias_map as flavor_alias_map)
@@ -336,20 +340,21 @@ class Leaf(Node):
             # Use a decent default value for chunksize
             chunksize *= 16
             # Now, go explore the L3 size and try to find a smarter chunksize
-            cpu_info = cpuinfo.get_cpu_info()
-            if 'l3_cache_size' in cpu_info:
-                # In general, is a good idea to set the chunksize equal to L3
-                l3_cache_size = cpu_info['l3_cache_size']
-                # cpuinfo sometimes returns cache sizes as strings (like,
-                # "4096 KB"), so refuse the temptation to guess and use the
-                # value only when it is an actual int.
-                # Also, sometimes cpuinfo does not return a correct L3 size;
-                # so in general, enforcing L3 > L2 is a good sanity check.
-                l2_cache_size = cpu_info.get('l2_cache_size', "Not found")
-                if (type(l3_cache_size) is int and
-                    type(l2_cache_size) is int and
-                    l3_cache_size > l2_cache_size):
-                    chunksize = l3_cache_size
+            if not missing_cpuinfo:
+                cpu_info = cpuinfo.get_cpu_info()
+                if 'l3_cache_size' in cpu_info:
+                    # In general, is a good idea to set the chunksize equal to L3
+                    l3_cache_size = cpu_info['l3_cache_size']
+                    # cpuinfo sometimes returns cache sizes as strings (like,
+                    # "4096 KB"), so refuse the temptation to guess and use the
+                    # value only when it is an actual int.
+                    # Also, sometimes cpuinfo does not return a correct L3 size;
+                    # so in general, enforcing L3 > L2 is a good sanity check.
+                    l2_cache_size = cpu_info.get('l2_cache_size', "Not found")
+                    if (type(l3_cache_size) is int and
+                        type(l2_cache_size) is int and
+                        l3_cache_size > l2_cache_size):
+                        chunksize = l3_cache_size
             # In Blosc2, the chunksize cannot be larger than 2 GB - BLOSC2_MAX_BUFFERSIZE
             if chunksize > 2**31 - 32:
                 chunksize = 2**31 - 32
-- 
2.40.0