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

feat: Use better error messages when opening files #20307

Merged
merged 2 commits into from
Dec 16, 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
3 changes: 2 additions & 1 deletion crates/polars-io/src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::Path;
use polars_core::prelude::*;
use polars_core::series::IsSorted;
use polars_core::POOL;
use polars_utils::create_file;
use rayon::prelude::*;

use crate::parquet::write::ParquetWriteOptions;
Expand Down Expand Up @@ -110,7 +111,7 @@ where
};

let write_part = |df: DataFrame, path: &Path| {
let f = std::fs::File::create(path)?;
let f = create_file(path)?;
file_write_options.write_df_to_file(df, f)?;
PolarsResult::Ok(())
};
Expand Down
11 changes: 6 additions & 5 deletions crates/polars-io/src/utils/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::Write;
use std::path::Path;

use polars_core::config;
use polars_error::{feature_gated, PolarsError, PolarsResult};
use polars_error::{feature_gated, PolarsResult};
use polars_utils::create_file;
use polars_utils::mmap::ensure_not_mapped;

use crate::cloud::CloudOptions;
Expand All @@ -24,8 +26,7 @@ pub fn try_get_writeable(
}

if path.starts_with("file://") {
std::fs::File::create(&path[const { "file://".len() }..])
.map_err(PolarsError::from)?;
create_file(Path::new(&path[const { "file://".len() }..]))?;
}

let writer = crate::pl_async::get_runtime()
Expand All @@ -45,7 +46,7 @@ pub fn try_get_writeable(
)
}

std::fs::File::create(&path).map_err(PolarsError::from)?;
create_file(&path)?;
let path = std::fs::canonicalize(&path)?;

ensure_not_mapped(&path.metadata()?)?;
Expand All @@ -69,7 +70,7 @@ pub fn try_get_writeable(
})
} else {
let path = resolve_homedir(&path);
std::fs::File::create(&path).map_err(PolarsError::from)?;
create_file(&path)?;

if verbose {
eprintln!(
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-mem-engine/src/executors/scan/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use polars_io::cloud::CloudOptions;
use polars_io::path_utils::is_cloud_url;
use polars_io::predicates::apply_predicate;
use polars_utils::mmap::MemSlice;
use polars_utils::open_file;
use rayon::prelude::*;

use super::*;
Expand Down Expand Up @@ -77,7 +78,7 @@ impl IpcExec {
let memslice = match source {
ScanSourceRef::Path(path) => {
let file = match idx_to_cached_file(index) {
None => std::fs::File::open(path)?,
None => open_file(path)?,
Some(f) => f?,
};

Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/batched_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use polars::io::csv::read::OwnedBatchedCsvReader;
use polars::io::mmap::MmapBytesReader;
use polars::io::RowIndex;
use polars::prelude::*;
use polars_utils::open_file;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedStr;

Expand Down Expand Up @@ -91,7 +92,7 @@ impl PyBatchedCsv {
.collect::<Vec<_>>()
});

let file = std::fs::File::open(path).map_err(PyPolarsErr::from)?;
let file = open_file(&path).map_err(PyPolarsErr::from)?;
let reader = Box::new(file) as Box<dyn MmapBytesReader>;
let reader = CsvReadOptions::default()
.with_infer_schema_length(infer_schema_length)
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::sync::Arc;
use polars::io::mmap::MmapBytesReader;
use polars_error::polars_err;
use polars_io::cloud::CloudOptions;
use polars_utils::create_file;
use polars_utils::mmap::MemSlice;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
Expand Down Expand Up @@ -350,7 +351,7 @@ fn get_either_buffer_or_path(
if let Ok(s) = py_f.extract::<Cow<str>>() {
let file_path = resolve_homedir(&&*s);
let f = if write {
File::create(&file_path)?
create_file(&file_path).map_err(PyPolarsErr::from)?
} else {
polars_utils::open_file(&file_path).map_err(PyPolarsErr::from)?
};
Expand Down
11 changes: 10 additions & 1 deletion py-polars/tests/unit/io/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import pytest

import polars as pl
from polars.io._utils import looks_like_url, parse_columns_arg, parse_row_index_args

if TYPE_CHECKING:
Expand Down Expand Up @@ -60,3 +61,11 @@ def test_parse_row_index_args() -> None:
)
def test_looks_like_url(url: str, result: bool) -> None:
assert looks_like_url(url) == result


@pytest.mark.parametrize(
"scan", [pl.scan_csv, pl.scan_parquet, pl.scan_ndjson, pl.scan_ipc]
)
def test_filename_in_err(scan: Any) -> None:
with pytest.raises(FileNotFoundError, match=r".*does not exist"):
scan("does not exist").collect()
Loading