



import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch





class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     @require_torch
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

           lowerCamelCase								=		"\nmname = \"hf-internal-testing/tiny-random-bert\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task=\"fill-mask\", model=mname)\nprint(\"success\")\n        "

           lowerCamelCase								=		"\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError(\"Offline mode is enabled, we shouldn't access internet\")\nsocket.socket = offline_socket\n        "

           # Force fetching the files so that we can use the cache
           lowerCamelCase								=		"hf-internal-testing/tiny-random-bert"
           BertConfig.from_pretrained(__a    )
           BertModel.from_pretrained(__a    )
           BertTokenizer.from_pretrained(__a    )
           pipeline(task="fill-mask"				,			model=__a    )

           # baseline - just load from_pretrained with normal network
           lowerCamelCase								=		[sys.executable, "-c", "\n".join([load, run, mock]    )]

           # should succeed
           lowerCamelCase								=		self.get_env()
           # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
           lowerCamelCase								=		"1"
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			0				,			result.stderr    )
           self.assertIn("success"				,			result.stdout.decode()    )







     @require_torch
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

           lowerCamelCase								=		"\nmname = \"hf-internal-testing/tiny-random-bert\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task=\"fill-mask\", model=mname)\nprint(\"success\")\n        "

           lowerCamelCase								=		"\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Faking flaky internet\")\nsocket.socket = offline_socket\n        "

           # Force fetching the files so that we can use the cache
           lowerCamelCase								=		"hf-internal-testing/tiny-random-bert"
           BertConfig.from_pretrained(__a    )
           BertModel.from_pretrained(__a    )
           BertTokenizer.from_pretrained(__a    )
           pipeline(task="fill-mask"				,			model=__a    )

           # baseline - just load from_pretrained with normal network
           lowerCamelCase								=		[sys.executable, "-c", "\n".join([load, run, mock]    )]

           # should succeed
           lowerCamelCase								=		self.get_env()
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			0				,			result.stderr    )
           self.assertIn("success"				,			result.stdout.decode()    )







     @require_torch
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

           lowerCamelCase								=		"\nmname = \"hf-internal-testing/tiny-random-bert-sharded\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint(\"success\")\n        "

           lowerCamelCase								=		"\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "

           # baseline - just load from_pretrained with normal network
           lowerCamelCase								=		[sys.executable, "-c", "\n".join([load, run]    )]

           # should succeed
           lowerCamelCase								=		self.get_env()
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			0				,			result.stderr    )
           self.assertIn("success"				,			result.stdout.decode()    )

           # next emulate no network
           lowerCamelCase								=		[sys.executable, "-c", "\n".join([load, mock, run]    )]

           # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
           # env["TRANSFORMERS_OFFLINE"] = "0"
           # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
           # self.assertEqual(result.returncode, 1, result.stderr)

           # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
           lowerCamelCase								=		"1"
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			0				,			result.stderr    )
           self.assertIn("success"				,			result.stdout.decode()    )







     @require_torch
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"\nfrom transformers import pipeline\n        "
           lowerCamelCase								=		"\nmname = \"hf-internal-testing/tiny-random-bert\"\npipe = pipeline(model=mname)\n        "

           lowerCamelCase								=		"\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "
           lowerCamelCase								=		self.get_env()
           lowerCamelCase								=		"1"
           lowerCamelCase								=		[sys.executable, "-c", "\n".join([load, mock, run]    )]
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			1				,			result.stderr    )
           self.assertIn(
               "You cannot infer task automatically within `pipeline` when using offline mode"				,			result.stderr.decode().replace("\n"				,			""    )				,			)







     @require_torch
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"\nfrom transformers import AutoModel\n        "
           lowerCamelCase								=		"\nmname = \"hf-internal-testing/test_dynamic_model\"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint(\"success\")\n        "

           # baseline - just load from_pretrained with normal network
           lowerCamelCase								=		[sys.executable, "-c", "\n".join([load, run]    )]

           # should succeed
           lowerCamelCase								=		self.get_env()
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			0				,			result.stderr    )
           self.assertIn("success"				,			result.stdout.decode()    )

           # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
           lowerCamelCase								=		"1"
           lowerCamelCase								=		subprocess.run(__a				,			env=__a				,			check=__a				,			capture_output=__a    )
           self.assertEqual(result.returncode				,			0				,			result.stderr    )
           self.assertIn("success"				,			result.stdout.decode()    )




