Changeset 10460:f2b41eb984eb in orange


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.

Location:
source/orange
Files:
2 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()) { 
  • source/orange/lib_kernel.cpp

    r10357 r10460  
    27352735 
    27362736const char *getExtension(const char *name); 
     2737char *getFileSystemEncoding(); // defined in lib_io.cpp 
    27372738 
    27382739PyObject *saveTabDelimited(PyObject *, PyObject *args, PyObject *keyws); 
     
    27452746{ 
    27462747  char *filename; 
     2748  bool free_filename = false; 
    27472749  if (!PyArg_ParseTuple(args, "s:ExampleGenerator.save", &filename)) 
    2748     return PYNULL; 
     2750  { 
     2751      // Try again, this time with the fs encoding. 
     2752      char *encoding = getFileSystemEncoding(); 
     2753      if (!PyArg_ParseTuple(args, "es:ExampleGenerator.save", encoding, &filename)) 
     2754          return PYNULL; 
     2755      free_filename = true; 
     2756      PyErr_Clear(); 
     2757  } 
    27492758 
    27502759  const char *extension = getExtension(filename); 
    27512760  if (!extension) 
     2761  { 
     2762    if (free_filename) 
     2763        PyMem_Free(filename); 
    27522764    PYERROR(PyExc_TypeError, "file name must have an extension", PYNULL); 
     2765  } 
    27532766 
    27542767 
     
    27882801 
    27892802  Py_DECREF(newargs); 
     2803 
     2804  if (free_filename) 
     2805      PyMem_Free(filename); 
     2806 
    27902807  return res; 
    27912808} 
     
    30573074} 
    30583075 
    3059  
    30603076CONSTRUCTOR_KEYWORDS(ExampleTable, "domain use useMetas dontCheckStored dontStore filterMetas filter_metas DC DK NA noClass noCodedDiscrete createNewOn") 
    30613077 
     
    30663082    char *filename = NULL; 
    30673083    if (PyArg_ParseTuple(argstuple, "s", &filename)) 
    3068       return loadDataFromFile(type, filename, argstuple, keywords, false); 
     3084        return loadDataFromFile(type, filename, argstuple, keywords, false); 
     3085 
     3086    PyErr_Clear(); 
     3087 
     3088    /*For a case where the unicode can't be converted to a default 
     3089     * encoding (on most platforms this is ASCII) 
     3090     */ 
     3091    char * coding = getFileSystemEncoding(); 
     3092    if (PyArg_ParseTuple(argstuple, "es", coding, &filename)) 
     3093    { 
     3094        PyObject *rval = loadDataFromFile(type, filename, argstuple, keywords, false); 
     3095        PyMem_Free(filename); 
     3096        return rval; 
     3097    } 
    30693098 
    30703099    PyErr_Clear(); 
Note: See TracChangeset for help on using the changeset viewer.