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

print to stderr #15704

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def load_conanfile_txt(self, conan_txt_path):
conanfile._conan_is_consumer = True
return conanfile

def _parse_conan_txt(self, contents, path, display_name):
@staticmethod
def _parse_conan_txt(contents, path, display_name):
conanfile = ConanFile(display_name)

try:
Expand Down Expand Up @@ -320,6 +321,9 @@ def _load_python_file(conan_file_path):
if not os.path.exists(conan_file_path):
raise NotFoundException("%s not found!" % conan_file_path)

def new_print(*args, **kwargs): # Make sure that all user python files print() goes to stderr
print(*args, **kwargs, file=sys.stderr)

module_id = str(uuid.uuid1())
current_dir = os.path.dirname(conan_file_path)
sys.path.insert(0, current_dir)
Expand Down Expand Up @@ -362,6 +366,7 @@ def _load_python_file(conan_file_path):
else:
if folder.startswith(current_dir):
module = sys.modules.pop(added)
module.print = new_print
Copy link
Contributor

@jcar87 jcar87 Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this - but makes me wonder if there are any risks since print is a built-in? thinking of unusual scenarios like a frozen interpreter embedded in an executable - but it looks good otherwise!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look, and in theory it seems ok, this kind of overwrite of module globals seems allowed, it is not changing the built-in print, just making the module print() pointing to another function, and this is done per-module, not globally.

sys.modules["%s.%s" % (module_id, added)] = module
except ConanException:
raise
Expand All @@ -373,6 +378,7 @@ def _load_python_file(conan_file_path):
finally:
sys.path.pop(0)

loaded.print = new_print
return loaded, module_id


Expand Down
35 changes: 35 additions & 0 deletions conans/test/integration/conanfile/test_print_in_conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json
import textwrap

from conans.test.utils.tools import TestClient


def test_print_in_conanfile():
"""
Tests that prints in conanfiles will not ruin json stdout outputs
"""
c = TestClient(light=True)
other = textwrap.dedent("""
def myprint(text):
print(text)
""")
conanfile = textwrap.dedent("""
import other
from conan import ConanFile

class MyTest(ConanFile):
name = "pkg"
version = "0.1"

def generate(self):
print("Hello world!!")
other.myprint("Bye world!!")
""")
c.save({"other.py": other,
"conanfile.py": conanfile})
c.run("install . --format=json")
assert "Hello world!!" in c.stderr
assert "Bye world!!" in c.stderr
info = json.loads(c.stdout)
# the json is correctly loaded
assert "graph" in info