# \`vtkCommunicator.MarshalDataObject\` mutates locale unexpectedly

**URL:** https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260
**Category:** Development
**Created:** [January 27, 2026, 4:50pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260 "2026-01-27T16:50:18Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![user27182](https://discourse.vtk.org/user_avatar/discourse.vtk.org/user27182/32/10104_2.png) [@user27182](https://discourse.vtk.org/u/user27182)
#### Post date: [January 27, 2026, 4:50pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/1 "2026-01-27T16:50:18Z")

</div>

This seems like a bug, since I would expect `locale` not to be mutated when calling vtk code like this:

```auto
import locale
import vtk

l = locale.getlocale()
e = locale.getencoding()
print(f"Locale before: {l}, Encoding: {e}")

vtk.vtkCommunicator.MarshalDataObject(vtk.vtkPolyData(), vtk.vtkCharArray())

l = locale.getlocale()
e = locale.getencoding()
print(f"Locale after: {l}, Encoding: {e}")

```

```auto
Locale before: ('en_US', 'UTF-8'), Encoding: UTF-8
Locale after: (None, None), Encoding: US-ASCII

```

This mutation has implications for python users, since `vtk.vtkCommunicator.MarshalDataObject` is called when pickling vtk objects:

> <https://github.com/Kitware/VTK/blob/b7dc8e7d978f8812bcd97f0f1bbf87b467649d16/Wrapping/Python/vtkmodules/util/pickle_support.py#L70-L71>

This is even more confusing when users aren’t even pickling objects themselves, e.g. pickling is used by `copy.deepcopy`, resulting in locale being unexpectedly mutated with this code:

```py
import copy
import vtk

poly = vtk.vtkPolyData()
poly_copy = copy.deepcopy(poly)

```

Is this normal/expected? Should this be tracked as an issue in GitLab?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [January 27, 2026, 6:38pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/2 "2026-01-27T18:38:39Z")

</div>

@jaswantp

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [January 27, 2026, 6:44pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/3 "2026-01-27T18:44:10Z")

</div>

@ben.boeckel i do not know about the `vtkCommunicator.MarshalDataObject` method. The marshalling that we worked on is different.

---

<div class="post-metadata">

### Author: ![user27182](https://discourse.vtk.org/user_avatar/discourse.vtk.org/user27182/32/10104_2.png) [@user27182](https://discourse.vtk.org/u/user27182)
#### Post date: [January 27, 2026, 7:09pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/4 "2026-01-27T19:09:04Z")

</div>

The marshalling is using a generic data object writer to serialize the vtk object:

> <https://github.com/Kitware/VTK/blob/b7dc8e7d978f8812bcd97f0f1bbf87b467649d16/Parallel/Core/vtkCommunicator.cxx#L501-L502>

Using this info, we can reproduce the bug by simply saving the mesh to file, e.g. with `vtkPolyDataWriter`:

```auto
import locale
import vtk

l = locale.getlocale()
e = locale.getencoding()
print(f"Locale before: {l}, Encoding: {e}")

poly = vtk.vtkPolyData()
writer = vtk.vtkPolyDataWriter()
writer.SetFileName("poly.vtk")
writer.SetInputData(poly)
writer.Write()

l = locale.getlocale()
e = locale.getencoding()
print(f"Locale after: {l}, Encoding: {e}")

```

So it’s not specific to pickling or marshalling, but is related to the IO or writer modules.

---

<div class="post-metadata">

### Author: ![user27182](https://discourse.vtk.org/user_avatar/discourse.vtk.org/user27182/32/10104_2.png) [@user27182](https://discourse.vtk.org/u/user27182)
#### Post date: [January 27, 2026, 7:25pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/5 "2026-01-27T19:25:13Z")

</div>

and if we comment out `writer.Write()`, then the locale is _not_ mutated. So this mutation is caused by the `Write` method somehow.

Perhaps this is the culprit?

> <https://github.com/Kitware/VTK/blob/b7dc8e7d978f8812bcd97f0f1bbf87b467649d16/IO/Legacy/vtkDataWriter.cxx#L124-L126>

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [January 28, 2026, 1:39am UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/6 "2026-01-28T01:39:16Z")

</div>

Yes, that looks to be the culprit. We should convert to using stream object locale APIs instead of messing with the global state.

---

<div class="post-metadata">

### Author: ![user27182](https://discourse.vtk.org/user_avatar/discourse.vtk.org/user27182/32/10104_2.png) [@user27182](https://discourse.vtk.org/u/user27182)
#### Post date: [January 28, 2026, 3:22pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/7 "2026-01-28T15:22:26Z")

</div>

> We should convert to using stream object locale APIs instead of messing with the global state.

Sounds like this should be tracked in GitLab then as a bug-fix / maintenance item?

Here’s the original MR: [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/5080](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/5080)  
The locale is _supposed_ to be restored here:

> <https://github.com/Kitware/VTK/blob/b7dc8e7d978f8812bcd97f0f1bbf87b467649d16/IO/Legacy/vtkDataWriter.cxx#L2302-L2303>

But it appears no tests were added with the MR to ensure this actually works.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [January 28, 2026, 6:17pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/8 "2026-01-28T18:17:13Z")

</div>

Even with restoration, any multi-threading will see the locale switch on it suddenly. Yes, please open an issue for it.

---

<div class="post-metadata">

### Author: ![user27182](https://discourse.vtk.org/user_avatar/discourse.vtk.org/user27182/32/10104_2.png) [@user27182](https://discourse.vtk.org/u/user27182)
#### Post date: [January 28, 2026, 6:26pm UTC](https://discourse.vtk.org/t/vtkcommunicator-marshaldataobject-mutates-locale-unexpectedly/16260/9 "2026-01-28T18:26:18Z")

</div>

[https://gitlab.kitware.com/vtk/vtk/-/issues/19928](https://gitlab.kitware.com/vtk/vtk/-/issues/19928)
