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

fix print - stderr #15934

Merged
merged 1 commit into from
Mar 25, 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
2 changes: 1 addition & 1 deletion conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def _load_python_file(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
kwargs['file'] = sys.stderr
kwargs.setdefault("file", sys.stderr)
print(*args, **kwargs)

module_id = str(uuid.uuid1())
Expand Down
35 changes: 27 additions & 8 deletions conans/test/unittests/model/conanfile_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import textwrap
import unittest

from conans.model.conan_file import ConanFile
Expand Down Expand Up @@ -40,12 +41,30 @@ def package_info(self):

def test_conanfile_new_print(self):
client = TestClient()
conanfile = """from conan import ConanFile
import sys
class Pkg(ConanFile):
def source(self):
print("Test", file=sys.stderr)
print("Test")
"""
conanfile = textwrap.dedent("""
from conan import ConanFile
import sys
class Pkg(ConanFile):
def source(self):
print("Test1", file=sys.stderr)
print("Test2")
""")
client.save({"conanfile.py": conanfile})
client.run("source .")
assert "Test1" in client.stderr
assert "Test2" in client.stderr
assert "" == client.stdout

def test_conanfile_new_print_save(self):
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
import sys
class Pkg(ConanFile):
def source(self):
with open("myfile.txt", "w") as f:
print("Test1", file=f)
""")
client.save({"conanfile.py": conanfile})
client.run("source .")
client.run("source .")
assert "Test1" in client.load("myfile.txt")