Skip to content

Commit

Permalink
[int] better document and rewrite submodule import decisions
Browse files Browse the repository at this point in the history
  • Loading branch information
digikar99 committed May 30, 2024
1 parent 18349a1 commit 76c9a1a
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/import-export.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,19 @@ Arguments:
(iter (for (submodule has-submodules) in submodules)
(for submodule-fullname = (concatenate 'string
pymodule-name "." submodule))
(when (and (char/= #\_ (aref submodule 0)) ; avoid private modules / packages
;; pkgutil is of type module
;; import matplotlib does not import matplotlib.pyplot
;; https://stackoverflow.com/questions/14812342/matplotlib-has-no-attribute-pyplot
;; We maintain these semantics.
;; The below form errors in the case of submodules and
;; therefore returns NIL.
(ignore-errors (pyobject-wrapper-eq
(pycall "type" (pyvalue "pkgutil"))
(pycall "type" (pyvalue submodule-fullname)))))
;; The AND clauses in WHEN below correspond to:
;; 1. Avoid private modules / packages
;; 2. The above processing using pkgutil.iter_modules above returns *all*
;; the possible submodules of PYMODULE-NAME. However, consider this behavior -
;; Some of these submodules may not actually be imported while
;; importing PYMODULE-NAME. For example, the above processing for
;; "matplotlib" outputs "pyplot" as a submodule of matplotlib.
;; However, "import matplotlib" does not import "pyplot".
;; See https://stackoverflow.com/questions/14812342/matplotlib-has-no-attribute-pyplot
;; We want to preserve this behavior. That is why, we first check if PYMODULE-NAME
;; actually has SUBMODULE as an attribute. If not, we do not process this any further.
(when (and (char/= #\_ (aref submodule 0))
(pycall "hasattr" (pyvalue pymodule-name) submodule))
(let ((*is-submodule* t))
(collect
(macroexpand-1
Expand Down

0 comments on commit 76c9a1a

Please sign in to comment.