from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from .onnx_pb2 import *

import sys

def load(obj):
    '''
    Loads a binary protobuf that stores onnx graph

    @params
    Takes a file-like object (has to implement fileno that returns a file descriptor)
    or a string containing a file name
    @return ONNX ModelProto object
    '''
    model = ModelProto()
    if isinstance(obj, str) or (sys.version_info[0] == 2 and
                                isinstance(obj, unicode_literals.unicode_or_str)):
        with open(obj, 'rb') as f:
            model.ParseFromString(f.read())
    else:
        model.ParseFromString(obj.read())
    return model

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from .onnx_pb2 import *

import sys

def load(obj):
    '''
    Loads a binary protobuf that stores onnx graph

    @params
    Takes a file-like object (has "read" function)
    or a string containing a file name
    @return ONNX ModelProto object
    '''
    model = ModelProto()
    if hasattr(obj, 'read') and callable(obj.read):
        model.ParseFromString(obj.read())
    else:
        with open(obj, 'rb') as f:
            model.ParseFromString(f.read())
    return model

Fix string/file-like object detection in onnx.load
Fix string/file-like object detection in onnx.load

  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  
  from .onnx_pb2 import *
  
  import sys
  
  def load(obj):
      '''
      Loads a binary protobuf that stores onnx graph
  
      @params
-     Takes a file-like object (has to implement fileno that returns a file descriptor)
+     Takes a file-like object (has "read" function)
      or a string containing a file name
      @return ONNX ModelProto object
      '''
      model = ModelProto()
-     if isinstance(obj, str) or (sys.version_info[0] == 2 and
-                                 isinstance(obj, unicode_literals.unicode_or_str)):
+     if hasattr(obj, 'read') and callable(obj.read):
+         model.ParseFromString(obj.read())
+     else:
          with open(obj, 'rb') as f:
              model.ParseFromString(f.read())
-     else:
-         model.ParseFromString(obj.read())
      return model
  
Fix string/file-like object detection in onnx.load
## Code Before:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from .onnx_pb2 import *

import sys

def load(obj):
    '''
    Loads a binary protobuf that stores onnx graph

    @params
    Takes a file-like object (has to implement fileno that returns a file descriptor)
    or a string containing a file name
    @return ONNX ModelProto object
    '''
    model = ModelProto()
    if isinstance(obj, str) or (sys.version_info[0] == 2 and
                                isinstance(obj, unicode_literals.unicode_or_str)):
        with open(obj, 'rb') as f:
            model.ParseFromString(f.read())
    else:
        model.ParseFromString(obj.read())
    return model

## Instruction:
Fix string/file-like object detection in onnx.load
## Code After:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from .onnx_pb2 import *

import sys

def load(obj):
    '''
    Loads a binary protobuf that stores onnx graph

    @params
    Takes a file-like object (has "read" function)
    or a string containing a file name
    @return ONNX ModelProto object
    '''
    model = ModelProto()
    if hasattr(obj, 'read') and callable(obj.read):
        model.ParseFromString(obj.read())
    else:
        with open(obj, 'rb') as f:
            model.ParseFromString(f.read())
    return model
