global name not defined

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

    #1

    global name not defined

    I added a function 'warn_Admin' and defined it just before another
    function 'process_log'. 'process_log' calls this warn_Admin' function.
    However, when it gets called i get the following error every time:
    ---
    Traceback (most recent call last):
    File "/usr/bin/denyhosts.py", line 202, in ?
    first_time, noemail, daemon)
    File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    86, in __init__
    last_offset)
    File "/usr/lib/python2.3/site-packages/DenyHosts/daemon.py", line 74,
    in createDaemon
    apply(func, args)
    File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    137, in runDaemon
    purge_time, purge_sleep_rat io)
    File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    178, in daemonLoop
    last_offset = self.process_lo g(logfile, last_offset)
    File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    380, in process_log
    [warn_Admin(ip) for ip in new_denied_host s]
    NameError: global name 'warn_Admin' is not defined
    --
    If I take the two functions out of their current environment and store
    them in test file and run it, it doesn't complain. I'm new to python
    so I'm guessing there is some weird scope rule I am missing. I did try
    'self.warn_Admi n(ip)' just to be safe but then I got a 'too many
    arguments' error?

    I'm lost :)

    the added function plus the header of the existing function(its too
    large):
    ------------
    def warn_Admin(warn _ip):
    SENDMAIL = "/usr/sbin/sendmail" # sendmail location
    p = os.popen("%s -t" % SENDMAIL, "w")
    p.write("To: kevin@netkev.co m\n")
    p.write("Subjec t: test from denyhosts\n")
    p.write("\n") # blank line separating headers from body
    p.write("Some text\n")
    p.write(warn_ip )
    sts = p.close()
    if sts != 0:
    info("Sendmail exit status: %s", sts)
    return sts


    def process_log(sel f, logfile, offset):
    -------------

    the call to warn_Admin from process_log:
    ---
    if new_denied_host s:
    info("new denied hosts: %s", str(new_denied_ hosts))
    #[info(ip) for ip in new_denied_host s]
    [warn_Admin(ip) for ip in new_denied_host s]
    else:
    debug("no new denied hosts")

    -kevin

  • Paul McGuire

    #2
    Re: global name not defined

    "NetKev" <kevin@netkev.c om> wrote in message
    news:1148336761 .776348.260450@ j33g2000cwa.goo glegroups.com.. .[color=blue]
    > I added a function 'warn_Admin' and defined it just before another
    > function 'process_log'. 'process_log' calls this warn_Admin' function.
    > However, when it gets called i get the following error every time:
    > ---
    > Traceback (most recent call last):
    > File "/usr/bin/denyhosts.py", line 202, in ?
    > first_time, noemail, daemon)
    > File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    > 86, in __init__
    > last_offset)
    > File "/usr/lib/python2.3/site-packages/DenyHosts/daemon.py", line 74,
    > in createDaemon
    > apply(func, args)
    > File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    > 137, in runDaemon
    > purge_time, purge_sleep_rat io)
    > File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    > 178, in daemonLoop
    > last_offset = self.process_lo g(logfile, last_offset)
    > File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
    > 380, in process_log
    > [warn_Admin(ip) for ip in new_denied_host s]
    > NameError: global name 'warn_Admin' is not defined
    > --
    > If I take the two functions out of their current environment and store
    > them in test file and run it, it doesn't complain. I'm new to python
    > so I'm guessing there is some weird scope rule I am missing. I did try
    > 'self.warn_Admi n(ip)' just to be safe but then I got a 'too many
    > arguments' error?
    >
    > I'm lost :)
    >
    > the added function plus the header of the existing function(its too
    > large):
    > ------------
    > def warn_Admin(warn _ip):
    > SENDMAIL = "/usr/sbin/sendmail" # sendmail location
    > p = os.popen("%s -t" % SENDMAIL, "w")
    > p.write("To: kevin@netkev.co m\n")
    > p.write("Subjec t: test from denyhosts\n")
    > p.write("\n") # blank line separating headers from body
    > p.write("Some text\n")
    > p.write(warn_ip )
    > sts = p.close()
    > if sts != 0:
    > info("Sendmail exit status: %s", sts)
    > return sts
    >
    >
    > def process_log(sel f, logfile, offset):
    > -------------
    >
    > the call to warn_Admin from process_log:
    > ---
    > if new_denied_host s:
    > info("new denied hosts: %s", str(new_denied_ hosts))
    > #[info(ip) for ip in new_denied_host s]
    > [warn_Admin(ip) for ip in new_denied_host s]
    > else:
    > debug("no new denied hosts")
    >
    > -kevin
    >[/color]

    Sounds like warn_Admin is defined within a class.
    a. could not resolve name when call was not qualified with "self."
    b. when called as "self.warn_Admi n", name was resolved, but got "too many
    arguments" - this is because there was no explicit self argument in the
    definition of warn_Admin.

    It doesn't look like warn_Admin needs to be in the class. Move warn_Admin
    to module-level scope, outside of the class containing process_log, and see
    if things work better.

    -- Paul


    -- Paul


    Comment

    • NetKev

      #3
      Re: global name not defined

      You are probably right and I think I will do so but just for the sake
      of my understanding of python...I noticed somthing. process_log takes
      two arguments when called but it's definition has 3 and one of them is
      "self". So I'm thinking if I modify my warn_Admin definition to
      include "self" and then call it from process_log with
      self.warn_Admin ... it will work. This explains why I was getting the
      "too many arguments" error.

      Comment

      • Paul McGuire

        #4
        Re: global name not defined

        "NetKev" <kevin@netkev.c om> wrote in message
        news:1148340731 .742024.270290@ j73g2000cwa.goo glegroups.com.. .[color=blue]
        > You are probably right and I think I will do so but just for the sake
        > of my understanding of python...I noticed somthing. process_log takes
        > two arguments when called but it's definition has 3 and one of them is
        > "self". So I'm thinking if I modify my warn_Admin definition to
        > include "self" and then call it from process_log with
        > self.warn_Admin ... it will work. This explains why I was getting the
        > "too many arguments" error.
        >[/color]

        Yes. When you invoke self.warn_Admin (x), it calls warn_Admin with 2 args,
        self and x.

        The reason I did not suggest this is becaus it looked like warn_Admin didn't
        really use anything inside self, so why make it a method?

        Looks like you are getting the method/function concepts straight.

        (I'm not trying to confuse you, but you could also make warn_Admin a
        staticmethod within the class, using the @staticmethod decorator. Static
        methods do not pass the self argument, so making warn_Admin into a static
        method would be another way to resolve this problem. But only do this if
        your class, whatever it is, has something inherently about it that wants its
        own warn_Admin method - otherwise, just make it a global function.)

        -- Paul


        Comment

        • bruno at modulix

          #5
          Re: global name not defined

          NetKev wrote:
          (snip)[color=blue]
          > def process_log(sel f, logfile, offset):
          > if new_denied_host s:
          > info("new denied hosts: %s", str(new_denied_ hosts))
          > [warn_Admin(ip) for ip in new_denied_host s][/color]

          This uselessly builds a list. List comprehension is meant to create
          lists, not to replace for loops.

          for ip in new_denied_host s:
          warn_admin(ip)


          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • NetKev

            #6
            Re: global name not defined

            good point

            Comment

            Working...