[Tutor] Weird error message

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Nov 4 19:42:14 EST 2003



On Tue, 4 Nov 2003, Conrad Koziol wrote:

> Hey thanks for the tip Danny but it still doesnt work. This is the last
> bit of the code
>
> 	posts = posttemplate
> 	subsitutes = ['NAME', 'DATE', 'SUBJECT', 'POST']
> 	list_of_posts = []
> 	content1 = re.escape('<!--INSERT NAME HERE-->')
> 	content2 = re.escape('<!--INSERT DATE HERE-->')
> 	content3 = re.escape('<!--INSERT SUBJECT HERE-->')
> 	content4 = re.escape('<!--INSERT POST HERE-->')
>
> 	for post in postsinfo:
> 		for value in range(4):
> 			posts = re.sub(content1, post[1], posts)
> 			posts = re.sub(content2, post[2], posts)
> 			posts = re.sub(content3, post[3], posts)
> 			posts = re.sub(content4, post[4], posts)


Hi Conrad,



What is post[1], post[2], post[3], and post[4]?  If any of these is
'None', we can get a similar "len() of unsized object" error message:

###
>>> re.sub('', None, "hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre.py",
line 143, in sub
    return _compile(pattern, 0).sub(repl, string, count)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre.py",
line 257, in _subx
    template = _compile_repl(template, pattern)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre.py",
line 242, in _compile_repl
    p = sre_parse.parse_template(repl, pattern)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre_parse.py",
line 645, in parse_template
    s = Tokenizer(source)
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre_parse.py",
line 186, in __init__
    self.__next()
  File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre_parse.py",
line 188, in __next
    if self.index >= len(self.string):
TypeError: len() of unsized object
###


I'd recommend looking more closely at what the program is sending to
re.sub(), since it's possible that post[1] contains something other than
what you're expecting.



Also, I'm not quite sure I understand the reason for the inner loop:

> 	for post in postsinfo:
> 		for value in range(4):
> 			posts = re.sub(content1, post[1], posts)
> 			posts = re.sub(content2, post[2], posts)
> 			posts = re.sub(content3, post[3], posts)
> 			posts = re.sub(content4, post[4], posts)
> 			list_of_posts.append(posts)
> 			posts = posttemplate


The code doesn't do anything with the 'value' of the loop, so that raises
my suspicion.  *grin* Furthermore, each post ends up getting appended four
times.



Hope this helps!




More information about the Tutor mailing list