Skip to content

Commit fa2cded

Browse files
authored
Merge pull request #5 from FugroRoames/julia-0.6-fixes
Various small fixes for julia-0.6 + extra tests
2 parents 9e5bee7 + 6da0f9d commit fa2cded

File tree

5 files changed

+106
-38
lines changed

5 files changed

+106
-38
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.5
2+
Compat 0.13.0

src/PlyIO.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ __precompile__()
22

33
module PlyIO
44

5+
using Compat
6+
57
# Types for the ply data model
68
export Ply, PlyElement, PlyComment, ArrayProperty, ListProperty
79
export plyname # Is there something in base we could overload for this?

src/io.jl

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ply_type_name(::Int64) = "int64"
4444
ply_type_name(::Float32) = "float32"
4545
ply_type_name(::Float64) = "float64"
4646

47-
typealias PlyNativeType Union{UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64,Float32,Float64}
47+
@compat const PlyNativeType = Union{UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64,Float32,Float64}
4848

4949
ply_type_name{T<:PlyNativeType}(A::AbstractArray{T}) = ply_type_name(T)
5050

@@ -56,50 +56,66 @@ const _host_is_little_endian = (ENDIAN_BOM == 0x04030201)
5656

5757

5858
function read_header(ply_file)
59-
@assert readline(ply_file) == "ply\n"
59+
firstline = Compat.readline(ply_file)
60+
if firstline != "ply"
61+
throw(ErrorException("Expected \"ply\" header, got \"$firstline\""))
62+
end
6063
element_name = ""
6164
element_numel = 0
6265
element_props = Vector{AbstractVector}()
6366
elements = PlyElement[]
6467
comments = PlyComment[]
6568
format = nothing
6669
while true
67-
line = strip(readline(ply_file))
68-
if line == "end_header"
70+
line = Compat.readline(ply_file)
71+
if line == "" && eof(ply_file)
72+
throw(ErrorException("Unexpected end of file reading ply header"))
73+
elseif line == "end_header"
6974
break
7075
elseif startswith(line, "comment")
7176
push!(comments, PlyComment(strip(line[8:end]), length(elements)+1))
72-
elseif startswith(line, "format")
73-
tok, format_type, format_version = split(line)
74-
@assert tok == "format"
75-
@assert format_version == "1.0"
76-
format = format_type == "ascii" ? Format_ascii :
77-
format_type == "binary_little_endian" ? Format_binary_little :
78-
format_type == "binary_big_endian" ? Format_binary_big :
79-
error("Unknown ply format $format_type")
80-
elseif startswith(line, "element")
81-
if !isempty(element_name)
82-
push!(elements, PlyElement(element_name, element_numel, element_props))
83-
element_props = Vector{AbstractVector}()
84-
end
85-
tok, element_name, element_numel = split(line)
86-
@assert tok == "element"
87-
element_numel = parse(Int,element_numel)
88-
elseif startswith(line, "property")
77+
else
8978
tokens = split(line)
90-
@assert tokens[1] == "property"
91-
if tokens[2] == "list"
92-
count_type_name, type_name, prop_name = tokens[3:end]
93-
count_type = ply_type(count_type_name)
94-
type_ = ply_type(type_name)
95-
push!(element_props, ListProperty(prop_name, ply_type(count_type_name), ply_type(type_name)))
79+
length(tokens) > 2 || throw(ErrorException("Bad ply header, line: \"$line\""))
80+
if tokens[1] == "format"
81+
length(tokens) == 3 || throw(ErrorException("Bad ply header, line: \"$line\""))
82+
_, format_type, format_version = tokens
83+
if format_version != "1.0"
84+
throw(ErrorException("Expected ply version 1.0, got $format_version"))
85+
end
86+
format = format_type == "ascii" ? Format_ascii :
87+
format_type == "binary_little_endian" ? Format_binary_little :
88+
format_type == "binary_big_endian" ? Format_binary_big :
89+
error("Unknown ply format $format_type")
90+
elseif tokens[1] == "element"
91+
if !isempty(element_name)
92+
push!(elements, PlyElement(element_name, element_numel, element_props))
93+
element_props = Vector{AbstractVector}()
94+
end
95+
length(tokens) == 3 || throw(ErrorException("Bad ply header, line: \"$line\""))
96+
_, element_name, element_numel = tokens
97+
element_numel = parse(Int,element_numel)
98+
elseif tokens[1] == "property"
99+
!isempty(element_name) || throw(ErrorException("Bad ply header: property before first element"))
100+
if tokens[2] == "list"
101+
length(tokens) == 5 || throw(ErrorException("Bad ply header, line: \"$line\""))
102+
count_type_name, type_name, prop_name = tokens[3:end]
103+
count_type = ply_type(count_type_name)
104+
type_ = ply_type(type_name)
105+
push!(element_props, ListProperty(prop_name, ply_type(count_type_name), ply_type(type_name)))
106+
else
107+
length(tokens) == 3 || throw(ErrorException("Bad ply header, line: \"$line\""))
108+
type_name, prop_name = tokens[2:end]
109+
push!(element_props, ArrayProperty(prop_name, ply_type(type_name)))
110+
end
96111
else
97-
type_name, prop_name = tokens[2:end]
98-
push!(element_props, ArrayProperty(prop_name, ply_type(type_name)))
112+
throw(ErrorException("Bad ply header, line: \"$line\""))
99113
end
100114
end
101115
end
102-
push!(elements, PlyElement(element_name, element_numel, element_props))
116+
if !isempty(element_name)
117+
push!(elements, PlyElement(element_name, element_numel, element_props))
118+
end
103119
elements, format, comments
104120
end
105121

