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