<commit_before>using System;
using System.IO;
using System.Runtime.InteropServices;

using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
    /// <summary>
    /// Test Import unittests and regressions
    /// </summary>
    /// <remarks>
    /// Keeping in old-style SetUp/TearDown due to required SetUp.
    /// The required directory structure was added to .\pythonnet\src\embed_tests\fixtures\ directory:
    /// + PyImportTest/
    /// | - __init__.py
    /// | + test/
    /// | | - __init__.py
    /// | | - one.py
    /// </remarks>
    public class PyImportTest
    {
        [OneTimeSetUp]
        public void SetUp()
        {
            PythonEngine.Initialize();

            /* Append the tests directory to sys.path
             * using reflection to circumvent the private
             * modifiers placed on most Runtime methods. */
            string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "fixtures");
            TestContext.Out.WriteLine(testPath);

            using var str = Runtime.Runtime.PyString_FromString(testPath);
            Assert.IsFalse(str.IsNull());
            BorrowedReference path = Runtime.Runtime.PySys_GetObject("path");
            Assert.IsFalse(path.IsNull);
            Runtime.Runtime.PyList_Append(path, str.Borrow());
        }

        [OneTimeTearDown]
        public void Dispose()
        {
            PythonEngine.Shutdown();
        }

        /// <summary>
        /// Test subdirectory import
        /// </summary>
        [Test]
        public void TestDottedName()
        {
            var module = PyModule.Import("PyImportTest.test.one");
            Assert.IsNotNull(module);
        }

        /// <summary>
        /// Tests that sys.args is set. If it wasn't exception would be raised.
        /// </summary>
        [Test]
        public void TestSysArgsImportException()
        {
            var module = PyModule.Import("PyImportTest.sysargv");
            Assert.IsNotNull(module);
        }

        /// <summary>
        /// Test Global Variable casting. GH#420
        /// </summary>
        [Test]
        public void TestCastGlobalVar()
        {
            dynamic foo = Py.Import("PyImportTest.cast_global_var");
            Assert.AreEqual("1", foo.FOO.ToString());
            Assert.AreEqual("1", foo.test_foo().ToString());

            foo.FOO = 2;
            Assert.AreEqual("2", foo.FOO.ToString());
            Assert.AreEqual("2", foo.test_foo().ToString());
        }

        [Test]
        public void BadAssembly()
        {
            string path;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                path = @"C:\Windows\System32\kernel32.dll";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                path = "/usr/lib/libc.dylib";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                path = "/usr/lib/locale/locale-archive";
            }
            else
            {
                Assert.Pass("TODO: add bad assembly location for other platforms");
                return;
            }

            string code = $@"
import clr
clr.AddReference('{path}')
";

            Assert.Throws<BadImageFormatException>(() => PythonEngine.Exec(code));
        }
    }
}

// regression for https://github.com/pythonnet/pythonnet/issues/1601
// initialize fails if a class derived from IEnumerable is in global namespace
public class PublicEnumerator : System.Collections.IEnumerable
{
    public System.Collections.IEnumerator GetEnumerator()
    {
        return null;
    }
}

<commit_msg>fixed MacOS bad assembly test by using PythonDLL (which is never a .NET assembly)<commit_after>using System;
using System.IO;
using System.Runtime.InteropServices;

using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
    /// <summary>
    /// Test Import unittests and regressions
    /// </summary>
    /// <remarks>
    /// Keeping in old-style SetUp/TearDown due to required SetUp.
    /// The required directory structure was added to .\pythonnet\src\embed_tests\fixtures\ directory:
    /// + PyImportTest/
    /// | - __init__.py
    /// | + test/
    /// | | - __init__.py
    /// | | - one.py
    /// </remarks>
    public class PyImportTest
    {
        [OneTimeSetUp]
        public void SetUp()
        {
            PythonEngine.Initialize();

            /* Append the tests directory to sys.path
             * using reflection to circumvent the private
             * modifiers placed on most Runtime methods. */
            string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "fixtures");
            TestContext.Out.WriteLine(testPath);

            using var str = Runtime.Runtime.PyString_FromString(testPath);
            Assert.IsFalse(str.IsNull());
            BorrowedReference path = Runtime.Runtime.PySys_GetObject("path");
            Assert.IsFalse(path.IsNull);
            Runtime.Runtime.PyList_Append(path, str.Borrow());
        }

        [OneTimeTearDown]
        public void Dispose()
        {
            PythonEngine.Shutdown();
        }

        /// <summary>
        /// Test subdirectory import
        /// </summary>
        [Test]
        public void TestDottedName()
        {
            var module = PyModule.Import("PyImportTest.test.one");
            Assert.IsNotNull(module);
        }

        /// <summary>
        /// Tests that sys.args is set. If it wasn't exception would be raised.
        /// </summary>
        [Test]
        public void TestSysArgsImportException()
        {
            var module = PyModule.Import("PyImportTest.sysargv");
            Assert.IsNotNull(module);
        }

        /// <summary>
        /// Test Global Variable casting. GH#420
        /// </summary>
        [Test]
        public void TestCastGlobalVar()
        {
            dynamic foo = Py.Import("PyImportTest.cast_global_var");
            Assert.AreEqual("1", foo.FOO.ToString());
            Assert.AreEqual("1", foo.test_foo().ToString());

            foo.FOO = 2;
            Assert.AreEqual("2", foo.FOO.ToString());
            Assert.AreEqual("2", foo.test_foo().ToString());
        }

        [Test]
        public void BadAssembly()
        {
            string path = Runtime.Runtime.PythonDLL;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                path = @"C:\Windows\System32\kernel32.dll";
            }

            Assert.IsTrue(File.Exists(path), $"Test DLL {path} does not exist!");

            string code = $@"
import clr
clr.AddReference('{path}')
";

            Assert.Throws<BadImageFormatException>(() => PythonEngine.Exec(code));
        }
    }
}

// regression for https://github.com/pythonnet/pythonnet/issues/1601
// initialize fails if a class derived from IEnumerable is in global namespace
public class PublicEnumerator : System.Collections.IEnumerable
{
    public System.Collections.IEnumerator GetEnumerator()
    {
        return null;
    }
}


{'fraction_non_alphanumeric': 0.11895754518705338, 'fraction_numerical': 0.004483676614824156, 'mean_word_length': 1.8202291584354011, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6483069', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1896, 'n_words': 421}