Functions that raise exceptions.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex G

    Functions that raise exceptions.

    Does anyone know how I would go about conditionally raising an
    exception in a decorator (or any returned function for that matter)?
    For example:

    def decorator(arg):
    def raise_exception (fn):
    raise Exception
    return raise_exception

    class some_class(obje ct):
    @raise_exceptio n
    def some_method(sel f)
    print "An exception should be raised when I'm called, but not
    when I'm defined"

    The intent of the above code is that an exception should be raised if
    some_method is ever called. It seems, however, since the decorator
    function is executed on import, the raise statement is executed, and I
    the exception gets thrown whenever the module is imported, rather than
    when the method is called. Does anyone have a clue how I might go
    about doing this?

    Thank you in advance,

    Alex.
  • Alex G

    #2
    Re: Functions that raise exceptions.

    whoops! The code should be:

    def decorator(arg):
        def raise_exception (fn):
          raise Exception
      return raise_exception

    class some_class(obje ct):
        @decorator('mea ningless string')
        def some_method(sel f):
            print "An exception should be raised when I'm called, but not
    when I'm defined"


    Comment

    • Duncan Booth

      #3
      Re: Functions that raise exceptions.

      Alex G <alexander.girm an@gmail.comwro te:
      Does anyone know how I would go about conditionally raising an
      exception in a decorator (or any returned function for that matter)?
      For example:
      >
      def decorator(arg):
      def raise_exception (fn):
      raise Exception
      return raise_exception
      >
      class some_class(obje ct):
      @raise_exceptio n
      def some_method(sel f)
      print "An exception should be raised when I'm called, but not
      when I'm defined"
      >
      The intent of the above code is that an exception should be raised if
      some_method is ever called. It seems, however, since the decorator
      function is executed on import, the raise statement is executed, and I
      the exception gets thrown whenever the module is imported, rather than
      when the method is called. Does anyone have a clue how I might go
      about doing this?
      Well, the simplest way would be to correct the syntax error in your class
      definition and to try calling the decorator you defined instead of calling
      the undefined 'raise_exceptio n'. Fix both of those and the code you posted
      works 'as is'.
      >>def decorator(arg):
      def raise_exception (fn):
      raise Exception
      return raise_exception
      >>class some_class(obje ct):
      @decorator
      def some_method(sel f):
      print "An exception should be raised when I'm called, but not when
      I'm defined"

      >>some_class(). some_method()
      Traceback (most recent call last):
      File "<pyshell#1 4>", line 1, in <module>
      some_class().so me_method()
      File "<pyshell#1 1>", line 3, in raise_exception
      raise Exception
      Exception
      >>>

      Comment

      • Alex G

        #4
        Re: Functions that raise exceptions.

        I'm sorry about the typos, but that doesn't seem to be what the issue
        is (I typed it into the textbox rather carelessly, I apologize :-( ).
        It seems to be an issue with passing the decorator an argument:

        Given:

        def decorator(arg):
        def raise_exception (fn):
        raise Exception
        return raise_exception

        If you pass the decorator an argument, it doesn't work as expected
        (but if you neglect the argument, it works, even though the decorator
        _expects_ an argument.

        That is,

        class classA(object):
        @decorator('arg ument')
        def some_method(sel f):
        print "An exception should be raised when I'm called, but not
        when I'm defined"

        Will result in an exception on definition.

        class classB(object):
        @decorator
        def some_method(sel f):
        print "An exception should be raised when I'm called, but not
        when I'm defined"

        Gets defined, and executing

        b = classB()
        b.some_method()
        >>b = classB()
        >>b.some_method ()
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "<stdin>", line 3, in raise_exception
        Exception

        works as expected, even though decorator is expecting an argument...

        Comment

        • Duncan Booth

          #5
          Re: Functions that raise exceptions.

          Alex G <alexander.girm an@gmail.comwro te:
          class classA(object):
          @decorator('arg ument')
          def some_method(sel f):
          print "An exception should be raised when I'm called, but not
          when I'm defined"
          >
          Will result in an exception on definition.
          Well, yes it would. That's because what you defined a decorator. i.e. a
          function which wraps the method in another function. A decorator is called
          with a single argument: the function/method to be wrapped.

          What you have done in the code above is to call something named 'decorator'
          which isn't a decorator at all, it's a function that you expect to return a
          decorator (a decorator factory if you wish). If you make it return a
          decorator then it will all work fine:
          >>def decoratorfactor y(s):
          def decorator(arg):
          def raise_exception (fn):
          raise Exception
          return raise_exception
          return decorator
          >>class some_class(obje ct):
          @decoratorfacto ry('somestring' )
          def some_method(sel f):
          print "An exception should be raised when I'm called, but not when
          I'm defined"

          >>some_class(). some_method()
          Traceback (most recent call last):
          File "<pyshell#2 4>", line 1, in <module>
          some_class().so me_method()
          File "<pyshell#2 1>", line 4, in raise_exception
          raise Exception
          Exception
          >>>

          Comment

          Working...