glob.glob unicode bug or feature

E

Elbert Lev

#Here is the script
#Python 2.3 on W2K

import glob

name = glob.glob(u"./*.mp3")[0]
print type(name)
name = glob.glob(u"*.mp3")[0]
print type(name)

##OUTPUT##
#<type 'unicode'>
#<type 'str'>

#Is this a bug, or a feature? I beleve it's a bug.
 
S

Skip Montanaro

Elbert> #Is this a bug, or a feature? I beleve it's a bug.

Dunno. Can you submit a bug report on SourceForge?

Skip
 
P

Peter Otten

Elbert said:
#Here is the script
#Python 2.3 on W2K

import glob

name = glob.glob(u"./*.mp3")[0]
print type(name)
name = glob.glob(u"*.mp3")[0]
print type(name)

##OUTPUT##
#<type 'unicode'>
#<type 'str'>

#Is this a bug, or a feature? I beleve it's a bug.

The information whether glob() was passed a unicode or str is lost in the
following line in glob.glob1():

if not dirname: dirname = os.curdir

A quick fix would be

if not dirname:
dirname = type(dirname)(os.curdir)

This will work if os.curdir consists entirely of characters in the ASCII
range, which I believe to be the case for all available platforms. Is there
a portable way to convert a str filename to unicode or should the os module
grow a unicode curdiru (uuugly name, but there already is os.getcwdu())?

Peter
 
N

Neil Hodgson

Peter Otten:
The information whether glob() was passed a unicode or str is lost in the
following line in glob.glob1():

if not dirname: dirname = os.curdir

A quick fix would be

if not dirname:
dirname = type(dirname)(os.curdir)

This didn't work for me as os.curdir has already been added in the call
to glob1. Instead, for Python 2.3, also changing line 22 inside glob() to

return glob1(type(dirname)(os.curdir), basename)

worked.

Neil
 
P

Peter Otten

Neil said:
This didn't work for me as os.curdir has already been added in the call
to glob1. Instead, for Python 2.3, also changing line 22 inside glob() to

return glob1(type(dirname)(os.curdir), basename)

worked.

I should have tried before posting. Now you point it out it seems I've
changed a piece of dead code. Or am I missing something ...again?

Peter
 
N

Neil Hodgson

Peter Otten:
Neil Hodgson wrote:
...
...
I should have tried before posting. Now you point it out it seems I've
changed a piece of dead code. Or am I missing something ...again?

It took quite a few examples for me to be convinced that the code is
dead. "leve" has submitted a bug and "nnorwitz" a patch to the Python bug
tracker and it works for me:

def glob1(dirname, pattern):
if not dirname: dirname = os.curdir
+ if isinstance(pattern, unicode): dirname = unicode(dirname)

http://sourceforge.net/tracker/index.php?func=detail&aid=1001604&group_id=5470&atid=305470

Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top