Skip to content

Commit

Permalink
Merge pull request #2064 from ablev-nv/vtarray_large_pybuffer
Browse files Browse the repository at this point in the history
[vt] fix for crash when creating VtArray from large python buffer

(Internal change: 2256956)
  • Loading branch information
pixar-oss committed Dec 8, 2022
2 parents cfa3e81 + 7d71b4b commit 1520d4a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pxr/base/vt/arrayPyBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ Vt_ArrayFromBuffer(TfPyObjWrapper const &obj,
// Check that the number of elements matches.
auto multiply = [](Py_ssize_t x, Py_ssize_t y) { return x * y; };
auto numItems = std::accumulate(
view.shape, view.shape + view.ndim, 1, multiply);
view.shape, view.shape + view.ndim, (Py_ssize_t)1, multiply);

// Compute the total # of items in one element.
auto elemShape = Vt_GetElementShape<T>();
auto elemSize = std::accumulate(
elemShape.begin(), elemShape.end(), 1, multiply);
elemShape.begin(), elemShape.end(), (Py_ssize_t)1, multiply);

// Sanity check data sizes.
auto arraySize = numItems / elemSize;
Expand Down
16 changes: 13 additions & 3 deletions pxr/base/vt/testenv/testVtArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# pylint: disable=range-builtin-not-iterating
#
from __future__ import division

import array
import unittest
import sys, math
from pxr import Gf, Vt
Expand Down Expand Up @@ -157,10 +159,10 @@ def test_Overflows(self):
for arrayType, (x0, x1) in overflows:
for x in (x0, x1):
with self.assertRaises(OverflowError):
array = arrayType((x,))
arrayT = arrayType((x,))
for x in (x0+1, x1-1):
array = arrayType((x,))
self.assertEqual(array, eval(repr(array)))
arrayT = arrayType((x,))
self.assertEqual(arrayT, eval(repr(arrayT)))

def test_ParallelizedOps(self):
m0 = Vt.Matrix4dArray((Gf.Matrix4d(1),Gf.Matrix4d(2)))
Expand Down Expand Up @@ -357,6 +359,14 @@ def _TestDivision(ArrayType, QuatType, ImgType):
_TestDivision(Vt.QuatdArray, Gf.Quatd, Gf.Vec3d)
_TestDivision(Vt.QuaternionArray, Gf.Quaternion, Gf.Vec3d)

def test_LargeBuffer(self):
'''VtArray can be created from a buffer with item count
greater than maxint'''
largePyBuffer = array.array('B', (0,)) * 2500000000
vtArrayFromBuffer = Vt.UCharArray.FromBuffer(largePyBuffer)
self.assertEqual(len(vtArrayFromBuffer), 2500000000)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1520d4a

Please sign in to comment.