Skip to content

Commit e2208db

Browse files
committed
Add another ReadFromString method prototype for memory increase protection
1 parent 4ee2984 commit e2208db

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/SWIG_files/wrapper/BRepTools.i

+15-6
Original file line numberDiff line numberDiff line change
@@ -984,15 +984,24 @@ Parameter theProgress the range of progress indicator to fill in.
984984
BRepTools::Write(shape, s);
985985
return s.str();}
986986
};
987-
%feature("autodoc", "Deserializes TopoDS_Shape from string") ReadFromString;
987+
%feature("autodoc", "Deserializes TopoDS_Shape from string. Create and return a new TopoDS_Shape each time the method is called.") ReadFromString;
988988
%extend{
989989
static TopoDS_Shape ReadFromString(const std::string & src) {
990-
std::stringstream s(src);
991-
TopoDS_Shape shape;
992-
BRep_Builder b;
993-
BRepTools::Read(shape, s, b);
994-
return shape;}
990+
std::istringstream s(std::move(src));
991+
TopoDS_Shape shape;
992+
BRep_Builder b;
993+
BRepTools::Read(shape, s, b);
994+
return shape;
995+
}
995996
};
997+
%feature("autodoc", "Deserializes TopoDS_Shape from string. Take a TopoDS_Shape instance by reference to prevent memory increase.") ReadFromString;
998+
%extend{
999+
static void ReadFromString(const std::string & src, TopoDS_Shape& shape) {
1000+
std::istringstream s(std::move(src));
1001+
BRep_Builder b;
1002+
BRepTools::Read(shape, s, b);
1003+
}
1004+
};
9961005
};
9971006

9981007

test/test_core_wrapper_features.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
BRepBuilderAPI_MakeEdge,
4949
BRepBuilderAPI_Sewing,
5050
)
51-
from OCC.Core.BRepTools import BRepTools_ShapeSet
51+
from OCC.Core.BRepTools import BRepTools_ShapeSet, breptools
5252
from OCC.Core.gp import (
5353
gp_Pnt,
5454
gp_Vec,
@@ -1218,3 +1218,18 @@ def test_breptools_shape_set_extensions():
12181218
brep_string_content = f.read()
12191219
new_shape_set = BRepTools_ShapeSet()
12201220
new_shape_set.ReadFromString(brep_string_content)
1221+
1222+
1223+
def test_topods_readfromstring_extension():
1224+
box = BRepPrimAPI_MakeBox(100, 100, 100).Shape()
1225+
string = breptools.WriteToString(box)
1226+
1227+
# create a new shape each time
1228+
for i in range(10):
1229+
box1 = breptools.ReadFromString(string)
1230+
1231+
# same, but fill in the shape
1232+
topods_shape = TopoDS_Shape()
1233+
# create a new shape each time
1234+
for i in range(10):
1235+
breptools.ReadFromString(string, topods_shape)

0 commit comments

Comments
 (0)