def test_cython_api(tmpdir): """ Basic test for the Cython API. """ # Fail early if cython is not found import cython # noqa with tmpdir.as_cwd(): # Set up temporary workspace pyx_file = 'pyarrow_cython_example.pyx' shutil.copyfile(os.path.join(here, pyx_file), os.path.join(str(tmpdir), pyx_file)) # Create setup.py file setup_code = setup_template.format(pyx_file=pyx_file, compiler_opts=compiler_opts, test_ld_path=test_ld_path) with open('setup.py', 'w') as f: f.write(setup_code) # ARROW-2263: Make environment with this pyarrow/ package first on the # PYTHONPATH, for local dev environments subprocess_env = test_util.get_modified_env_with_pythonpath() # Compile extension module subprocess.check_call( [sys.executable, 'setup.py', 'build_ext', '--inplace'], env=subprocess_env) # Check basic functionality orig_path = sys.path[:] sys.path.insert(0, str(tmpdir)) try: mod = __import__('pyarrow_cython_example') check_cython_example_module(mod) finally: sys.path = orig_path # Check the extension module is loadable from a subprocess without # pyarrow imported first. code = """if 1: import sys mod = __import__({mod_name!r}) arr = mod.make_null_array(5) assert mod.get_array_length(arr) == 5 assert arr.null_count == 5 """.format(mod_name='pyarrow_cython_example') if sys.platform == 'win32': delim, var = ';', 'PATH' else: delim, var = ':', 'LD_LIBRARY_PATH' subprocess_env[var] = delim.join(pa.get_library_dirs() + [subprocess_env.get(var, '')]) subprocess.check_call([sys.executable, '-c', code], stdout=subprocess.PIPE, env=subprocess_env)
def test_deserialize_buffer_in_different_process(): import tempfile f = tempfile.NamedTemporaryFile(delete=False) b = pa.serialize(pa.py_buffer(b'hello')).to_buffer() f.write(b.to_pybytes()) f.close() subprocess_env = test_util.get_modified_env_with_pythonpath() dir_path = os.path.dirname(os.path.realpath(__file__)) python_file = os.path.join(dir_path, 'deserialize_buffer.py') subprocess.check_call([sys.executable, python_file, f.name], env=subprocess_env)
def test_cython_api(tmpdir): """ Basic test for the Cython API. """ pytest.importorskip('Cython') ld_path_default = os.path.join(os.environ['ARROW_HOME'], 'lib') test_ld_path = os.environ.get('PYARROW_TEST_LD_PATH', ld_path_default) with tmpdir.as_cwd(): # Set up temporary workspace pyx_file = 'pyarrow_cython_example.pyx' shutil.copyfile(os.path.join(here, pyx_file), os.path.join(str(tmpdir), pyx_file)) # Create setup.py file if os.name == 'posix': compiler_opts = ['-std=c++11'] else: compiler_opts = [] setup_code = setup_template.format(pyx_file=pyx_file, compiler_opts=compiler_opts, test_ld_path=test_ld_path) with open('setup.py', 'w') as f: f.write(setup_code) # ARROW-2263: Make environment with this pyarrow/ package first on the # PYTHONPATH, for local dev environments subprocess_env = test_util.get_modified_env_with_pythonpath() # Compile extension module subprocess.check_call( [sys.executable, 'setup.py', 'build_ext', '--inplace'], env=subprocess_env) # Check basic functionality orig_path = sys.path[:] sys.path.insert(0, str(tmpdir)) try: mod = __import__('pyarrow_cython_example') arr = pa.array([1, 2, 3]) assert mod.get_array_length(arr) == 3 with pytest.raises(TypeError, match="not an array"): mod.get_array_length(None) finally: sys.path = orig_path
def test_cython_api(tmpdir): """ Basic test for the Cython API. """ pytest.importorskip('Cython') ld_path_default = os.path.join(os.environ['ARROW_HOME'], 'lib') test_ld_path = os.environ.get('PYARROW_TEST_LD_PATH', ld_path_default) with tmpdir.as_cwd(): # Set up temporary workspace pyx_file = 'pyarrow_cython_example.pyx' shutil.copyfile(os.path.join(here, pyx_file), os.path.join(str(tmpdir), pyx_file)) # Create setup.py file if os.name == 'posix': compiler_opts = ['-std=c++11'] else: compiler_opts = [] setup_code = setup_template.format(pyx_file=pyx_file, compiler_opts=compiler_opts, test_ld_path=test_ld_path) with open('setup.py', 'w') as f: f.write(setup_code) # ARROW-2263: Make environment with this pyarrow/ package first on the # PYTHONPATH, for local dev environments subprocess_env = test_util.get_modified_env_with_pythonpath() # Compile extension module subprocess.check_call([sys.executable, 'setup.py', 'build_ext', '--inplace'], env=subprocess_env) # Check basic functionality orig_path = sys.path[:] sys.path.insert(0, str(tmpdir)) try: mod = __import__('pyarrow_cython_example') arr = pa.array([1, 2, 3]) assert mod.get_array_length(arr) == 3 with pytest.raises(TypeError, match="not an array"): mod.get_array_length(None) finally: sys.path = orig_path
def test_deserialize_components_in_different_process(): arr = pa.array([1, 2, 5, 6], type=pa.int8()) ser = pa.serialize(arr) data = pickle.dumps(ser.to_components(), protocol=-1) code = """if 1: import pickle import pyarrow as pa data = {0!r} components = pickle.loads(data) arr = pa.deserialize_components(components) assert arr.to_pylist() == [1, 2, 5, 6], arr """.format(data) subprocess_env = test_util.get_modified_env_with_pythonpath() print("** sys.path =", sys.path) print("** setting PYTHONPATH to:", subprocess_env['PYTHONPATH']) subprocess.check_call(["python", "-c", code], env=subprocess_env)
def test_visit_strings(tmpdir): with tmpdir.as_cwd(): # Set up temporary workspace pyx_file = 'bound_function_visit_strings.pyx' shutil.copyfile(os.path.join(here, pyx_file), os.path.join(str(tmpdir), pyx_file)) # Create setup.py file setup_code = setup_template.format(pyx_file=pyx_file, compiler_opts=compiler_opts, test_ld_path=test_ld_path) with open('setup.py', 'w') as f: f.write(setup_code) subprocess_env = test_util.get_modified_env_with_pythonpath() # Compile extension module subprocess.check_call( [sys.executable, 'setup.py', 'build_ext', '--inplace'], env=subprocess_env) sys.path.insert(0, str(tmpdir)) mod = __import__('bound_function_visit_strings') strings = ['a', 'b', 'c'] visited = [] mod._visit_strings(strings, visited.append) assert visited == strings with pytest.raises(ValueError, match="wtf"): def raise_on_b(s): if s == 'b': raise ValueError('wtf') mod._visit_strings(strings, raise_on_b)