Skip to content

fix: Python compatibility, safety, and resource management#213

Open
maksimtech wants to merge 1 commit intocnr-isti-vclab:mainfrom
maksimtech:fix/python-compatibility-and-safety
Open

fix: Python compatibility, safety, and resource management#213
maksimtech wants to merge 1 commit intocnr-isti-vclab:mainfrom
maksimtech:fix/python-compatibility-and-safety

Conversation

@maksimtech
Copy link
Copy Markdown

Summary

  • NumPy ≥ 1.24 compatibility (critical) — replace all np.bool / np.int / np.float deprecated aliases with np.bool_ across 6 model files. These were removed in NumPy 1.24 and caused an AttributeError on import in any environment using a modern NumPy (pip default since early 2023).
  • Narrow 85+ bare except: clauses across 30+ files to specific exception types (TypeError, ValueError, KeyError, OSError, etc.), preventing accidental swallowing of SystemExit / KeyboardInterrupt and making real errors visible during development.
  • Fix resource leaks — all open() / tarfile.open() / pickle write patterns converted to with context managers in 11 files; removed a stale f.close() after a with block.
  • Replace subprocess.getstatusoutput(string) with subprocess.run(list, ...) to avoid implicit shell=True in install.py and install_conda_windows.py.
  • Replace stdout=open(os.devnull, 'wb') with subprocess.DEVNULL — one file descriptor was leaked per check_call invocation.
  • Guard os.chdir('coraline') in install.py with save/restore in a finally block so the CWD is always restored even when the cmake/make build fails.
  • Replace deprecated torch.nn.functional.upsample with interpolate (alias on import); also update the stale 2019 error message referencing CUDA 10.0 / PyTorch 1.0.0 / Python 3.6.8.
  • Add requirements.txt — canonical dependency list derived from install.py with pinned lower bounds and a numpy<2.0 upper cap.

Test plan

  • python -c "import numpy as np; np.bool_" — no AttributeError on NumPy 1.24+
  • Launch TagLab on Python 3.11 + NumPy 1.26 (current pip default) and confirm startup
  • Run install.py cpu on Linux and macOS — confirm clean install without CUDA
  • Open a project, trigger autosave, close — no file-handle errors on Windows
  • Trigger classifier run — confirm .dat score files are created and cleaned up
  • Verify signal connections in connectProject() work on repeated open/close

🤖 Generated with Claude Code

NumPy ≥ 1.24 compatibility (critical)
- Replace all np.bool / np.int / np.float deprecated aliases with
  np.bool_ across 6 model files; these were removed in NumPy 1.24
  and caused an AttributeError on import with any modern environment.

Bare except: narrowed to specific types (30+ files, 85+ clauses)
- Qt signal UniqueConnection guards: except: → except TypeError
- List.index() guards: except: → except ValueError
- Dict key access: except: → except KeyError
- Numeric conversions in widgets: except: → except (ValueError, ZeroDivisionError)
- OS / file system ops: except: → except OSError / IndexError
- Download failures: bare except re-raised with "from e" to preserve chain
- NewDataset.py os.makedirs try/except replaced with exist_ok=True

Resource leaks fixed (11 files)
- All open() / tarfile.open() / pickle open-write-close patterns
  converted to "with" context managers (batch_processing, Project,
  MapClassifier, training, dataloaders/helpers, pascal, sbd).
- Removed stale f.close() call after a "with" block in Project.py.

Subprocess shell injection and DEVNULL leaks (install.py, install_conda_windows.py)
- All subprocess.getstatusoutput('string') calls replaced with
  subprocess.run(['list'], capture_output=True, text=True) to avoid
  implicit shell=True.
- stdout=open(os.devnull, 'wb') replaced with subprocess.DEVNULL
  to stop leaking a file descriptor on every check_call invocation.

os.chdir CWD not restored on error (install.py)
- Coraline build wrapped in try/finally to restore the original CWD
  even when cmake or make fails, preventing broken relative paths for
  the remainder of the install session.

Deprecated PyTorch API (TagLab.py)
- torch.nn.functional.upsample replaced by aliasing to interpolate on
  import; existing call sites unchanged.
- Updated stale error message that still referenced CUDA 10.0 / PyTorch
  1.0.0 / Python 3.6.8 from 2019.

Add requirements.txt
- Canonical dependency list derived from install.py with pinned lower
  bounds and a numpy<2.0 upper cap.
- torch/CUDA installs remain handled by install.py (CUDA-version-aware).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants