modular / modular/modular

[BUG] 'PythonObject' cannot be used in contextmanagers (with blocks)

Open
#3,620 1 comment 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.