Anything like 'inspect.getsourceencoding()'?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Epler

    Anything like 'inspect.getsourceencoding()'?

    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)

    iD8DBQFBcdWJJd0 1MZaTXX0RAnyUAJ 9dN2Shq85BpM/LH6+QinvL3OD40g CfW7Hb
    1zvj6WpqwJbtTk1 bA1+4+nM=
    =K3uY
    -----END PGP SIGNATURE-----

  • Maciej Dziardziel

    #2
    Re: Anything like 'inspect.getsou rceencoding()'?

    Jeff Epler wrote:
    [color=blue]
    > 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[/color]

    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 -*-


    --
    Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)


    How can you be ALONE with somebody? Think about it...

    Comment

    • Martin v. Löwis

      #3
      Re: Anything like 'inspect.getsou rceencoding()'?

      Jeff Epler wrote:[color=blue]
      > 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.[/color]

      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

      Comment

      Working...