We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[Fix] sys.stderr.fileno() throws an AttributeError exception
When I tried to delivered the playwright tasks via `celery==4.4.7`, I got an `AttributeError` exception at line 50 in `main.py`. 

`except AttributeError:`

I've checked the source from `pytest`, the original code is:

 ```
     except (AttributeError, io.UnsupportedOperation):
            # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
            # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
            # This is potentially dangerous, but the best we can do.
            return sys.__stderr__.fileno()
```

It perfectly works if I changed it to the original code, so I think we should be consistent with the original source code to enhance compatibility.

I also checked the celery official docs. The reason why I got an `AttributeError` exception probably is that the log proxy of celery is not support the `stderr` attribute.

[Fix] sys.stderr.fileno() throws an AttributeError exception
When I tried to delivered the playwright tasks via `celery==4.4.7`, I got an `AttributeError` exception at line 50 in `main.py`. 

`except AttributeError:`

I've checked the source from `pytest`, the original code is:

 ```
     except (AttributeError, io.UnsupportedOperation):
            # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
            # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
            # This is potentially dangerous, but the best we can do.
            return sys.__stderr__.fileno()
```

It perfectly works if I changed it to the original code, so I think we should be consistent with the original source code to enhance compatibility.

I also checked the celery official docs. The reason why I got an `AttributeError` exception probably is that the log proxy of celery is not support the `stderr` attribute.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `playwright/main.py`
Content:
```
1 # Copyright (c) Microsoft Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import asyncio
16 import io
17 import os
18 import subprocess
19 import sys
20 from pathlib import Path
21 from typing import Any
22 
23 from greenlet import greenlet
24 
25 from playwright.async_api import Playwright as AsyncPlaywright
26 from playwright.connection import Connection
27 from playwright.helper import Error
28 from playwright.object_factory import create_remote_object
29 from playwright.path_utils import get_file_dirname
30 from playwright.playwright import Playwright
31 from playwright.sync_api import Playwright as SyncPlaywright
32 from playwright.sync_base import dispatcher_fiber, set_dispatcher_fiber
33 
34 
35 def compute_driver_executable() -> Path:
36     package_path = get_file_dirname()
37     platform = sys.platform
38     if platform == "win32":
39         return package_path / "driver" / "playwright-cli.exe"
40     return package_path / "driver" / "playwright-cli"
41 
42 
43 async def run_driver_async() -> Connection:
44     driver_executable = compute_driver_executable()
45 
46     # Sourced from: https://github.com/pytest-dev/pytest/blob/49827adcb9256c9c9c06a25729421dcc3c385edc/src/_pytest/faulthandler.py#L73-L80
47     def _get_stderr_fileno() -> int:
48         try:
49             return sys.stderr.fileno()
50         except io.UnsupportedOperation:
51             # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
52             # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
53             # This is potentially dangerous, but the best we can do.
54             return sys.__stderr__.fileno()
55 
56     proc = await asyncio.create_subprocess_exec(
57         str(driver_executable),
58         "run-driver",
59         stdin=asyncio.subprocess.PIPE,
60         stdout=asyncio.subprocess.PIPE,
61         stderr=_get_stderr_fileno(),
62         limit=32768,
63     )
64     assert proc.stdout
65     assert proc.stdin
66     connection = Connection(
67         proc.stdout, proc.stdin, create_remote_object, asyncio.get_event_loop()
68     )
69     return connection
70 
71 
72 def run_driver() -> Connection:
73     loop = asyncio.get_event_loop()
74     if loop.is_running():
75         raise Error("Can only run one Playwright at a time.")
76     return loop.run_until_complete(run_driver_async())
77 
78 
79 class SyncPlaywrightContextManager:
80     def __init__(self) -> None:
81         self._connection = run_driver()
82         self._playwright: SyncPlaywright
83 
84     def __enter__(self) -> SyncPlaywright:
85         g_self = greenlet.getcurrent()
86 
87         def callback_wrapper(playwright_impl: Playwright) -> None:
88             self._playwright = SyncPlaywright(playwright_impl)
89             g_self.switch()
90 
91         self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)
92         set_dispatcher_fiber(greenlet(lambda: self._connection.run_sync()))
93         dispatcher_fiber().switch()
94         playwright = self._playwright
95         playwright.stop = self.__exit__  # type: ignore
96         return playwright
97 
98     def start(self) -> SyncPlaywright:
99         return self.__enter__()
100 
101     def __exit__(self, *args: Any) -> None:
102         self._connection.stop_sync()
103 
104 
105 class AsyncPlaywrightContextManager:
106     def __init__(self) -> None:
107         self._connection: Connection
108 
109     async def __aenter__(self) -> AsyncPlaywright:
110         self._connection = await run_driver_async()
111         self._connection.run_async()
112         playwright = AsyncPlaywright(
113             await self._connection.wait_for_object_with_known_name("Playwright")
114         )
115         playwright.stop = self.__aexit__  # type: ignore
116         return playwright
117 
118     async def start(self) -> AsyncPlaywright:
119         return await self.__aenter__()
120 
121     async def __aexit__(self, *args: Any) -> None:
122         self._connection.stop_async()
123 
124 
125 if sys.platform == "win32":
126     # Use ProactorEventLoop in 3.7, which is default in 3.8
127     loop = asyncio.ProactorEventLoop()
128     asyncio.set_event_loop(loop)
129 
130 
131 def main() -> None:
132     driver_executable = compute_driver_executable()
133     my_env = os.environ.copy()
134     my_env["PW_CLI_TARGET_LANG"] = "python"
135     subprocess.run([str(driver_executable), *sys.argv[1:]], env=my_env)
136
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/playwright/main.py b/playwright/main.py
--- a/playwright/main.py
+++ b/playwright/main.py
@@ -43,11 +43,11 @@
 async def run_driver_async() -> Connection:
     driver_executable = compute_driver_executable()
 
-    # Sourced from: https://github.com/pytest-dev/pytest/blob/49827adcb9256c9c9c06a25729421dcc3c385edc/src/_pytest/faulthandler.py#L73-L80
+    # Sourced from: https://github.com/pytest-dev/pytest/blob/824e9cf67abcfc47df25a59bf32ebd8c25fbd02a/src/_pytest/faulthandler.py#L70-L77
     def _get_stderr_fileno() -> int:
         try:
             return sys.stderr.fileno()
-        except io.UnsupportedOperation:
+        except (AttributeError, io.UnsupportedOperation):
             # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
             # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
             # This is potentially dangerous, but the best we can do.

{"golden_diff": "diff --git a/playwright/main.py b/playwright/main.py\n--- a/playwright/main.py\n+++ b/playwright/main.py\n@@ -43,11 +43,11 @@\n async def run_driver_async() -> Connection:\n     driver_executable = compute_driver_executable()\n \n-    # Sourced from: https://github.com/pytest-dev/pytest/blob/49827adcb9256c9c9c06a25729421dcc3c385edc/src/_pytest/faulthandler.py#L73-L80\n+    # Sourced from: https://github.com/pytest-dev/pytest/blob/824e9cf67abcfc47df25a59bf32ebd8c25fbd02a/src/_pytest/faulthandler.py#L70-L77\n     def _get_stderr_fileno() -> int:\n         try:\n             return sys.stderr.fileno()\n-        except io.UnsupportedOperation:\n+        except (AttributeError, io.UnsupportedOperation):\n             # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.\n             # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors\n             # This is potentially dangerous, but the best we can do.\n", "issue": "[Fix] sys.stderr.fileno() throws an AttributeError exception\nWhen I tried to delivered the playwright tasks via `celery==4.4.7`, I got an `AttributeError` exception at line 50 in `main.py`. \r\n\r\n`except AttributeError:`\r\n\r\nI've checked the source from `pytest`, the original code is:\r\n\r\n ```\r\n     except (AttributeError, io.UnsupportedOperation):\r\n            # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.\r\n            # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors\r\n            # This is potentially dangerous, but the best we can do.\r\n            return sys.__stderr__.fileno()\r\n```\r\n\r\nIt perfectly works if I changed it to the original code, so I think we should be consistent with the original source code to enhance compatibility.\r\n\r\nI also checked the celery official docs. The reason why I got an `AttributeError` exception probably is that the log proxy of celery is not support the `stderr` attribute.\r\n\n[Fix] sys.stderr.fileno() throws an AttributeError exception\nWhen I tried to delivered the playwright tasks via `celery==4.4.7`, I got an `AttributeError` exception at line 50 in `main.py`. \r\n\r\n`except AttributeError:`\r\n\r\nI've checked the source from `pytest`, the original code is:\r\n\r\n ```\r\n     except (AttributeError, io.UnsupportedOperation):\r\n            # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.\r\n            # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors\r\n            # This is potentially dangerous, but the best we can do.\r\n            return sys.__stderr__.fileno()\r\n```\r\n\r\nIt perfectly works if I changed it to the original code, so I think we should be consistent with the original source code to enhance compatibility.\r\n\r\nI also checked the celery official docs. The reason why I got an `AttributeError` exception probably is that the log proxy of celery is not support the `stderr` attribute.\r\n\n", "before_files": [{"content": "# Copyright (c) Microsoft Corporation.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport asyncio\nimport io\nimport os\nimport subprocess\nimport sys\nfrom pathlib import Path\nfrom typing import Any\n\nfrom greenlet import greenlet\n\nfrom playwright.async_api import Playwright as AsyncPlaywright\nfrom playwright.connection import Connection\nfrom playwright.helper import Error\nfrom playwright.object_factory import create_remote_object\nfrom playwright.path_utils import get_file_dirname\nfrom playwright.playwright import Playwright\nfrom playwright.sync_api import Playwright as SyncPlaywright\nfrom playwright.sync_base import dispatcher_fiber, set_dispatcher_fiber\n\n\ndef compute_driver_executable() -> Path:\n    package_path = get_file_dirname()\n    platform = sys.platform\n    if platform == \"win32\":\n        return package_path / \"driver\" / \"playwright-cli.exe\"\n    return package_path / \"driver\" / \"playwright-cli\"\n\n\nasync def run_driver_async() -> Connection:\n    driver_executable = compute_driver_executable()\n\n    # Sourced from: https://github.com/pytest-dev/pytest/blob/49827adcb9256c9c9c06a25729421dcc3c385edc/src/_pytest/faulthandler.py#L73-L80\n    def _get_stderr_fileno() -> int:\n        try:\n            return sys.stderr.fileno()\n        except io.UnsupportedOperation:\n            # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.\n            # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors\n            # This is potentially dangerous, but the best we can do.\n            return sys.__stderr__.fileno()\n\n    proc = await asyncio.create_subprocess_exec(\n        str(driver_executable),\n        \"run-driver\",\n        stdin=asyncio.subprocess.PIPE,\n        stdout=asyncio.subprocess.PIPE,\n        stderr=_get_stderr_fileno(),\n        limit=32768,\n    )\n    assert proc.stdout\n    assert proc.stdin\n    connection = Connection(\n        proc.stdout, proc.stdin, create_remote_object, asyncio.get_event_loop()\n    )\n    return connection\n\n\ndef run_driver() -> Connection:\n    loop = asyncio.get_event_loop()\n    if loop.is_running():\n        raise Error(\"Can only run one Playwright at a time.\")\n    return loop.run_until_complete(run_driver_async())\n\n\nclass SyncPlaywrightContextManager:\n    def __init__(self) -> None:\n        self._connection = run_driver()\n        self._playwright: SyncPlaywright\n\n    def __enter__(self) -> SyncPlaywright:\n        g_self = greenlet.getcurrent()\n\n        def callback_wrapper(playwright_impl: Playwright) -> None:\n            self._playwright = SyncPlaywright(playwright_impl)\n            g_self.switch()\n\n        self._connection.call_on_object_with_known_name(\"Playwright\", callback_wrapper)\n        set_dispatcher_fiber(greenlet(lambda: self._connection.run_sync()))\n        dispatcher_fiber().switch()\n        playwright = self._playwright\n        playwright.stop = self.__exit__  # type: ignore\n        return playwright\n\n    def start(self) -> SyncPlaywright:\n        return self.__enter__()\n\n    def __exit__(self, *args: Any) -> None:\n        self._connection.stop_sync()\n\n\nclass AsyncPlaywrightContextManager:\n    def __init__(self) -> None:\n        self._connection: Connection\n\n    async def __aenter__(self) -> AsyncPlaywright:\n        self._connection = await run_driver_async()\n        self._connection.run_async()\n        playwright = AsyncPlaywright(\n            await self._connection.wait_for_object_with_known_name(\"Playwright\")\n        )\n        playwright.stop = self.__aexit__  # type: ignore\n        return playwright\n\n    async def start(self) -> AsyncPlaywright:\n        return await self.__aenter__()\n\n    async def __aexit__(self, *args: Any) -> None:\n        self._connection.stop_async()\n\n\nif sys.platform == \"win32\":\n    # Use ProactorEventLoop in 3.7, which is default in 3.8\n    loop = asyncio.ProactorEventLoop()\n    asyncio.set_event_loop(loop)\n\n\ndef main() -> None:\n    driver_executable = compute_driver_executable()\n    my_env = os.environ.copy()\n    my_env[\"PW_CLI_TARGET_LANG\"] = \"python\"\n    subprocess.run([str(driver_executable), *sys.argv[1:]], env=my_env)\n", "path": "playwright/main.py"}], "after_files": [{"content": "# Copyright (c) Microsoft Corporation.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport asyncio\nimport io\nimport os\nimport subprocess\nimport sys\nfrom pathlib import Path\nfrom typing import Any\n\nfrom greenlet import greenlet\n\nfrom playwright.async_api import Playwright as AsyncPlaywright\nfrom playwright.connection import Connection\nfrom playwright.helper import Error\nfrom playwright.object_factory import create_remote_object\nfrom playwright.path_utils import get_file_dirname\nfrom playwright.playwright import Playwright\nfrom playwright.sync_api import Playwright as SyncPlaywright\nfrom playwright.sync_base import dispatcher_fiber, set_dispatcher_fiber\n\n\ndef compute_driver_executable() -> Path:\n    package_path = get_file_dirname()\n    platform = sys.platform\n    if platform == \"win32\":\n        return package_path / \"driver\" / \"playwright-cli.exe\"\n    return package_path / \"driver\" / \"playwright-cli\"\n\n\nasync def run_driver_async() -> Connection:\n    driver_executable = compute_driver_executable()\n\n    # Sourced from: https://github.com/pytest-dev/pytest/blob/824e9cf67abcfc47df25a59bf32ebd8c25fbd02a/src/_pytest/faulthandler.py#L70-L77\n    def _get_stderr_fileno() -> int:\n        try:\n            return sys.stderr.fileno()\n        except (AttributeError, io.UnsupportedOperation):\n            # pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.\n            # https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors\n            # This is potentially dangerous, but the best we can do.\n            return sys.__stderr__.fileno()\n\n    proc = await asyncio.create_subprocess_exec(\n        str(driver_executable),\n        \"run-driver\",\n        stdin=asyncio.subprocess.PIPE,\n        stdout=asyncio.subprocess.PIPE,\n        stderr=_get_stderr_fileno(),\n        limit=32768,\n    )\n    assert proc.stdout\n    assert proc.stdin\n    connection = Connection(\n        proc.stdout, proc.stdin, create_remote_object, asyncio.get_event_loop()\n    )\n    return connection\n\n\ndef run_driver() -> Connection:\n    loop = asyncio.get_event_loop()\n    if loop.is_running():\n        raise Error(\"Can only run one Playwright at a time.\")\n    return loop.run_until_complete(run_driver_async())\n\n\nclass SyncPlaywrightContextManager:\n    def __init__(self) -> None:\n        self._connection = run_driver()\n        self._playwright: SyncPlaywright\n\n    def __enter__(self) -> SyncPlaywright:\n        g_self = greenlet.getcurrent()\n\n        def callback_wrapper(playwright_impl: Playwright) -> None:\n            self._playwright = SyncPlaywright(playwright_impl)\n            g_self.switch()\n\n        self._connection.call_on_object_with_known_name(\"Playwright\", callback_wrapper)\n        set_dispatcher_fiber(greenlet(lambda: self._connection.run_sync()))\n        dispatcher_fiber().switch()\n        playwright = self._playwright\n        playwright.stop = self.__exit__  # type: ignore\n        return playwright\n\n    def start(self) -> SyncPlaywright:\n        return self.__enter__()\n\n    def __exit__(self, *args: Any) -> None:\n        self._connection.stop_sync()\n\n\nclass AsyncPlaywrightContextManager:\n    def __init__(self) -> None:\n        self._connection: Connection\n\n    async def __aenter__(self) -> AsyncPlaywright:\n        self._connection = await run_driver_async()\n        self._connection.run_async()\n        playwright = AsyncPlaywright(\n            await self._connection.wait_for_object_with_known_name(\"Playwright\")\n        )\n        playwright.stop = self.__aexit__  # type: ignore\n        return playwright\n\n    async def start(self) -> AsyncPlaywright:\n        return await self.__aenter__()\n\n    async def __aexit__(self, *args: Any) -> None:\n        self._connection.stop_async()\n\n\nif sys.platform == \"win32\":\n    # Use ProactorEventLoop in 3.7, which is default in 3.8\n    loop = asyncio.ProactorEventLoop()\n    asyncio.set_event_loop(loop)\n\n\ndef main() -> None:\n    driver_executable = compute_driver_executable()\n    my_env = os.environ.copy()\n    my_env[\"PW_CLI_TARGET_LANG\"] = \"python\"\n    subprocess.run([str(driver_executable), *sys.argv[1:]], env=my_env)\n", "path": "playwright/main.py"}]}