You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5KB

  1. import py, sys
  2. class DeprecationWarning(DeprecationWarning):
  3. def __init__(self, msg, path, lineno):
  4. self.msg = msg
  5. self.path = path
  6. self.lineno = lineno
  7. def __repr__(self):
  8. return "%s:%d: %s" %(self.path, self.lineno+1, self.msg)
  9. def __str__(self):
  10. return self.msg
  11. def _apiwarn(startversion, msg, stacklevel=2, function=None):
  12. # below is mostly COPIED from python2.4/warnings.py's def warn()
  13. # Get context information
  14. if isinstance(stacklevel, str):
  15. frame = sys._getframe(1)
  16. level = 1
  17. found = frame.f_code.co_filename.find(stacklevel) != -1
  18. while frame:
  19. co = frame.f_code
  20. if co.co_filename.find(stacklevel) == -1:
  21. if found:
  22. stacklevel = level
  23. break
  24. else:
  25. found = True
  26. level += 1
  27. frame = frame.f_back
  28. else:
  29. stacklevel = 1
  30. msg = "%s (since version %s)" %(msg, startversion)
  31. warn(msg, stacklevel=stacklevel+1, function=function)
  32. def warn(msg, stacklevel=1, function=None):
  33. if function is not None:
  34. import inspect
  35. filename = inspect.getfile(function)
  36. lineno = py.code.getrawcode(function).co_firstlineno
  37. else:
  38. try:
  39. caller = sys._getframe(stacklevel)
  40. except ValueError:
  41. globals = sys.__dict__
  42. lineno = 1
  43. else:
  44. globals = caller.f_globals
  45. lineno = caller.f_lineno
  46. if '__name__' in globals:
  47. module = globals['__name__']
  48. else:
  49. module = "<string>"
  50. filename = globals.get('__file__')
  51. if filename:
  52. fnl = filename.lower()
  53. if fnl.endswith(".pyc") or fnl.endswith(".pyo"):
  54. filename = filename[:-1]
  55. elif fnl.endswith("$py.class"):
  56. filename = filename.replace('$py.class', '.py')
  57. else:
  58. if module == "__main__":
  59. try:
  60. filename = sys.argv[0]
  61. except AttributeError:
  62. # embedded interpreters don't have sys.argv, see bug #839151
  63. filename = '__main__'
  64. if not filename:
  65. filename = module
  66. path = py.path.local(filename)
  67. warning = DeprecationWarning(msg, path, lineno)
  68. import warnings
  69. warnings.warn_explicit(warning, category=Warning,
  70. filename=str(warning.path),
  71. lineno=warning.lineno,
  72. registry=warnings.__dict__.setdefault(
  73. "__warningsregistry__", {})
  74. )