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

Use pickle instead of repr/ast.literal_eval #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions colcon_python_setup_py/package_identification/python_setup_py.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0

import ast
import distutils.core
import os
import pickle
from pathlib import Path
import runpy
try:
Expand Down Expand Up @@ -134,11 +134,11 @@ def get_setup_arguments(setup_py):
setuptools.setup = setuptools_setup
except NameError:
pass
# filter out any data which doesn't work with ast.literal_eval
# filter out any data which doesn't serialize
for key, value in list(data.items()):
try:
ast.literal_eval(repr(value))
except SyntaxError:
pickle.dumps(value)
except pickle.PicklingError:
del data[key]
return data

Expand Down Expand Up @@ -226,20 +226,22 @@ def get_setup_arguments_with_context(setup_py, env):
pkg_path = str(pkg_path).replace(os.sep, os.altsep)
setup_py = str(setup_py).replace(os.sep, os.altsep)
code_lines = [
'import pickle',
'import sys',
"sys.path.insert(0, '%s')" % pkg_path,
'from colcon_python_setup_py.package_identification.python_setup_py'
' import get_setup_arguments',
"output = repr(get_setup_arguments('%s'))" % setup_py,
"sys.stdout.buffer.write(output.encode('utf-8'))"]
"output = get_setup_arguments('%s')" % setup_py,
'pickle.dump(output, sys.stdout.buffer)'
]

# invoke get_setup_arguments() in a separate interpreter
cmd = [sys.executable, '-c', ';'.join(code_lines)]
result = subprocess.run(
cmd, stdout=subprocess.PIPE, env=env, check=True)
output = result.stdout.decode('utf-8')
output = result.stdout

return ast.literal_eval(output)
return pickle.loads(output)


_setup_information_cache = {}
Expand Down Expand Up @@ -269,6 +271,7 @@ def get_setup_information(setup_py, *, env=None):
def _get_setup_information(setup_py, *, env=None):
code_lines = [
'import sys',
'import pickle',
'from distutils.core import run_setup',

'dist = run_setup('
Expand All @@ -293,14 +296,14 @@ def _get_setup_information(setup_py, *, env=None):
# skip values with custom type OrderedSet
" if k not in ('license_files', 'provides_extras')}",

"sys.stdout.buffer.write(repr(data).encode('utf-8'))"]
'pickle.dump(data, sys.stdout.buffer)']

# invoke distutils.core.run_setup() in a separate interpreter
cmd = [
sys.executable, '-c', ';'.join(line.lstrip() for line in code_lines)]
result = subprocess.run(
cmd, stdout=subprocess.PIPE,
cwd=os.path.abspath(str(setup_py.parent)), check=True, env=env)
output = result.stdout.decode('utf-8')
output = result.stdout

return ast.literal_eval(output)
return pickle.loads(output)