pyblish / pyblish-base

Pyblish base library - see https://github.com/pyblish/pyblish for details.
Other
127 stars 59 forks source link

Support loading unicode contained plugin in Python 3 #367

Closed davidlatwe closed 4 years ago

davidlatwe commented 4 years ago

What's changed

Some of our plugins contain Unicode characters in plugin's label attribute or doc string. Those plugins which registered in plugin path can be discovered via Pyblish in Python 2 but fails in Python 3 due to bytes encoding issue.

The solution was simple, just change the open mode into "rb" in api.discover.

Example

  1. Save a plugin with Unicode character
# /path/to/plugin/unicode_plugin.py

import pyblish.api

class UnicodePlugin(pyblish.api.InstancePlugin):

    order = pyblish.api.CollectorOrder
    label = "🤨"  # Unicode emoji

    def process(self, instance):
        return True
  1. In Python 3, register the dir path and run pyblish.api.discover to see the test result
import pyblish.api

# Remove other plugins for testing
pyblish.api.deregister_all_plugins()
pyblish.api.deregister_all_paths()

# Register our only Unicode contained plugin
pyblish.api.register_plugin_path("/path/to/plugin")

# AssertionError will raised without this PR
assert len(pyblish.api.discover()) == 1
BigRoy commented 4 years ago

@davidlatwe could it be worth it potentially adding a test for this too or would it be overly verbose? Other than that, nice and clean solution!

davidlatwe commented 4 years ago

Yeah, I was thinking about adding a test case for this, too ! But maybe tomorrow, I am just about to go out now. ☺️

mottosso commented 4 years ago

Nice one. :) Yes, test would be good, just an additional file with lots of unicode should suffice. And if all existing tests pass already then that's already a giant confirmation it works.

davidlatwe commented 4 years ago

Test case added !

mottosso commented 4 years ago

Looks good, nice work. :)