347 def raisewithtb(exc: BaseException, tb) -> NoReturn: |
347 def raisewithtb(exc: BaseException, tb) -> NoReturn: |
348 """Raise exception with the given traceback""" |
348 """Raise exception with the given traceback""" |
349 raise exc.with_traceback(tb) |
349 raise exc.with_traceback(tb) |
350 |
350 |
351 |
351 |
|
352 # Copied over from the 3.13 Python stdlib `inspect.cleandoc`, with a couple |
|
353 # of removals explained inline. |
|
354 # It differs slightly from the 3.8+ version, so it's better to use the same |
|
355 # version to remove any potential for variation. |
|
356 def cleandoc(doc): |
|
357 """Clean up indentation from docstrings. |
|
358 |
|
359 Any whitespace that can be uniformly removed from the second line |
|
360 onwards is removed.""" |
|
361 lines = doc.expandtabs().split('\n') |
|
362 |
|
363 # Find minimum indentation of any non-blank lines after first line. |
|
364 margin = sys.maxsize |
|
365 for line in lines[1:]: |
|
366 content = len(line.lstrip(' ')) |
|
367 if content: |
|
368 indent = len(line) - content |
|
369 margin = min(margin, indent) |
|
370 # Remove indentation. |
|
371 if lines: |
|
372 lines[0] = lines[0].lstrip(' ') |
|
373 if margin < sys.maxsize: |
|
374 for i in range(1, len(lines)): |
|
375 lines[i] = lines[i][margin:] |
|
376 # Here the upstream *Python* version does newline trimming, but it looks |
|
377 # like the compiler (written in C) does not, so go with what the compiler |
|
378 # does. |
|
379 return '\n'.join(lines) |
|
380 |
|
381 |
352 def getdoc(obj: object) -> Optional[bytes]: |
382 def getdoc(obj: object) -> Optional[bytes]: |
353 """Get docstring as bytes; may be None so gettext() won't confuse it |
383 """Get docstring as bytes; may be None so gettext() won't confuse it |
354 with _('')""" |
384 with _('')""" |
355 doc = builtins.getattr(obj, '__doc__', None) |
385 doc = builtins.getattr(obj, '__doc__', None) |
356 if doc is None: |
386 if doc is None: |
357 return doc |
387 return doc |
|
388 if sys.version_info < (3, 13): |
|
389 # Python 3.13+ "cleans up" the docstring at compile time, let's |
|
390 # normalize this behavior for previous versions |
|
391 doc = cleandoc(doc) |
358 return sysbytes(doc) |
392 return sysbytes(doc) |
359 |
393 |
360 |
394 |
361 # these wrappers are automagically imported by hgloader |
395 # these wrappers are automagically imported by hgloader |
362 delattr = builtins.delattr |
396 delattr = builtins.delattr |