Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53408

Re: UnicodeDecodeError issue

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'charset:iso-8859-7': 0.04; 'output': 0.05; 'say,': 0.05; 'skip:\\ 20': 0.07; 'variables': 0.07; 'happen,': 0.09; 'label,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:\\ 40': 0.09; 'try:': 0.09; 'assume': 0.14; '"w")': 0.16; "'w'": 0.16; 'all?': 0.16; 'block.': 0.16; 'earlier.': 0.16; 'formatted': 0.16; 'occurs,': 0.16; 'ought': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'set,': 0.16; 'subject:issue': 0.16; 'truncates': 0.16; 'exception': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'first.': 0.19; 'skip:p 40': 0.19; 'value.': 0.19; 'examples': 0.20; 'written': 0.21; '>>>': 0.22; 'code,': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'simpler': 0.24; 'skip': 0.24; 'earlier': 0.24; 'file.': 0.24; "haven't": 0.24; 'define': 0.26; 'possibly': 0.26; 'header:X -Complaints-To:1': 0.27; 'record': 0.27; 'host': 0.29; 'chris': 0.29; 'mode': 0.30; 'statement': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'exceptions': 0.31; 'initialized': 0.31; 'sep': 0.31; 'skip:= 20': 0.31; 'file': 0.32; 'open': 0.33; 'alone': 0.33; 'skip:b 30': 0.33; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'doing': 0.36; 'useful': 0.36; 'should': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'handle': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'anything': 0.39; 'does': 0.39; 'realize': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'tell': 0.60; 'skip:o 30': 0.61; 'course': 0.61; "you're": 0.61; 'first': 0.61; 'making': 0.63; 'kind': 0.63; 'more': 0.64; 'city': 0.66; 'here': 0.66; 'anything.': 0.68; 'study': 0.69; 'further,': 0.74; 'coffee': 0.84; 'presumably': 0.84; 'starters,': 0.84; 'wipe': 0.84; 'drink': 0.93; 'examine': 0.93; 'tied': 0.93; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: UnicodeDecodeError issue
Date Sun, 1 Sep 2013 10:35:07 +0000 (UTC)
References <kvs36n$3ap$1@news.grnet.gr> <ce40cfed-978b-41d6-9fca-c1a9d54855cf@googlegroups.com> <mailman.439.1378019568.19984.python-list@python.org> <kvuq27$7ef$2@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-7
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 174.32.174.36
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.443.1378031734.19984.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1378031734 news.xs4all.nl 15939 [2001:888:2000:d::a6]:42421
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53408

Show key headers only | View raw


On 1/9/2013 03:23, Ferrous Cranus wrote:

> Στις 1/9/2013 10:12 πμ, ο/η Chris Angelico έγραψε:
>> On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>>> Ye i'm aware that i need to define variables before i try to make use of them.
>>> I have study all of your examples and then re-view my code and i can *assure* you that the line statement that tied to set the 'host' variable is very early at the top of the script(of course after imports), and the cur.execute comes after.
>>>
>>> The problem here is not what you say, that i try to drink k a coffee before actually making one first but rather than i cannot drink the coffee although i know *i have tried* to make one first.
>>>
>>>
>>> i will upload the code for you to prove my sayings at pastebin.
>>>
>>> http://pastebin.com/J97guApQ
>>
>> You're setting host inside a try/except block. Are you getting
>> exceptions that prevent it from being set, perhaps?
>>

   <SNIP>
>
> Let alone that i when i try to set the 'host' variable i get this line 
> at my '/tmp/err.out'
>
>
> nikos@superhost.gr [~]# cat /tmp/err.out
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
> 'invalid start byte')
>

Let's examine the relevant code from your pastebin above:


try:
        gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
        city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
        host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy Detected"
except Exception as e:
        print( repr(e), file=open( '/tmp/err.out', 'w' ) )
 
Presumably you do realize that any exception on any of the three lines
will skip the remainder, and print that line to the /tmp/err.out file? 
And that if any exception occurs, it'll be before 'host' is initialized
at all?  Possibly before 'city' is initialized, or even 'gi' ?   So
there's no reason yet to assume that somehow setting the 'host'
variable triggers anything. When in doubt, consider including only one
line in any given try block. But let's try simpler changes.

