Fix #112788: python modules should have ModuleType attributes #112813

Open
Leonardo Covarrubias wants to merge 1 commits from leocov/blender:112788-add-bpy-module-attrs into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
First-time contributor

This addresses: #112788

Problem:
Some bpy python modules are not actually modules defined with PyModuleDef and so they lack many of the attributes described by the ModuleType interface.
This is problematic when code tries to inspect them using importlib.import_module(...) or custom MetaPathFinder and Loader and for example read the __name__ attribute (it does not exist).
Conceptually: if something can be imported like: import bpy.app.translations then it should behave like a module in some basic ways.

The problematic sudo-modules are:

  • bpy.app
  • bpy.app.handlers
  • bpy.app.translations

Solution:
Its straightforward to add these attributes to the objects standing in as modules so that they are available for use.
These additions should not impact any internal or external code.
I've chosen to add valid values for some but not all of the ModuleType attributes so that the end result is equivalent to other bpy modules generated with PyModuleDef

Added:

  • __name__ with the correct string value
  • __spec__ with None value
  • __package__ with None value
  • __loader__ with None value
This addresses: https://projects.blender.org/blender/blender/issues/112788 **Problem:** Some `bpy` python modules are not actually modules defined with `PyModuleDef` and so they lack many of the attributes described by the [ModuleType](https://docs.python.org/3/library/types.html#types.ModuleType) interface. This is problematic when code tries to inspect them using `importlib.import_module(...)` or custom `MetaPathFinder` and `Loader` and for example read the `__name__` attribute (it does not exist). Conceptually: if something can be imported like: `import bpy.app.translations` then it should behave like a module in some basic ways. The problematic sudo-modules are: - bpy.app - bpy.app.handlers - bpy.app.translations **Solution:** Its straightforward to add these attributes to the objects standing in as modules so that they are available for use. These additions should not impact any internal or external code. I've chosen to add valid values for some but not all of the `ModuleType` attributes so that the end result is equivalent to other `bpy` modules generated with `PyModuleDef` Added: - `__name__` with the correct string value - `__spec__` with None value - `__package__` with None value - `__loader__` with None value
Leonardo Covarrubias changed title from WIP #112788: python modules should have ModuleType attributes to Fix #112788: python modules should have ModuleType attributes 2023-09-24 20:22:59 +02:00
Iliya Katushenock added this to the Python API project 2023-09-24 20:49:48 +02:00
Leonardo Covarrubias force-pushed 112788-add-bpy-module-attrs from a5c669963a to 7526655507 2023-09-25 22:26:58 +02:00 Compare
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 112788-add-bpy-module-attrs:leocov-112788-add-bpy-module-attrs
git checkout leocov-112788-add-bpy-module-attrs
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#112813
No description provided.