Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect pep-0263 #249

Open
patrikha opened this issue Feb 11, 2022 · 0 comments
Open

Detect pep-0263 #249

patrikha opened this issue Feb 11, 2022 · 0 comments

Comments

@patrikha
Copy link

patrikha commented Feb 11, 2022

It would be great if the following header is supported:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

please see:
pep-0263

hax workaround:

def get_encoding(path):
    if not isinstance(path, Path):
        path = Path(path)
    with open(path, 'rb') as stream:
        raw_bytes = stream.read(1024)
    encoding = chardet.detect(raw_bytes)['encoding']
    if path.suffix.lower() == '.py':
        start_pos = raw_bytes.find(b'-*- coding: ')
        if start_pos != -1:
            start_pos += 12
            end_pos = raw_bytes[start_pos:].find(b' -*-')
            if end_pos:
                end_pos += start_pos
                coding = raw_bytes[start_pos:end_pos]
                encoding = coding.decode(encoding)
    return encoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant