Skip to content

Commit

Permalink
[Fix] Fix when package overwrites __path__ to a string. (#43)
Browse files Browse the repository at this point in the history
Summary: Fix when package overwrites `__path__` to a string.

Test Plan: all

Reviewed-by: -

Issue: -
  • Loading branch information
oraluben authored Sep 19, 2023
1 parent 219b1a8 commit 9845236
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
run: |
nox -s test_import_third_party-${{ matrix.conda-py }}
- name: Setup tmate session
if: ${{ failure() }}
if: ${{ failure() && false }}
uses: mxschmitt/action-tmate@v3

test-twine:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='code-data-share',
version='0.1.0',
version='0.0.3',
packages=['cds'],
package_dir={'': 'src'},
python_requires='>=3.8.0',
Expand Down
19 changes: 17 additions & 2 deletions src/cds/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ def wrap_exec_module(self, module):
# __path__ must be an iterable of strings,
# we convert any valid __path__ to tuple of strings.
# Ref: https://docs.python.org/3/reference/import.html#module-path
path = tuple(path) if path else None
if not path:
path = None
elif isinstance(path, str):
# Some package might change this, which is non-standard.
# There is no correct way to handle this.
# We retain the file location to maintain the original import function,
# and also record the `path` for debugging purposes.
import os.path
path = (os.path.dirname(file), path)
else:
try:
path = tuple(path)
except TypeError:
_verbose(f"{name}.__path__ is not iterable: {path}", 1)
path = None
meta_map[name] = (package, file, path)
return module

Expand All @@ -68,8 +82,9 @@ def wrap_exec_module(self, module):
_verbose(f'executing module {line} triggers an SystemExit({e.code}), ignoring.', 1)
except Exception as e:
_verbose(f'executing module {line} triggers an {e.__class__.__name__}, traceback:', 1)
import sys
import traceback
_verbose(''.join(traceback.format_exception(e)), 1)
_verbose(''.join(traceback.format_exception(*sys.exc_info())), 1)

shared_class_data = tuple([(k, v) for k, v in {
k: (*meta_map[k], code_map[k])
Expand Down

0 comments on commit 9845236

Please sign in to comment.