The behavior is similar to the bug from #151.
(test_asizeof) rxue@xenon:~$ ipython
Python 3.8.15 | packaged by conda-forge | (default, Nov 22 2022, 08:49:35)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.7.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
...: from pympler.asizeof import asizeof
...: x=np.str_(' '*100)
...: asizeof(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 4
2 from pympler.asizeof import asizeof
3 x=np.str_(' '*100)
----> 4 asizeof(x)
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:2603, in asizeof(*objs, **opts)
2601 if x: # don't size, profile or rank _getobjects tuple
2602 _asizer.exclude_objs(t)
-> 2603 s = _asizer.asizeof(*t)
2604 _asizer.print_stats(objs=t, opts=opts) # show opts as _kwdstr
2605 _asizer._clear()
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:2032, in Asizer.asizeof(self, *objs, **opts)
2030 self.set(**opts)
2031 self.exclude_refs(*objs) # skip refs to objs
-> 2032 return sum(self._sizer(o, 0, 0, None) for o in objs)
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:2032, in <genexpr>(.0)
2030 self.set(**opts)
2031 self.exclude_refs(*objs) # skip refs to objs
-> 2032 return sum(self._sizer(o, 0, 0, None) for o in objs)
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:1937, in Asizer._sizer(self, obj, pid, deep, sized)
1935 v = _typedefs.get(k, None)
1936 if not v: # new typedef
-> 1937 _typedefs[k] = v = _typedef(obj, derive=self._derive_,
1938 frames=self._frames_,
1939 infer=self._infer_)
1940 if (v.both or self._code_) and v.kind is not self._ign_d:
1941 s = f = v.flat(obj, self._mask) # flat size
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:1597, in _typedef(obj, derive, frames, infer)
1595 v.set(refs=_namedtuple_refs)
1596 elif numpy and _isnumpy(obj): # NumPy data
-> 1597 v.set(**_numpy_kwds(obj))
1598 if _numpy_excl:
1599 _getsizeof_excls_add(t)
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:1183, in _Typedef.set(self, safe_len, **kwds)
1181 d = self.kwds()
1182 d.update(kwds)
-> 1183 self.reset(**d)
1184 if safe_len and self.item:
1185 self.leng = _len
File /opt/mambaforge/envs/test_asizeof/lib/python3.8/site-packages/pympler/asizeof.py:1192, in _Typedef.reset(self, base, item, leng, refs, both, kind, type, vari)
1189 '''Reset all specified attributes.
1190 '''
1191 if base < 0:
-> 1192 raise ValueError('invalid option: %s=%r' % ('base', base))
1193 else:
1194 self.base = base
ValueError: invalid option: base=-219
In [2]:
My understanding is that asizeof
tries to register the base
size of numpy.str_ when it came across this NumPy data type for the first time. However, numpy.str_ has a dynamical size, so the base
from the diff between sys.getsizeof(obj)
and obj.nbytes
becomes a bit unpredictable?
On the other hand, starting fresh, this still works:
(test_asizeof) rxue@xenon:~$ ipython
Python 3.8.15 | packaged by conda-forge | (default, Nov 22 2022, 08:49:35)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.7.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
...: from pympler.asizeof import asizeof
...:
...: x=np.str_()
...: asizeof(x)
...:
...: x=np.str_(' '*100)
...: asizeof(x)
Out[1]: 200
tested with Pympler
ver 1.0.1 and numpy
ver 1.23.5