Suggestion for os.path.join

David Bolen db3l at fitlinxx.com
Sun May 27 22:04:57 EDT 2001


boogiemorg at aol.com (David Morgenthaler) writes:

> On Windows os.path.normpath converts everything to '\', and this is
> the problem. The '\' works well with anything Pythonic. But when
> used with C-extensions the '\' becomes an escape character within
> the C string, thus ruining the path.

I'm not sure I follow this - within source both Python and C use "\"
as an escape sequence in strings, but neither interpret actual string
memory contents using escape sequences.  Since anything you pass to a
C extension is by definition already in memory, there's no such
thing as being an escape character - unless you send that string
through some other sort of interpreter that is going to deal with it
that way.

Or for example:

    import cextension

    mystring = "\\a\\file\\path"

    cextension.function(mystring)

Now, internally in the memory location that holds the string object to
which mystring refers is the literal string "\a\file\path".  The \\
was necessary for the Python interpreter but are not in the literal
string.

Note that printing out mystring may or may not show "\\" depending on
whether str() or repr() is used, but that's a visual representation
and not the actual contents of the string object.

When the C extension is called, it's the reference to that buffer that
is passed through (or rather the string object from which the C
extension can extract the byte sequence), and that buffer is perfectly
valid as a file path.

This is very different that trying to define that file path within C
source that will be interpreted by the compiler, in which case you need
the same quoting mechanism used in the Python source above.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list