src/types.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Return the name that `data` is associated with when serialized in a ply file
99
function plyname
1010
end
1111

12-
typealias PropNameList Union{AbstractVector,Tuple}
12+
@compat const PropNameList = Union{AbstractVector,Tuple}
1313

1414
#--------------------------------------------------
1515
"""
@@ -41,7 +41,7 @@ Base.summary(prop::ArrayProperty) = "$(length(prop))-element $(typeof(prop)) \"$
4141
Base.size(prop::ArrayProperty) = size(prop.data)
4242
Base.getindex(prop::ArrayProperty, i::Int) = prop.data[i]
4343
Base.setindex!(prop::ArrayProperty, v, i::Int) = prop.data[i] = v
44-
Base.linearindexing(prop::ArrayProperty) = Base.LinearFast()
44+
@compat Base.IndexStyle(::Type{<:ArrayProperty}) = IndexLinear()
4545

4646
# List methods
4747
Base.resize!(prop::ArrayProperty, len) = resize!(prop.data, len)
@@ -80,7 +80,7 @@ Base.summary(prop::ListProperty) = "$(length(prop))-element $(typeof(prop)) \"$(
8080
Base.length(prop::ListProperty) = length(prop.start_inds)-1
8181
Base.size(prop::ListProperty) = (length(prop),)
8282
Base.getindex(prop::ListProperty, i::Int) = prop.data[prop.start_inds[i]:prop.start_inds[i+1]-1]
83-
Base.linearindexing(prop::ListProperty) = Base.LinearFast()
83+
@compat Base.IndexStyle(::Type{<:ListProperty}) = IndexLinear()
8484
# TODO: Do we need Base.setindex!() ? Hard to provide with above formulation...
8585

8686
# List methods
@@ -198,7 +198,7 @@ Ply() = Ply(Vector{PlyElement}(), Vector{String}())
198198
function Base.show(io::IO, ply::Ply)
199199
buf = IOBuffer()
200200
write_header(ply, buf, true)
201-
headerstr = takebuf_string(buf)
201+
headerstr = String(take!(buf))
202202
headerstr = replace(strip(headerstr), "\n", "\n ")
203203
print(io, "$Ply with header:\n $headerstr")
204204
end

test/runtests.jl

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using PlyIO
22
using StaticArrays
33
using Base.Test
4+
using Compat
45

56
@testset "PlyIO" begin
67

@@ -22,7 +23,7 @@ using Base.Test
2223

2324
buf = IOBuffer()
2425
save_ply(ply, buf, ascii=true)
25-
str = takebuf_string(buf)
26+
str = String(take!(buf))
2627
open("simple_test_tmp.ply", "w") do fid
2728
write(fid, str)
2829
end
@@ -58,7 +59,7 @@ end
5859

5960
buf = IOBuffer()
6061
save_ply(ply, buf, ascii=true)
61-
str = takebuf_string(buf)
62+
str = String(take!(buf))
6263
open("empty_test_tmp.ply", "w") do fid
6364
write(fid, str)
6465
end
@@ -114,7 +115,7 @@ end
114115
UInt8, UInt16, UInt32, UInt64,
115116
Float32, Float64]
116117
ply = Ply()
117-
arrayprop = zeros(proptype, 1) + 42
118+
arrayprop = fill(proptype(42), 1)
118119
listprop = ListProperty("listprop", proptype<:Integer ? proptype : Int32, proptype)
119120
push!(listprop, collect(proptype, 1:10))
120121
push!(ply, PlyElement("test", ArrayProperty("arrayprop", arrayprop), listprop))
@@ -141,7 +142,7 @@ end
141142
))
142143
buf = IOBuffer()
143144
save_ply(ply, buf, ascii=true)
144-
str = takebuf_string(buf)
145+
str = String(take!(buf))
145146
open("SVector_properties_test_tmp.ply", "w") do fid
146147
write(fid, str)
147148
end
@@ -164,4 +165,52 @@ end
164165
end
165166

166167

168+
@testset "Malformed ply headers" begin
169+
@test_throws ErrorException load_ply(IOBuffer("asdf"))
170+
171+
@test_throws ErrorException load_ply(IOBuffer("ply"))
172+
173+
@test_throws ErrorException load_ply(IOBuffer("""
174+
ply
175+
format ascii 2.0
176+
"""))
177+
178+
@test_throws ErrorException load_ply(IOBuffer("""
179+
ply
180+
format 1.0
181+
end_header"""))
182+
183+
@test_throws ErrorException load_ply(IOBuffer("""
184+
ply
185+
format ascii 1.0
186+
asdf
187+
end_header"""))
188+
189+
@test_throws ErrorException load_ply(IOBuffer("""
190+
ply
191+
format ascii 1.0
192+
element el
193+
end_header"""))
194+
195+
@test_throws ErrorException load_ply(IOBuffer("""
196+
ply
197+
format ascii 1.0
198+
property float x
199+
end_header"""))
200+
201+
@test_throws ErrorException load_ply(IOBuffer("""
202+
ply
203+
format ascii 1.0
204+
element el 0
205+
property
206+
end_header"""))
207+
208+
@test_throws ErrorException load_ply(IOBuffer("""
209+
ply
210+
format ascii 1.0
211+
element el 0
212+
property list
213+
end_header"""))
214+
end
215+
167216
end # @testset PlyIO

0 commit comments

Comments
 (0)