Skip to content
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
8 changes: 8 additions & 0 deletions include/cdfpp/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ struct Attribute
Attribute& operator=(const Attribute&) = default;
Attribute(const std::string& name, attr_data_t&& data) : name { name }
{
if (name.empty())
{
throw std::invalid_argument { "Attribute name cannot be empty" };
}
this->data = std::move(data);
}

Expand Down Expand Up @@ -177,6 +181,10 @@ struct VariableAttribute
VariableAttribute& operator=(const VariableAttribute&) = default;
VariableAttribute(const std::string& name, attr_data_t&& data) : name { name }
{
if (name.empty())
{
throw std::invalid_argument { "Attribute name cannot be empty" };
}
this->data = std::move(data);
}

Expand Down
2 changes: 2 additions & 0 deletions include/cdfpp/cdf-file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
template <class stream_t>
inline stream_t& operator<<(stream_t& os, const cdf_map<std::string, cdf::Variable>& variables)
{
std::for_each(std::cbegin(variables), std::cend(variables),
[&os](const auto& item) { item.second.__repr__(os, indent_t {}, false); });
return os;
}

Expand Down
8 changes: 8 additions & 0 deletions include/cdfpp/variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ struct Variable
, p_is_nrv { is_nrv }
, p_compression { compression_type }
{
if (name.empty())
{
throw std::invalid_argument { "Variable name cannot be empty" };
}
if (this->majority() == cdf_majority::column)
{
majority::swap(_data(), p_shape);
Expand All @@ -120,6 +124,10 @@ struct Variable
, p_is_nrv { is_nrv }
, p_compression { compression_type }
{
if (name.empty())
{
throw std::invalid_argument { "Variable name cannot be empty" };
}
}

inline bool operator==(const Variable& other) const
Expand Down
14 changes: 14 additions & 0 deletions tests/python_variable_set_values/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ def test_filter_cdf_with_regex(self):
self.assertIn("global_attr2", self.cdf.attributes)
self.assertIn("global_attr3", self.cdf.attributes)

class PycdfEmptyNamesAreNotAllowed(unittest.TestCase):
def test_variable_name_cannot_be_empty(self):
cdf = pycdfpp.CDF()
with self.assertRaises(ValueError):
cdf.add_variable("", values=np.arange(10))

def test_attribute_name_cannot_be_empty(self):
cdf = pycdfpp.CDF()
cdf.add_variable("var1", values=np.arange(10))
with self.assertRaises(ValueError):
cdf["var1"].add_attribute("", "value")
with self.assertRaises(ValueError):
cdf.add_attribute("", ["value"])


if __name__ == '__main__':
unittest.main()
Loading