473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Message from exception raised in generator disappears

I thought I was being pretty clever with my first attempt at using generators,
but I seem to be missing some crucial concept, for even though this seems to
work as intended, the text of the exception message does not bubble up with
either of the ValueErrors when one of them is raised.
# This helps iterate over a unicode string. When python is built with
# 16-bit chars (as is the default on Windows), it returns surrogate
# pairs together (unlike 'for c in s'), and detects illegal surrogate
# pairs. Byte strings are unaffected.
def chars(s):
surrogate = None
for c in s:
cp = ord(c)
if surrogate is not None:
if cp > 56319 and cp < 57344:
pair = surrogate + c
surrogate = None
yield pair
else:
raise ValueError("Bad surrogate pair in %s" % s)
else:
if cp > 55295 and cp < 57344:
if cp < 56320:
surrogate = c
else:
raise ValueError("Bad surrogate pair in %s" %s)
else:
surrogate = None
yield c
if surrogate is not None:
raise ValueError("Bad surrogate pair at end of %s" % s)
# as expected, returns u'example \xe9...\u2022...\U00010000...\U0010fffd'
''.join([c for c in chars(u'example \xe9...\u2022...\ud800\udc00...\U0010fffd')])

# now test the 3 exception conditions. Each produces a ValueError
''.join([c for c in chars(u'2nd half bad: \ud800bogus')])
''.join([c for c in chars(u'no 1st half: \udc00')])
''.join([c for c in chars(u'no 2nd half: \ud800')])
All 3 result of the exception tests result in a bare ValueError; there's no
"Bad surrogate pair in" message shown. Why is thta? What am I doing wrong?
Jul 18 '05 #1
1 1654
Mike Brown wrote:
I thought I was being pretty clever with my first attempt at using generators,
but I seem to be missing some crucial concept, for even though this seems to
work as intended, the text of the exception message does not bubble up with
either of the ValueErrors when one of them is raised.
# This helps iterate over a unicode string. When python is built with
# 16-bit chars (as is the default on Windows), it returns surrogate
# pairs together (unlike 'for c in s'), and detects illegal surrogate
# pairs. Byte strings are unaffected.
def chars(s):
surrogate = None
for c in s:
cp = ord(c)
if surrogate is not None:
if cp > 56319 and cp < 57344:
pair = surrogate + c
surrogate = None
yield pair
else:
raise ValueError("Bad surrogate pair in %s" % s)
else:
if cp > 55295 and cp < 57344:
if cp < 56320:
surrogate = c
else:
raise ValueError("Bad surrogate pair in %s" %s)
else:
surrogate = None
yield c
if surrogate is not None:
raise ValueError("Bad surrogate pair at end of %s" % s)
# as expected, returns u'example \xe9...\u2022...\U00010000...\U0010fffd'
''.join([c for c in chars(u'example \xe9...\u2022...\ud800\udc00...\U0010fffd')])

# now test the 3 exception conditions. Each produces a ValueError
''.join([c for c in chars(u'2nd half bad: \ud800bogus')])
''.join([c for c in chars(u'no 1st half: \udc00')])
''.join([c for c in chars(u'no 2nd half: \ud800')])
All 3 result of the exception tests result in a bare ValueError; there's no
"Bad surrogate pair in" message shown. Why is thta? What am I doing wrong?


The problem is that type('abc%s' % u'\udc00') is unicode, not str.
Change your raises to something like:
raise ValueError("Bad surrogate pair at end of %r" % s)
and the you can relax.

-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Green | last post by:
I'm encountering a place where if I use a for loop I can't catch the error raised by a member. I've tested python 2.3 and 2.3.1 ( the only ones I had quickly around ) and it's still occuring. ...
3
by: Calvin Spealman | last post by:
I was wondering if it was possible, now or with a patch, to do either of the following: 1) Cause another thread's most recent function, or a given function in a given thread, to yield its...
8
by: Lonnie Princehouse | last post by:
In a recent post, Michele Simionato asked about resumable (or re-entrant) exceptions, the basic idea being that when raised and uncaught, these exceptions would only propagate backwards through a...
4
by: james t kirk | last post by:
I'm writing a wrapper class to handle the line merging and filtering for a log file analysis app The problem I'm running into is that the StopIteration exception raised when the wrapped file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.