2012-10-25 2 views
9

누구든지이 장고 미들웨어에 대한 경고의 실제 이유를 말할 수 있습니까? 어떻게 해결할 수 있습니까?DeprecationWarning : BaseException.message는 Python 2.6 예외로 더 이상 사용되지 않습니다 .__ class__, exception.message,

나는이 메시지가 무엇입니까 :

class GeneralMiddleware(object): 
    def process_exception(self, request, exception): 
     if exception.__class__ is SandboxError: 
      # someone is trying to access a sandbox that he has no 
      # permission to 
      return HttpResponseRedirect("/notpermitted/") 

     exc_type, value, tb = sys.exc_info() 
     data = traceback.format_tb(
        tb, None) + traceback.format_exception_only(
        exc_type, value) 
     msg = (
      "Failure when calling method:\n" 
      u"URL:'%s'\nMethod:'%s'\nException Type:'%s'\n" 
      u"Error Message '%s'\nFull Message:\n%s" 
      % (request.get_full_path(), request.method, 
       exception.__class__, exception.message, 
+0

'예외 .__ class__는 SandboxError' 대신'isinstance (exception, SandboxError)'를 시도하십시오. – Blender

+0

믹서기에는 작동하지 않습니다 ... 빠른 응답을 주셔서 감사합니다. 실제로 예외에 대한 경고가 나타납니다 .__ class__, exception.message this line. – PythonDev

답변

24

내가 제대로 파이썬 2.5의 새로운 인상 구문으로 전환 할 때, 그들이 찬성 message 멤버를 제거있어 기억한다면 (?) args 튜플 이전 버전과의 호환성을 위해 BaseException.message은 실질적으로 BaseException.args[0] if BaseException.args else None과 동일하지만 새 코드에서 사용하면 안됩니다. (더 인수, ()에 대해 보호 애호가 버전이 없을 수도 걱정하는 경우, 또는)

그래서, 당신이 원하는에 따라, 하나 args-message (당신이 원하는 경우 모든 인수) 또는 args[0]을 변경합니다.

이 변경의 이유는 새로운 스타일 예외의 경우 raise 또는 except에 더 이상 마법이 없다는 것입니다. raise 문에 예외 클래스의 생성자를 호출하고 except 문에있는 변수에서 예외를 catch하는 것입니다. 따라서 :

try: 
    raise MyException('Out of cheese error', 42) 
except Exception as x: 
    print x.args 

그러면 ('Out of cheese error', 42)이 인쇄됩니다. print x.message 만 있다면 'Out of cheese error'을 얻으실 수 있습니다. 따라서 별도의 멤버 등으로 오류 코드를 전달하기 위해 멋진 일을해야했던 Exception 하위 클래스를 단순화 할 수 있습니다. 사실, 모든 것은 다음과 같습니다 :

class BaseException(object): 
    def __init__(self, *args): 
    self.args = args 
0

Exception에서 상속하여 SandboxError 클래스 "DeprecationWarning. BaseException.message이 파이썬 2.6 예외 클래스, exception.message, 추천되지되었습니다" 수업? 그렇지 않으면이 메시지가 나타납니다. 추론은 PEP352에 설명되어 있습니다.

코드에서이 예외 사항은 다음과 같이 정의한다 :

class SandboxException(Exception): 
    .... 
+0

예 예외로부터 SandboxError 클래스를 상속 받았습니다. SandboxError 클래스 (예외) : 통과 – PythonDev