|
1 | 1 | import os |
2 | 2 | import inspect |
3 | 3 | import sys |
| 4 | + |
4 | 5 | import pytensor |
5 | 6 | from pathlib import Path |
6 | 7 |
|
|
235 | 236 | # Resolve function |
236 | 237 | # This function is used to populate the (source) links in the API |
237 | 238 | def linkcode_resolve(domain, info): |
238 | | - def find_source(): |
| 239 | + def find_obj() -> object: |
239 | 240 | # try to find the file and line number, based on code from numpy: |
240 | 241 | # https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286 |
241 | 242 | obj = sys.modules[info["module"]] |
242 | 243 | for part in info["fullname"].split("."): |
243 | 244 | obj = getattr(obj, part) |
| 245 | + return obj |
244 | 246 |
|
| 247 | + def find_source(obj): |
245 | 248 | fn = Path(inspect.getsourcefile(obj)) |
246 | | - fn = fn.relative_to(Path(__file__).parent) |
| 249 | + fn = fn.relative_to(Path(pytensor.__file__).parent) |
247 | 250 | source, lineno = inspect.getsourcelines(obj) |
248 | 251 | return fn, lineno, lineno + len(source) - 1 |
249 | 252 |
|
| 253 | + def fallback_source(): |
| 254 | + return info["module"].replace(".", "/") + ".py" |
| 255 | + |
250 | 256 | if domain != "py" or not info["module"]: |
251 | 257 | return None |
| 258 | + |
252 | 259 | try: |
253 | | - filename = "pytensor/%s#L%d-L%d" % find_source() |
| 260 | + obj = find_obj() |
254 | 261 | except Exception: |
255 | | - filename = info["module"].replace(".", "/") + ".py" |
| 262 | + filename = fallback_source() |
| 263 | + else: |
| 264 | + try: |
| 265 | + filename = "pytensor/%s#L%d-L%d" % find_source(obj) |
| 266 | + except Exception: |
| 267 | + # warnings.warn(f"Could not find source code for {domain}:{info}") |
| 268 | + try: |
| 269 | + filename = obj.__module__.replace(".", "/") + ".py" |
| 270 | + except AttributeError: |
| 271 | + # Some objects do not have a __module__ attribute (?) |
| 272 | + filename = fallback_source() |
| 273 | + |
256 | 274 | import subprocess |
257 | 275 |
|
258 | 276 | tag = subprocess.Popen( |
|
0 commit comments