Your file mode is 'w' which will wipe out anything written earlier.  Yet
if the exception does not happen, the file will still contain error(s)
from some earlier run.  You should open (and truncate) the file before
the first use, then use the same handle in each exception at which you
want to record any information.  Further, you ought to put more into the
file than just the repr(e) value.  For starters, the Traceback would be
nice.

If I were doing it, I'd be printing  repr(sys.exc_info()) to that file. 
I'd also have some kind of label, so that once I had more than one of
these in the code, I'd have an easyt way to tell which was which.

This is my first crack at it (untested):

errout = open("/tmp/err.out", "w")   #opens and truncates the error
output file
try:
    gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
    city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
    host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
"Proxy Detected"
except Exception as e:
    print( "Xyzzy exception-", repr(sys.exc_info()), file=errout)
    errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier.  But right now, you need to stop throwing
away useful information.


-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 09:41 +0300
  Re: UnicodeDecodeError issue Chris Angelico <rosuav@gmail.com> - 2013-08-31 16:53 +1000
    Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 10:02 +0300
      Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 10:18 +0300
  Re: UnicodeDecodeError issue Peter Otten <__peter__@web.de> - 2013-08-31 09:25 +0200
    Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 10:58 +0300
      Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 11:31 +0300
        Re: UnicodeDecodeError issue Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 11:28 +0000
          Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 15:58 +0300
            Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-08-31 16:07 +0300
            Re: UnicodeDecodeError issue Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 15:44 +0000
  Re: UnicodeDecodeError issue Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-08-31 23:50 -0700
    Re: UnicodeDecodeError issue Chris Angelico <rosuav@gmail.com> - 2013-09-01 17:12 +1000
      Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-01 10:23 +0300
        Re: UnicodeDecodeError issue Chris Angelico <rosuav@gmail.com> - 2013-09-01 17:28 +1000
        Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-01 10:35 +0000
          Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-01 16:59 +0300
            Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-01 15:40 +0000
        Re: UnicodeDecodeError issue Chris Angelico <rosuav@gmail.com> - 2013-09-01 20:51 +1000
    Re: UnicodeDecodeError issue Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-01 08:35 +0000
      Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-01 17:08 +0300
        Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-01 17:25 +0300
        Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-01 15:36 +0000
          Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-01 19:10 +0300
            Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-02 01:23 +0300
              Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-01 23:14 +0000
                Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-02 07:16 +0300
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-02 11:38 +0000
                Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-02 14:49 +0300
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-02 12:21 +0000
                Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-02 18:05 +0300
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-02 18:28 +0000
                Re: UnicodeDecodeError issue Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-09-04 01:35 -0700
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-04 11:26 +0000
                Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-04 14:38 +0300
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-04 12:38 +0000
                Re: UnicodeDecodeError issue Ferrous Cranus <nikos@superhost.gr> - 2013-09-04 17:29 +0300
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-05 00:17 +0000
                Re: UnicodeDecodeError issue Steven D'Aprano <steve@pearwood.info> - 2013-09-05 03:07 +0000
                Re: UnicodeDecodeError issue Chris Angelico <rosuav@gmail.com> - 2013-09-05 13:59 +1000
                Re: UnicodeDecodeError issue Steven D'Aprano <steve@pearwood.info> - 2013-09-05 05:28 +0000
                Re: UnicodeDecodeError issue MRAB <python@mrabarnett.plus.com> - 2013-09-02 12:56 +0100
                Re: UnicodeDecodeError issue Dave Angel <davea@davea.name> - 2013-09-02 12:24 +0000
                Re: UnicodeDecodeError issue MRAB <python@mrabarnett.plus.com> - 2013-09-02 15:44 +0100
                Re: UnicodeDecodeError issue wxjmfauth@gmail.com - 2013-09-03 08:23 -0700
                Re: UnicodeDecodeError issue Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-04 10:01 +0200
                Re: UnicodeDecodeError issue wxjmfauth@gmail.com - 2013-09-04 07:08 -0700
                Re: UnicodeDecodeError issue Chris Angelico <rosuav@gmail.com> - 2013-09-03 08:45 +1000
                Re: UnicodeDecodeError issue Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-03 14:56 +0000
                Re: UnicodeDecodeError issue Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-02 20:49 -0400

csiph-web