From ff846fde1bc54bd03b87bbb90b94c85da10655a4 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Fri, 5 Aug 2022 11:21:48 -0400 Subject: [PATCH] [pipreqs/pipreqs.py] Use `codec` to get an `open` with `encoding` argument ; fix `__version__` read (import cannot work) ; don't crash on `UnicodeDecodeError` --- pipreqs/pipreqs.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 24eeeb7..48a8c77 100644 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -43,12 +43,24 @@ import re import logging import ast import traceback +from codecs import open + from docopt import docopt import requests from yarg import json2package from yarg.exceptions import HTTPError -from pipreqs import __version__ +if sys.version[0] == "2": + from itertools import imap as map, ifilter as filter + +with open(os.path.join(os.path.dirname(__file__), "__init__.py"), "rt") as f: + __version__ = next(map( + lambda buf: next(map(lambda e: e.value.s, ast.parse(buf).body)), + filter( + lambda line: line.startswith("__version__"), + f, + ), + )) REGEXP = [ re.compile(r'^import (.+)$'), @@ -190,6 +202,10 @@ def get_imports_info( logging.debug( 'Package %s does not exist or network problems', item) continue + except UnicodeDecodeError: + logging.debug( + 'Package %s could not be read from server', item) + continue result.append({'name': item, 'version': data.latest_release_id}) return result