Ignore:
Timestamp:
03/07/12 20:15:51 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
rebase_source:
78ee9b1644489e06468a72a9633b10cf42a7ef66
Message:

Accept unicode objects in sys.getfilesystemencoding() as filenames to Table save/load functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/orange/lib_io.cpp

    r10220 r10460  
    3838PyObject *encodeStatus(const vector<int> &Status);  // in cls_misc.cpp 
    3939PyObject *encodeStatus(const vector<pair<int, int> > &metaStatus); 
     40 
     41 
     42/* Same as sys.getfilesystemencoding() 
     43 * (the returned pointer points to a PyString_Object internal buffer 
     44 * and should not be modified). 
     45 */ 
     46char* getFileSystemEncoding() 
     47{ 
     48    PyObject *fsencoding = PySys_GetObject("getfilesystemencoding"); // Borrowed ref 
     49    fsencoding = PyObject_CallObject(fsencoding, NULL); // This should be a string. 
     50    assert(PyString_Check(fsencoding)); 
     51    return PyString_AsString(fsencoding); 
     52} 
    4053 
    4154/* ************ FILE EXAMPLE GENERATORS ************ */ 
     
    216229{ PyTRY 
    217230    char *filename; 
     231    bool free_filename = false; 
    218232    PExampleGenerator gen; 
    219233 
    220234    if (!PyArg_ParseTuple(args, "sO&", &filename, pt_ExampleGenerator, &gen)) 
    221       PYERROR(PyExc_TypeError, "string and example generator expected", PYNULL); 
     235    { 
     236        char *encoding = getFileSystemEncoding(); 
     237        if (!PyArg_ParseTuple(args, "esO&", encoding, &filename, pt_ExampleGenerator, &gen)) 
     238            PYERROR(PyExc_TypeError, "string and example generator expected", PYNULL); 
     239        PyErr_Clear(); 
     240        free_filename = true; 
     241    } 
    222242 
    223243    if (skipAttrTypes && !gen->domain->classVar) { 
    224244      PyErr_Format(PyExc_TypeError, "Format .%s cannot save classless data sets", defaultExtension); 
     245      if (free_filename) 
     246          PyMem_Free(filename); 
    225247      return PYNULL; 
    226248    } 
     
    228250    char *DK = NULL, *DC = NULL; 
    229251    if (!readUndefinedSpecs(keyws, DK, DC)) 
    230       return PYNULL; 
     252    { 
     253      if (free_filename) 
     254          PyMem_Free(filename); 
     255      return PYNULL; 
     256    } 
    231257   
    232258    FILE *ostr = openExtended(filename, defaultExtension); 
    233259    if (!ostr) 
    234       return PYNULL; 
    235  
     260    { 
     261      if (free_filename) 
     262          PyMem_Free(filename); 
     263      return PYNULL; 
     264    } 
     265 
     266    if (free_filename) 
     267        PyMem_Free(filename); 
    236268    tabDelim_writeDomain(ostr, gen->domain, skipAttrTypes, delim, listDiscreteValues); 
    237269    tabDelim_writeExamples(ostr, gen, delim, DK, DC); 
     
    317349{ PyTRY 
    318350    char *filename; 
     351    bool free_filename = false; 
    319352    PExampleGenerator gen; 
    320353 
    321354    if (!PyArg_ParseTuple(args, "sO&:saveBasket", &filename, pt_ExampleGenerator, &gen)) 
    322       return PYNULL; 
     355    { 
     356      char *encoding = getFileSystemEncoding(); 
     357      if (!PyArg_ParseTuple(args, "esO&:saveBasket", encoding, &filename, pt_ExampleGenerator, &gen)) 
     358          return PYNULL; 
     359      PyErr_Clear(); 
     360      free_filename = true; 
     361    } 
    323362 
    324363    if (gen->domain->variables->size()) 
     364    { 
     365      if (free_filename) 
     366          PyMem_Free(filename); 
    325367      PYERROR(PyExc_TypeError, ".basket format can only store meta-attribute values", PYNULL); 
     368    } 
    326369 
    327370    FILE *ostr = openExtended(filename, "basket"); 
     371 
    328372    if (!ostr) 
    329       return PYNULL; 
     373    { 
     374      if (free_filename) 
     375          PyMem_Free(filename); 
     376      return PYNULL; 
     377    } 
    330378 
    331379    set<int> missing; 
     
    337385      fclose(ostr); 
    338386      remove(filename); 
     387      if (free_filename) 
     388          PyMem_Free(filename); 
    339389      throw; 
    340390    } 
    341391 
    342392    fclose(ostr); 
     393 
     394    if (free_filename) 
     395        PyMem_Free(filename); 
    343396 
    344397    if (!missing.empty()) { 
Note: See TracChangeset for help on using the changeset viewer.