lib/python/pickle.rb in python-pickle-0.1.0 vs lib/python/pickle.rb in python-pickle-0.1.1

- old
+ new

@@ -26,11 +26,11 @@ } # The default protocol version to use. # # @api public - DEFAULT_PROTCOL = 4 + DEFAULT_PROTOCOL = 4 # The highest protocol version supported. # # @api public HIGHEST_PROTOCOL = 5 @@ -86,16 +86,26 @@ # # @param [Integer, nil] protocol # The explicit protocol version to use. If `nil` the protocol version will # be inferred by inspecting the first two bytes of the stream. # + # @param [Hash{Symbol => Object}] kwargs + # Additional keyword arguments. + # + # @option kwargs [Hash{Integer => Object}] :extensions + # A Hash of registered extension IDs and their Objects. + # + # @option kwargs [Hash{String => Hash{String => Class,Method}}] :constants + # An optional mapping of custom Python constant names to Ruby classes + # or methods. + # # @api public # - def self.load(data,**kwargs) + def self.load(data, protocol: nil, **kwargs) deserializer = Deserializer.new(**kwargs) - parse(data) do |instruction| + parse(data, protocol: protocol) do |instruction| status, object = deserializer.execute(instruction) if status == :halt return object end @@ -108,15 +118,31 @@ # Deserializes a Python Pickle file. # # @param [String] path # The path of the file. # + # @param [Hash{Symbol => Object}] kwargs + # Additional keyword arguments. + # + # @option kwargs [Hash{Integer => Object}] :extensions + # A Hash of registered extension IDs and their Objects. + # + # @option kwargs [Hash{String => Hash{String => Class,Method}}] :constants + # An optional mapping of custom Python constant names to Ruby classes + # or methods. + # # @return [Object] # The deserialized object. # def self.load_file(path,**kwargs) - load(File.open(path,'rb'),**kwargs) + result = nil + + File.open(path,'rb') do |file| + result = load(file,**kwargs) + end + + return result end # # Serializes the Ruby object into Python Pickle data. # @@ -127,15 +153,15 @@ # The option output to write the Pickle data to. # # @param [Integer] protocol # The desired Python Pickle protocol to use. # + # @note serializing is currently not supported. + # # @api public # def self.dump(object,output=nil, protocol: DEFAULT_PROTOCOL) - if (protocol < 0) || (protocol > HIGHEST_PROTOCOL) - raise(ArgumentError,"protocol must be between 0 or #{HIGHEST_PROTOCOL}, but was #{protocol.inspect}") - end + raise(NotImplementedError,"pickle serializing is currently not supported") end # # Infers the protocol version from the IO stream. #