Anything like 'inspect.getsourceencoding()'?

J

Jeff Epler

Is there a convenient way to find the encoding of a source file? I
thought maybe this would be in the inspect module, but I didn't see it
there. Just as nice would be a way to get the file as a unicode string,
I suppose.

(This is related to another thread I've recently posted to, where
another user was having trouble with pydoc's links to source files using
the file: protocol. I suggested having pydoc serve the source files,
and provided a patch, but it's crossed my mind that it would be nice to
tell the browser the encoding of that file.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBcdWJJd01MZaTXX0RAnyUAJ9dN2Shq85BpM/LH6+QinvL3OD40gCfW7Hb
1zvj6WpqwJbtTk1bA1+4+nM=
=K3uY
-----END PGP SIGNATURE-----
 
M

Maciej Dziardziel

Jeff said:
Is there a convenient way to find the encoding of a source file? I
thought maybe this would be in the inspect module, but I didn't see it
there. Just as nice would be a way to get the file as a unicode string,
I suppose.

(This is related to another thread I've recently posted to, where
another user was having trouble with pydoc's links to source files using
the file: protocol. I suggested having pydoc serve the source files,
and provided a patch, but it's crossed my mind that it would be nice to
tell the browser the encoding of that file.

Jeff

According to Python documentation:

It is possible to use encodings different than ASCII in Python source files.
The best way to do it is to put one more special comment line right after
the #! line to define the source file encoding:

# -*- coding: iso-8859-1 -*-
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Jeff said:
Is there a convenient way to find the encoding of a source file? I
thought maybe this would be in the inspect module, but I didn't see it
there. Just as nice would be a way to get the file as a unicode string,
I suppose.

No. inspect operates on the byte-code/internal representation level, and
at that level, there is no notion of source encoding. The source
encoding information gets lost during compilation (as it is no longer
needed).

Somebody proposed preserving it in __[en]coding__, but that hasn't been
implemented.

Regards,
Martin
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top