Repository: pydicom/pydicom

Issue:
Write failure with implicit -> explicit VR
```python
>>> from pydicom import dcmread
>>> from pydicom.filebase import DicomBytesIO
>>> from pydicom.filewriter import write_dataset
>>> ds = dcmread('dicom_files/RTImageStorage.dcm')
>>> fp = DicomBytesIO()
>>> fp.is_little_endian = True
>>> fp.is_implicit_VR = False
>>> write_dataset(fp, ds)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../pydicom/pydicom/filewriter.py", line 473, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/.../pydicom/pydicom/tag.py", line 37, in tag_in_exception
    raise type(ex)(msg)
TypeError: With tag (0008, 0008) got exception: object of type 'NoneType' has no len()
Traceback (most recent call last):
  File "/.../pydicom/pydicom/tag.py", line 30, in tag_in_exception
    yield
  File "/.../pydicom/pydicom/filewriter.py", line 473, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File "/.../pydicom/pydicom/filewriter.py", line 384, in write_data_element
    if len(VR) != 2:
TypeError: object of type 'NoneType' has no len()
```
Probably related to the #616 PR @mrbean-bremen 


Discussion:
scaramallion: Looks good.

Patch:
diff --git a/pydicom/filewriter.py b/pydicom/filewriter.py
index 797439608..f15749508 100644
--- a/pydicom/filewriter.py
+++ b/pydicom/filewriter.py
@@ -456,6 +456,8 @@ def write_dataset(fp, dataset, parent_encoding=default_encoding):
     Attempt to correct ambiguous VR elements when explicit little/big
       encoding Elements that can't be corrected will be returned unchanged.
     """
+    _harmonize_properties(dataset, fp)
+
     if not fp.is_implicit_VR and not dataset.is_original_encoding:
         dataset = correct_ambiguous_vr(dataset, fp.is_little_endian)
 
@@ -475,6 +477,22 @@ def write_dataset(fp, dataset, parent_encoding=default_encoding):
     return fp.tell() - fpStart
 
 
+def _harmonize_properties(dataset, fp):
+    """Make sure the properties in the dataset and the file pointer are
+    consistent, so the user can set both with the same effect.
+    Properties set on the destination file object always have preference.
+    """
+    # ensure preference of fp over dataset
+    if hasattr(fp, 'is_little_endian'):
+        dataset.is_little_endian = fp.is_little_endian
+    if hasattr(fp, 'is_implicit_VR'):
+        dataset.is_implicit_VR = fp.is_implicit_VR
+
+    # write the properties back to have a consistent state
+    fp.is_implicit_VR = dataset.is_implicit_VR
+    fp.is_little_endian = dataset.is_little_endian
+
+
 def write_sequence(fp, data_element, encoding):
     """Write a dicom Sequence contained in data_element to the file fp."""
     # write_data_element has already written the VR='SQ' (if needed) and


Test patch:
diff --git a/pydicom/tests/test_filewriter.py b/pydicom/tests/test_filewriter.py
index 464d6b172..4b943d651 100644
--- a/pydicom/tests/test_filewriter.py
+++ b/pydicom/tests/test_filewriter.py
@@ -1129,6 +1129,46 @@ class TestWriteToStandard(object):
         for elem_in, elem_out in zip(ds_explicit, ds_out):
             assert elem_in == elem_out
 
+    def test_write_dataset(self):
+        # make sure writing and reading back a dataset works correctly
+        ds = dcmread(mr_implicit_name)
+        fp = DicomBytesIO()
+        write_dataset(fp, ds)
+        fp.seek(0)
+        ds_read = read_dataset(fp, is_implicit_VR=True, is_little_endian=True)
+        for elem_orig, elem_read in zip(ds_read, ds):
+            assert elem_orig == elem_read
+
+    def test_write_dataset_with_explicit_vr(self):
+        # make sure conversion from implicit to explicit VR does not
+        # raise (regression test for #632)
+        ds = dcmread(mr_implicit_name)
+        fp = DicomBytesIO()
+        fp.is_implicit_VR = False
+        fp.is_little_endian = True
+        write_dataset(fp, ds)
+        fp.seek(0)
+        ds_read = read_dataset(fp, is_implicit_VR=False, is_little_endian=True)
+        for elem_orig, elem_read in zip(ds_read, ds):
+            assert elem_orig == elem_read
+
+    def test_convert_implicit_to_explicit_vr_using_destination(self):
+        # make sure conversion from implicit to explicit VR works
+        # if setting the property in the destination
+        ds = dcmread(mr_implicit_name)
+        ds.is_implicit_VR = False
+        ds.file_meta.TransferSyntaxUID = '1.2.840.10008.1.2.1'
+        fp = DicomBytesIO()
+        fp.is_implicit_VR = False
+        fp.is_little_endian = True
+        ds.save_as(fp, write_like_original=False)
+        fp.seek(0)
+        ds_out = dcmread(fp)
+        ds_explicit = dcmread(mr_name)
+
+        for elem_in, elem_out in zip(ds_explicit, ds_out):
+            assert elem_in == elem_out
+
     def test_convert_explicit_to_implicit_vr(self):
         # make sure conversion from explicit to implicit VR works
         # without private tags