import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate

def     __lowercase(	UpperCAmelCase__ ):


      """simple docstring"""



      return x + 2






class    lowerCamelCase__  (						unittest.TestCase):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"x = 3"
           lowerCamelCase								=		{}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           assert result == 3
           self.assertDictEqual(__a				,			{"x": 3}    )

           lowerCamelCase								=		"x = y"
           lowerCamelCase								=		{"y": 5}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           # evaluate returns the value of the last assignment.
           assert result == 5
           self.assertDictEqual(__a				,			{"x": 5, "y": 5}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"y = add_two(x)"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{"add_two": add_two}				,			state=__a    )
           assert result == 5
           self.assertDictEqual(__a				,			{"x": 3, "y": 5}    )

           # Won't work without the tool
           with CaptureStdout() as out:
                 lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           assert result is None
           assert "tried to execute add_two" in out.out







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"x = 3"
           lowerCamelCase								=		{}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           assert result == 3
           self.assertDictEqual(__a				,			{"x": 3}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"test_dict = {'x': x, 'y': add_two(x)}"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{"add_two": add_two}				,			state=__a    )
           self.assertDictEqual(__a				,			{"x": 3, "y": 5}    )
           self.assertDictEqual(__a				,			{"x": 3, "test_dict": {"x": 3, "y": 5}}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"x = 3\ny = 5"
           lowerCamelCase								=		{}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           # evaluate returns the value of the last assignment.
           assert result == 5
           self.assertDictEqual(__a				,			{"x": 3, "y": 5}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"text = f'This is x: {x}.'"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           # evaluate returns the value of the last assignment.
           assert result == "This is x: 3."
           self.assertDictEqual(__a				,			{"x": 3, "text": "This is x: 3."}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"if x <= 3:\n    y = 2\nelse:\n    y = 5"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           # evaluate returns the value of the last assignment.
           assert result == 2
           self.assertDictEqual(__a				,			{"x": 3, "y": 2}    )

           lowerCamelCase								=		{"x": 8}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           # evaluate returns the value of the last assignment.
           assert result == 5
           self.assertDictEqual(__a				,			{"x": 8, "y": 5}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"test_list = [x, add_two(x)]"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{"add_two": add_two}				,			state=__a    )
           self.assertListEqual(__a				,			[3, 5]    )
           self.assertDictEqual(__a				,			{"x": 3, "test_list": [3, 5]}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"y = x"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{}				,			state=__a    )
           assert result == 3
           self.assertDictEqual(__a				,			{"x": 3, "y": 3}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"test_list = [x, add_two(x)]\ntest_list[1]"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{"add_two": add_two}				,			state=__a    )
           assert result == 5
           self.assertDictEqual(__a				,			{"x": 3, "test_list": [3, 5]}    )

           lowerCamelCase								=		"test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
           lowerCamelCase								=		{"x": 3}
           lowerCamelCase								=		evaluate(__a				,			{"add_two": add_two}				,			state=__a    )
           assert result == 5
           self.assertDictEqual(__a				,			{"x": 3, "test_dict": {"x": 3, "y": 5}}    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"x = 0\nfor i in range(3):\n    x = i"
           lowerCamelCase								=		{}
           lowerCamelCase								=		evaluate(__a				,			{"range": range}				,			state=__a    )
           assert result == 2
           self.assertDictEqual(__a				,			{"x": 2, "i": 2}    )