|
17 | 17 | (def inspect (as-jvm (import-module "inspect") {})) |
18 | 18 | (def argspec (get-attr inspect "getfullargspec")) |
19 | 19 | (def py-source (get-attr inspect "getsource")) |
| 20 | +(def py-sourcelines (get-attr inspect "getsourcelines")) |
| 21 | +(def py-file (get-attr inspect "getfile")) |
20 | 22 | (def types (import-module "types")) |
21 | 23 | (def fn-type |
22 | 24 | (call-attr builtins "tuple" |
|
42 | 44 | (def importlib (py/import-module "importlib")) |
43 | 45 | (def importlib_util (import-module "importlib.util")) |
44 | 46 | (def reload-module (py/get-attr importlib "reload")) |
| 47 | + |
45 | 48 | (defn findspec [x] |
46 | 49 | (let [-findspec |
47 | 50 | (-> importlib_util (get-attr "find_spec"))] |
48 | 51 | (-findspec x))) |
49 | 52 |
|
| 53 | + |
| 54 | +(defn find-lineno [x] |
| 55 | + (try |
| 56 | + (-> x py-sourcelines last) |
| 57 | + (catch Exception _ |
| 58 | + nil))) |
| 59 | + |
| 60 | +(defn find-file [x] |
| 61 | + (try |
| 62 | + (py-file x) |
| 63 | + (catch Exception _ |
| 64 | + nil))) |
| 65 | + |
50 | 66 | (defn py-fn-argspec [f] |
51 | 67 | (if-let [spec (try (when-not (pyclass? f) |
52 | 68 | (argspec f)) |
|
155 | 171 | (recur argspec' defaults' arglists)))))) |
156 | 172 |
|
157 | 173 |
|
158 | | -(defn py-class-argspec [class] |
159 | | - (let [constructor (py/get-attr class "__init__")] |
160 | | - (py-fn-argspec constructor))) |
161 | | - |
162 | 174 |
|
163 | 175 | (defn py-fn-metadata [fn-name x {:keys [no-arglists?]}] |
164 | 176 | (let [fn-argspec (pyargspec x) |
|
190 | 202 |
|
191 | 203 | (defn base-pyobj-map |
192 | 204 | [item] |
193 | | - (merge {:type (py/python-type item) |
194 | | - :doc (doc item) |
195 | | - :str (.toString item) |
196 | | - :flags (pyobj-flags item)} |
197 | | - (when (has-attr? item "__module__") |
198 | | - {:module (get-attr item "__module__")}) |
199 | | - (when (has-attr? item "__name__") |
200 | | - {:name (get-attr item "__name__")}))) |
| 205 | + (cond-> {:type (py/python-type item) |
| 206 | + :doc (doc item) |
| 207 | + :str (.toString item) |
| 208 | + :flags (pyobj-flags item) |
| 209 | + :line (find-lineno item) |
| 210 | + :file (find-file item)} |
| 211 | + (has-attr? item "__module__") |
| 212 | + (assoc :module (get-attr item "__module__")) |
| 213 | + (has-attr? item "__name__") |
| 214 | + (assoc :name (get-attr item "__name__")) |
| 215 | + (and (find-lineno item) (find-file item)) |
| 216 | + (assoc :line (find-lineno item) :file (find-file item)))) |
201 | 217 |
|
202 | 218 |
|
203 | 219 | (defn scalar? |
|
280 | 296 |
|
281 | 297 | (defn metadata-map->py-obj |
282 | 298 | [metadata-map] |
283 | | - (case (:type metadata-map) |
284 | | - :module (import-module (:name metadata-map)) |
285 | | - :type (-> (import-module (:module metadata-map)) |
286 | | - (get-attr (:name metadata-map))))) |
| 299 | + (try |
| 300 | + (case (:type metadata-map) |
| 301 | + :module (import-module (:name metadata-map)) |
| 302 | + :type (-> (import-module (:module metadata-map)) |
| 303 | + (get-attr (:name metadata-map)))) |
| 304 | + (catch Exception _ |
| 305 | + ;; metatypes -- e.g. socket.SocketIO |
| 306 | + (-> (import-module (:module metadata-map)) |
| 307 | + (get-attr (:name metadata-map)))))) |
287 | 308 |
|
288 | 309 |
|
289 | 310 | (defn get-or-create-namespace! |
|
0 commit comments