25592fa6c2
ci / test (push) Successful in 6m14s
History-free snapshot of the private development repository at 77a82eb. Code, docs and the public catalogue only: no plant or job knowledge layers, no client, job or producer names (tools/scrub.py clean against the widened term list; a second word-level grep clean). The intro deck is left out because its slide images cannot be checked for job drawings.
32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""The small Python 2 / Python 3 bridge.
|
|
|
|
We do not choose the interpreter -- pyRevit does, from the engine configured
|
|
for the host. This extension was written on Revit 2025 with IronPython 2.7,
|
|
but the same folder may be dropped on a machine whose pyRevit is set to
|
|
IronPython 3 or CPython 3, where `unicode` and `basestring` are a NameError.
|
|
|
|
A NameError at module scope takes the whole button down with a traceback the
|
|
user cannot act on, so the two names are resolved once, here.
|
|
|
|
Everything else in this extension is already written in the intersection of
|
|
the two languages: no f-strings, no print statement, no implicit integer
|
|
division, no pathlib. Keep it that way and the engine stops mattering.
|
|
"""
|
|
|
|
try: # Python 2 (IronPython 2.7)
|
|
text_type = unicode # noqa: F821
|
|
string_types = (str, unicode) # noqa: F821
|
|
except NameError: # Python 3 (IronPython 3, CPython 3)
|
|
text_type = str
|
|
string_types = (str,)
|
|
|
|
|
|
def as_text(value, encoding='utf-8'):
|
|
"""Return value as the interpreter's unicode text type."""
|
|
if isinstance(value, text_type):
|
|
return value
|
|
if isinstance(value, bytes):
|
|
return value.decode(encoding)
|
|
return text_type(value)
|