# Fun with PythonObjects - Mojo compiler error

**URL:** <https://forum.modular.com/t/fun-with-pythonobjects-mojo-compiler-error/1082>\
**Category:** Mojo\
**Tags:** mojo-compiler, python-interop\
**Created:** [March 19, 2025, 12:18am UTC](https://forum.modular.com/t/fun-with-pythonobjects-mojo-compiler-error/1082 "2025-03-19T00:18:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EzRyder](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ezryder/32/133_2.png) [@EzRyder](https://forum.modular.com/u/EzRyder)\
**Post date:** [March 19, 2025, 12:18am UTC](https://forum.modular.com/t/fun-with-pythonobjects-mojo-compiler-error/1082/1 "2025-03-19T00:18:09Z")

</div>

This issue was quite a challenge to figure out from within my project’s code, but with persistence I reduced the compiler error down to a minimal repro. Follow the link below for the details in the issue opened today on Github. I do not understand the error message other than ‘something bad happened’. 😀  
Some kind of issue with memory allocation or a memory leak related to PythonObjects?

**Error:** `field 'self.test_agent.name._buffer._len' destroyed out of the middle of a value, preventing the overall value from being destroyed`

> <https://github.com/modular/max/issues/4163>
>
> \### Bug description
> 
> \### Actual behavior
> Referring to the code below, \`struct Ap…pAgents:\` emits the following compiler error at the assignment of \`self.test\_agent\`: 
> \`field 'self.test\_agent.name.\_buffer.\_len' destroyed out of the middle of a value, preventing the overall value from being destroyed\`
> 
> In \`struct AgentBase\`, if either instance variable of type PythonObject are removed completely or changed to a local variable scoped within \`\_\_init\_\_\`, the error goes away. Likewise, removing the instance variable \`name\` from \`struct TestAgent\` also prevents the error.
> 
> \### Expected behavior
> The code compiles and executes as written with no error.
> 
> \### Steps to reproduce
> 
> \*\*Minimum code to reproduce the error.\*\*
> \`\`\`
> from python import Python, PythonObject
> 
> trait TAgent(CollectionElement):
> ...
> 
> 
> trait TAgent(CollectionElement):
> ...
> 
> 
> @value
> struct AgentBase\[T: TAgent\](CollectionElement):
> var agent: T
> 
> var name: String
> var py\_datetime: PythonObject
> var py\_time: PythonObject
> 
> fn \_\_init\_\_(out self, name: String, agent: T) raises:
> self.agent = agent
> 
> self.name = name
> self.py\_datetime = Python.import\_module("datetime")
> self.py\_time = Python.import\_module("time")
> 
> 
> @value
> struct TestAgent(TAgent):
> var name: String
> 
> fn \_\_init\_\_(out self, name: String = "Bond, James Bond") raises:
> self.name = name
> 
> 
> @value
> struct AppAgents:
> var test\_agent: AgentBase\[TestAgent\]
> 
> fn \_\_init\_\_(out self) raises:
> alias AGENT\_NAME = "007"
> 
> # Error on assignment statement.
> # field 'self.test\_agent.name.\_buffer.\_len' destroyed out of the middle of a value, preventing the overall value from being destroyed
> self.test\_agent = AgentBase(AGENT\_NAME, TestAgent())
> 
> 
> fn main() raises:
> app\_agents = AppAgents()
> \`\`\`
> 
> 
> \### System information
> 
> \`\`\`
> Package Version Build Size Kind Source
> max 25.2.0.dev2025031805 release 9.7 KiB conda max
> max-core 25.2.0.dev2025031805 release 251.6 MiB conda max-core
> max-python 25.2.0.dev2025031805 release 120.3 MiB conda max-python
> \`\`\`
> 
> \`\`\`
> Magic version: 0.7.2
> System
> \------------
> Pixi version: 0.41.4
> Platform: linux-64
> Virtual packages: \_\_unix=0=0
> : \_\_linux=6.11.0=0
> : \_\_glibc=2.40=0
> : \_\_cuda=12.6=0
> : \_\_archspec=1=skylake
> Cache dir: /home/johnsoe1/.cache/rattler/cache
> Auth storage: /home/johnsoe1/.rattler/credentials.json
> Config locations: No config files found
> 
> Global
> \------------
> Bin dir: /home/johnsoe1/.modular/bin
> Environment dir: /home/johnsoe1/.modular/envs
> Manifest dir: /home/johnsoe1/.modular/manifests/pixi-global.toml
> \`\`\`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [September 15, 2025, 12:18am UTC](https://forum.modular.com/t/fun-with-pythonobjects-mojo-compiler-error/1082/2 "2025-09-15T00:18:22Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
