[BUG] 'PythonObject' cannot be used in contextmanagers (with blocks)
Open
Nobody has claimed this yet.
enhancement
help wanted
mojo
mojo-repo
- Dominant language
- Mojo
- Stars
- 29.8k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Bug description
PythonObject that implements __enter__() cannot be used in Mojo context managers.
Steps to reproduce
See the following code:
from python import Python
with Python.import_module('builtins').open('file.txt') as file:
# do something
Error raised:
'PythonObject' does not implement the '__enter__' method
System information
OS:
❯ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.5 LTS"
Mojo:
❯ mojo -v
mojo 2024.10.605 (10ee785d)
Modular:
❯ modular -v
modular 0.9.3 (020e342b)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Mojo reproduction using Python.import_module('builtins').open and confirm the 'enter' error on the reported version. Trace PythonObject's context-manager interop and verify that a Python object implementing enter() can be used in a Mojo with block without the error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100