NVIDIA / NVTX

The NVIDIA® Tools Extension SDK (NVTX) is a C-based Application Programming Interface (API) for annotating events, code ranges, and resources in your applications.
Apache License 2.0
310 stars 48 forks source link

Python: fix nullcontext usage when disabled #79

Closed merlinND closed 1 year ago

merlinND commented 1 year ago

Hello,

I have encountered a problem with NVTX Python when NVTX_DISABLE is set. Here is how to reproduce it:

python3.11 -m venv .venv
source .venv/bin/activate
pip install --upgrade nvtx
# Successfully installed nvtx-0.2.5

With the following short test file:

import nvtx

@nvtx.annotate('test1')
def test1():
    print('test1')

test1()

with nvtx.annotate('test2'):
    print('test2')

The result is:

$ NVTX_DISABLE=1 python ./test_nvtx.py
test1
Traceback (most recent call last):
  File "/home/merlin/Desktop/nvtx-test/./test_nvtx.py", line 10, in <module>
    with nvtx.annotate('test2'):
  File "/usr/lib/python3.11/contextlib.py", line 755, in __enter__
    return self.enter_result
           ^^^^^^^^^^^^^^^^^
AttributeError: 'annotate' object has no attribute 'enter_result'

With the proposed fix, the test script runs without issues.

shwina commented 1 year ago

This looks good to me! I'll merge, test locally and publish a new release on PyPI with the fix included

shwina commented 1 year ago

This should be available in version 0.2.8 which has been uploaded to PyPI.