コード例 #1
0
PyObject* getFileData(int argc, char *argv[])
{
    PyObject *BioModule = PyImport_ImportModule("Bio");
    const char *filename, *filetype, *pycmdToRun;
    filename = (const char *)PyUnicode_DecodeFSDefault(argv[1]);
    filetype = (const char *)PyUnicode_DecodeFSDefault(argv[2]);
    std::string cmdToRun = "import Bio\nBio.SeqIO.parse(";
    cmdToRun = cmdToRun + filename + std::string(",") + filetype;
    pycmdToRun = cmdToRun.c_str();

    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyObject* filedata;
    filedata = PyRun_String(pycmdToRun, 0, NULL, NULL);
    Py_DECREF(filename);
    Py_DECREF(filetype);
    Py_Finalize();
    PyMem_RawFree(program);

    return filedata;
}
コード例 #2
0
ファイル: _psutil_sunos.c プロジェクト: Alren-huang/psutil
/*
 * Return process name and args as a Python tuple.
 */
static PyObject *
psutil_proc_name_and_args(PyObject *self, PyObject *args) {
    int pid;
    char path[1000];
    psinfo_t info;
    const char *procfs_path;
    PyObject *py_name;
    PyObject *py_args;

    if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
        return NULL;
    sprintf(path, "%s/%i/psinfo", procfs_path, pid);
    if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
        return NULL;

#if PY_MAJOR_VERSION >= 3
    py_name = PyUnicode_DecodeFSDefault(info.pr_fname);
    if (!py_name)
        return NULL;
    py_args = PyUnicode_DecodeFSDefault(info.pr_psargs);
    if (!py_args)
        return NULL;
    return Py_BuildValue("OO", py_name, py_args);
#else
    return Py_BuildValue("ss", info.pr_fname, info.pr_psargs);
#endif
}
コード例 #3
0
ファイル: _psutil_sunos.c プロジェクト: jomann09/psutil
/*
 * Return process name and args as a Python tuple.
 */
static PyObject *
psutil_proc_name_and_args(PyObject *self, PyObject *args) {
    int pid;
    char path[1000];
    psinfo_t info;
    const char *procfs_path;
    PyObject *py_name = NULL;
    PyObject *py_args = NULL;
    PyObject *py_retlist = NULL;

    if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
        return NULL;
    sprintf(path, "%s/%i/psinfo", procfs_path, pid);
    if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
        return NULL;

    py_name = PyUnicode_DecodeFSDefault(info.pr_fname);
    if (!py_name)
        goto error;
    py_args = PyUnicode_DecodeFSDefault(info.pr_psargs);
    if (!py_args)
        goto error;
    py_retlist = Py_BuildValue("OO", py_name, py_args);
    if (!py_retlist)
        goto error;
    Py_DECREF(py_name);
    Py_DECREF(py_args);
    return py_retlist;

error:
    Py_XDECREF(py_name);
    Py_XDECREF(py_args);
    Py_XDECREF(py_retlist);
    return NULL;
}
コード例 #4
0
ファイル: _psutil_sunos.c プロジェクト: jomann09/psutil
/*
 * Return users currently connected on the system.
 */
static PyObject *
psutil_users(PyObject *self, PyObject *args) {
    struct utmpx *ut;
    PyObject *py_tuple = NULL;
    PyObject *py_username = NULL;
    PyObject *py_tty = NULL;
    PyObject *py_hostname = NULL;
    PyObject *py_user_proc = NULL;
    PyObject *py_retlist = PyList_New(0);

    if (py_retlist == NULL)
        return NULL;

    setutxent();
    while (NULL != (ut = getutxent())) {
        if (ut->ut_type == USER_PROCESS)
            py_user_proc = Py_True;
        else
            py_user_proc = Py_False;
        py_username = PyUnicode_DecodeFSDefault(ut->ut_user);
        if (! py_username)
            goto error;
        py_tty = PyUnicode_DecodeFSDefault(ut->ut_line);
        if (! py_tty)
            goto error;
        py_hostname = PyUnicode_DecodeFSDefault(ut->ut_host);
        if (! py_hostname)
            goto error;
        py_tuple = Py_BuildValue(
            "(OOOfOi)",
            py_username,              // username
            py_tty,                   // tty
            py_hostname,              // hostname
            (float)ut->ut_tv.tv_sec,  // tstamp
            py_user_proc,             // (bool) user process
            ut->ut_pid                // process id
        );
        if (py_tuple == NULL)
            goto error;
        if (PyList_Append(py_retlist, py_tuple))
            goto error;
        Py_DECREF(py_username);
        Py_DECREF(py_tty);
        Py_DECREF(py_hostname);
        Py_DECREF(py_tuple);
    }
    endutxent();

    return py_retlist;

error:
    Py_XDECREF(py_username);
    Py_XDECREF(py_tty);
    Py_XDECREF(py_hostname);
    Py_XDECREF(py_tuple);
    Py_DECREF(py_retlist);
    endutxent();
    return NULL;
}
コード例 #5
0
ファイル: _psutil_sunos.c プロジェクト: jomann09/psutil
/*
 * Return disk mounted partitions as a list of tuples including device,
 * mount point and filesystem type.
 */
static PyObject *
psutil_disk_partitions(PyObject *self, PyObject *args) {
    FILE *file;
    struct mnttab mt;
    PyObject *py_dev = NULL;
    PyObject *py_mountp = NULL;
    PyObject *py_tuple = NULL;
    PyObject *py_retlist = PyList_New(0);

    if (py_retlist == NULL)
        return NULL;

    file = fopen(MNTTAB, "rb");
    if (file == NULL) {
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    while (getmntent(file, &mt) == 0) {
        py_dev = PyUnicode_DecodeFSDefault(mt.mnt_special);
        if (! py_dev)
            goto error;
        py_mountp = PyUnicode_DecodeFSDefault(mt.mnt_mountp);
        if (! py_mountp)
            goto error;
        py_tuple = Py_BuildValue(
            "(OOss)",
            py_dev,           // device
            py_mountp,        // mount point
            mt.mnt_fstype,    // fs type
            mt.mnt_mntopts);  // options
        if (py_tuple == NULL)
            goto error;
        if (PyList_Append(py_retlist, py_tuple))
            goto error;
        Py_DECREF(py_dev);
        Py_DECREF(py_mountp);
        Py_DECREF(py_tuple);
    }
    fclose(file);
    return py_retlist;

error:
    Py_XDECREF(py_dev);
    Py_XDECREF(py_mountp);
    Py_XDECREF(py_tuple);
    Py_DECREF(py_retlist);
    if (file != NULL)
        fclose(file);
    return NULL;
}
コード例 #6
0
ファイル: grpmodule.c プロジェクト: Anzumana/cpython
static PyObject *
mkgrent(struct group *p)
{
    int setIndex = 0;
    PyObject *v = PyStructSequence_New(&StructGrpType), *w;
    char **member;

    if (v == NULL)
        return NULL;

    if ((w = PyList_New(0)) == NULL) {
        Py_DECREF(v);
        return NULL;
    }
    for (member = p->gr_mem; *member != NULL; member++) {
        PyObject *x = PyUnicode_DecodeFSDefault(*member);
        if (x == NULL || PyList_Append(w, x) != 0) {
            Py_XDECREF(x);
            Py_DECREF(w);
            Py_DECREF(v);
            return NULL;
        }
        Py_DECREF(x);
    }

#define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
    SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name));
#ifdef __VMS
    SET(setIndex++, Py_None);
    Py_INCREF(Py_None);
#else
    if (p->gr_passwd)
            SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd));
    else {
            SET(setIndex++, Py_None);
            Py_INCREF(Py_None);
    }
#endif
    SET(setIndex++, PyLong_FromLong((long) p->gr_gid));
    SET(setIndex++, w);
#undef SET

    if (PyErr_Occurred()) {
        Py_DECREF(v);
        return NULL;
    }

    return v;
}
コード例 #7
0
ファイル: py-ext-backend.c プロジェクト: 20centaurifux/efind
static void
_py_append_local_extension_path(PyObject *path)
{
	assert(path != NULL);
	assert(PyList_Check(path));

	char localpath[PATH_MAX];

	if(path_builder_local_extensions(localpath, PATH_MAX))
	{
		DEBUGF("python", "Appending local extension directory to Python path: %s", localpath);

		PyObject *location = PyUnicode_DecodeFSDefault(localpath);

		if(location)
		{
			PyList_Append(path, location);
		}
		else
		{
			fprintf(stderr, "Invalid encoding: %s\n", localpath);
		}
	}
	else
	{
		WARNING("python", "Couldn't build local extension path.");
	}
}
コード例 #8
0
ファイル: mlabraw.cpp プロジェクト: Answeror/mlabwrap
// FIXME: add string array support
static PyObject *mx2char(const mxArray *pArray)
{
  size_t buflen;
  char *buf;
  PyObject *lRetval;
  if (mxGetM(pArray) > 1) {
    PyErr_SetString(mlabraw_error, "Only 1 Dimensional strings are currently supported");
    return NULL;
  }
  buflen = mxGetN(pArray) + 1;

  //buf = (char *)mxCalloc(buflen, sizeof(char));
  //pyassert(buf, "Out of MATLAB(TM) memory");
  //if (mxGetString(pArray, buf, buflen)) {
    //PyErr_SetString(mlabraw_error, "Unable to extract MATLAB(TM) string");
    //mxFree(buf);
    //return NULL;
  //}

  buf = mxArrayToString(pArray);
  if (!buf) {
    PyErr_SetString(mlabraw_error, "Unable to extract MATLAB(TM) string");
    mxFree(buf);
    return NULL;
  }

#ifdef PY3K
  lRetval = PyUnicode_DecodeFSDefault(buf);
#else
  lRetval = (PyStringObject *)PyString_FromString(buf);
#endif
  mxFree(buf);
  return lRetval;
	error_return: return NULL;
}
コード例 #9
0
ファイル: bpy.c プロジェクト: BHCLL/blendocv
// PyDoc_STRVAR(bpy_user_resource_doc[]= // now in bpy/utils.py
static PyObject *bpy_user_resource(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
{
	char *type;
	char *subdir= NULL;
	int folder_id;
	static const char *kwlist[]= {"type", "subdir", NULL};

	char *path;

	if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:user_resource", (char **)kwlist, &type, &subdir))
		return NULL;
	
	/* stupid string compare */
	if     (!strcmp(type, "DATAFILES")) folder_id= BLENDER_USER_DATAFILES;
	else if (!strcmp(type, "CONFIG"))   folder_id= BLENDER_USER_CONFIG;
	else if (!strcmp(type, "SCRIPTS"))  folder_id= BLENDER_USER_SCRIPTS;
	else if (!strcmp(type, "AUTOSAVE")) folder_id= BLENDER_USER_AUTOSAVE;
	else {
		PyErr_SetString(PyExc_ValueError, "invalid resource argument");
		return NULL;
	}
	
	/* same logic as BLI_get_folder_create(), but best leave it up to the script author to create */
	path= BLI_get_folder(folder_id, subdir);

	if (!path)
		path= BLI_get_user_folder_notest(folder_id, subdir);

	return PyUnicode_DecodeFSDefault(path ? path : "");
}
コード例 #10
0
ファイル: GenethonDoc.cpp プロジェクト: karlnicholas/GeneThon
PyObject*  CGenethonDoc::setPythonPath(CString& fileName) {
    CString filePath;
    CString appName;
    SplitFilename(fileName, &filePath, &appName);

    PyObject* sysPath = PySys_GetObject("path");
    PyObject* pyPath = PyUnicode_FromString(filePath);
    PyList_Append(sysPath, pyPath);
    Py_DECREF(pyPath);
    pyPath = PyUnicode_FromString("c:/users/karl/Anaconda3");
    PyList_Append(sysPath, pyPath);
    Py_DECREF(pyPath);
    pyPath = PyUnicode_FromString("c:/users/karl/Anaconda3/Lib");
    PyList_Append(sysPath, pyPath);
    Py_DECREF(pyPath);
    pyPath = PyUnicode_FromString("c:/users/karl/Anaconda3/Dlls");
    PyList_Append(sysPath, pyPath);
    Py_DECREF(pyPath);
    pyPath = PyUnicode_FromString("c:/users/karl/Anaconda3/Lib/site-packages");
    PyList_Append(sysPath, pyPath);
    Py_DECREF(pyPath);

    PyObject*pName = PyUnicode_DecodeFSDefault(appName);
    PyObject*pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    return pModule;
}
コード例 #11
0
ファイル: package.c プロジェクト: vadmium/pyalpm
static PyObject* pyalpm_package_get_files(AlpmPackage *self, void *closure) {
  const alpm_filelist_t *flist = NULL;
  PyObject *result = NULL;

  CHECK_IF_INITIALIZED();

  flist = alpm_pkg_get_files(self->c_data);
  if (!flist)
    Py_RETURN_NONE;
  else {
    ssize_t i;
    result = PyList_New((Py_ssize_t)flist->count);
    for (i = 0; i < (ssize_t)flist->count; i++) {
      const alpm_file_t *file = flist->files + i;
      PyObject *filename = PyUnicode_DecodeFSDefault(file->name);
      PyObject *filesize = PyLong_FromLongLong(file->size);
      PyObject *filemode = PyLong_FromUnsignedLong(file->mode);
      PyObject *item = PyTuple_New(3);
      if (item && filename && filesize && filemode) {
        PyTuple_SET_ITEM(item, 0, filename);
        PyTuple_SET_ITEM(item, 1, filesize);
        PyTuple_SET_ITEM(item, 2, filemode);
        PyList_SET_ITEM(result, i, item);
      } else {
        Py_CLEAR(item);
        Py_CLEAR(filename);
        Py_CLEAR(filesize);
        Py_CLEAR(filemode);
        return NULL;
      }
    }
  }
  return result;
}
コード例 #12
0
ファイル: _warnings.c プロジェクト: PiJoules/cpython-modified
int
PyErr_WarnExplicit(PyObject *category, const char *text,
                   const char *filename_str, int lineno,
                   const char *module_str, PyObject *registry)
{
    PyObject *message = PyUnicode_FromString(text);
    PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
    PyObject *module = NULL;
    int ret = -1;

    if (message == NULL || filename == NULL)
        goto exit;
    if (module_str != NULL) {
        module = PyUnicode_FromString(module_str);
        if (module == NULL)
            goto exit;
    }

    ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
                                   module, registry);

 exit:
    Py_XDECREF(message);
    Py_XDECREF(module);
    Py_XDECREF(filename);
    return ret;
}
コード例 #13
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
PyObject *
PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
                  PyObject *locals, int closeit, PyCompilerFlags *flags)
{
    PyObject *ret = NULL;
    mod_ty mod;
    PyArena *arena = NULL;
    PyObject *filename;

    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL)
        goto exit;

    arena = PyArena_New();
    if (arena == NULL)
        goto exit;

    mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
                                     flags, NULL, arena);
    if (closeit)
        fclose(fp);
    if (mod == NULL) {
        goto exit;
    }
    ret = run_mod(mod, filename, globals, locals, flags, arena);

exit:
    Py_XDECREF(filename);
    if (arena != NULL)
        PyArena_Free(arena);
    return ret;
}
コード例 #14
0
ファイル: openbsd.c プロジェクト: Zyalia/psutil
// returns the command line as a python list object
PyObject *
psutil_get_cmdline(long pid) {
    static char **argv;
    char **p;
    PyObject *py_arg = NULL;
    PyObject *py_retlist = Py_BuildValue("[]");

    if (!py_retlist)
        return NULL;
    if (pid < 0)
        return py_retlist;

    if ((argv = _psutil_get_argv(pid)) == NULL)
        goto error;

    for (p = argv; *p != NULL; p++) {
#if PY_MAJOR_VERSION >= 3
        py_arg = PyUnicode_DecodeFSDefault(*p);
#else
        py_arg = Py_BuildValue("s", *p);
#endif
        if (!py_arg)
            goto error;
        if (PyList_Append(py_retlist, py_arg))
            goto error;
        Py_DECREF(py_arg);
    }
    return py_retlist;

error:
    Py_XDECREF(py_arg);
    Py_DECREF(py_retlist);
    return NULL;
}
コード例 #15
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
static int
set_main_loader(PyObject *d, const char *filename, const char *loader_name)
{
    PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
    int result = 0;

    filename_obj = PyUnicode_DecodeFSDefault(filename);
    if (filename_obj == NULL)
        return -1;
    PyInterpreterState *interp = _PyInterpreterState_Get();
    bootstrap = PyObject_GetAttrString(interp->importlib,
                                       "_bootstrap_external");
    if (bootstrap != NULL) {
        loader_type = PyObject_GetAttrString(bootstrap, loader_name);
        Py_DECREF(bootstrap);
    }
    if (loader_type == NULL) {
        Py_DECREF(filename_obj);
        return -1;
    }
    loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
    Py_DECREF(loader_type);
    if (loader == NULL) {
        return -1;
    }
    if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
        result = -1;
    }
    Py_DECREF(loader);
    return result;
}
コード例 #16
0
ファイル: openbsd.c プロジェクト: Zyalia/psutil
PyObject *
psutil_proc_cwd(PyObject *self, PyObject *args) {
    // Reference:
    // http://anoncvs.spacehopper.org/openbsd-src/tree/bin/ps/print.c#n179
    long pid;
    struct kinfo_proc kp;
    char path[MAXPATHLEN];
    size_t pathlen = sizeof path;

    if (! PyArg_ParseTuple(args, "l", &pid))
        return NULL;
    if (psutil_kinfo_proc(pid, &kp) == -1)
        return NULL;

    int name[] = { CTL_KERN, KERN_PROC_CWD, pid };
    if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
#if PY_MAJOR_VERSION >= 3
    return PyUnicode_DecodeFSDefault(path);
#else
    return Py_BuildValue("s", path);
#endif
}
コード例 #17
0
ファイル: errors.c プロジェクト: cpcloud/cpython
PyObject *
PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
{
    PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
    PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
    Py_XDECREF(name);
    return result;
}
コード例 #18
0
void jieba_init(void)
{
	void *res;
	Py_Initialize();

	if (!Py_IsInitialized()) {
		printf("Py_Initialize() fails.\n");
		assert(0);
	}

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");

	/* Decode a string using Py_FileSystemDefaultEncoding */
	jieba_wrap_nm = PyUnicode_DecodeFSDefault(JIEBA_WRAP_NAME);

	jieba_module = PyImport_Import(jieba_wrap_nm); /* new ref */
	if (jieba_module == NULL) {
		printf("PyImport_Import() fails.\n");
		goto free;
	}

	jieba_init_func = PyObject_GetAttrString(jieba_module,
			JIEBA_WRAP_INIT_FUN); /* new ref */
	if (jieba_init_func == NULL) {
		printf("PyObject_GetAttrString() fails.\n");

		jieba_release();
		jieba_module = NULL;
	}

	assert(1 == PyCallable_Check(jieba_init_func));

	/* call jieba_init_func in Python */
	res = PyObject_CallObject(jieba_init_func, NULL); /* new ref */
	Py_DECREF(res);
	Py_DECREF(jieba_init_func);

free:
	/* should free obj returned by PyUnicode_DecodeFSDefault(), see #1.3:
	 * http://python.readthedocs.org/en/latest/extending/embedding.html */
	Py_DECREF(jieba_wrap_nm);

	assert(jieba_module != NULL);

	goto invoke_lazy_dict_load; /* uncomment if you do not want this */
	return;

invoke_lazy_dict_load:
	res = jieba_cut("初始化test", strlen("初始化test"));
	if (res) {
		foreach_tok(res, &token_donothing, NULL);

		// PRINT_REF_CNT(res);
		Py_DECREF(res);
	}
}
コード例 #19
0
/* after code that pyrex generates */
void _ctypes_add_traceback(char *funcname, char *filename, int lineno)
{
	PyObject *py_srcfile = 0;
	PyObject *py_funcname = 0;
	PyObject *py_globals = 0;
	PyObject *empty_tuple = 0;
	PyObject *empty_string = 0;
	PyCodeObject *py_code = 0;
	PyFrameObject *py_frame = 0;
    
	py_srcfile = PyUnicode_DecodeFSDefault(filename);
	if (!py_srcfile) goto bad;
	py_funcname = PyUnicode_FromString(funcname);
	if (!py_funcname) goto bad;
	py_globals = PyDict_New();
	if (!py_globals) goto bad;
	empty_tuple = PyTuple_New(0);
	if (!empty_tuple) goto bad;
	empty_string = PyBytes_FromString("");
	if (!empty_string) goto bad;
	py_code = PyCode_New(
		0,            /*int argcount,*/
		0,            /*int kwonlyargcount,*/
		0,            /*int nlocals,*/
		0,            /*int stacksize,*/
		0,            /*int flags,*/
		empty_string, /*PyObject *code,*/
		empty_tuple,  /*PyObject *consts,*/
		empty_tuple,  /*PyObject *names,*/
		empty_tuple,  /*PyObject *varnames,*/
		empty_tuple,  /*PyObject *freevars,*/
		empty_tuple,  /*PyObject *cellvars,*/
		py_srcfile,   /*PyObject *filename,*/
		py_funcname,  /*PyObject *name,*/
		lineno,   /*int firstlineno,*/
		empty_string  /*PyObject *lnotab*/
		);
	if (!py_code) goto bad;
	py_frame = PyFrame_New(
		PyThreadState_Get(), /*PyThreadState *tstate,*/
		py_code,             /*PyCodeObject *code,*/
		py_globals,          /*PyObject *globals,*/
		0                    /*PyObject *locals*/
		);
	if (!py_frame) goto bad;
	py_frame->f_lineno = lineno;
	PyTraceBack_Here(py_frame);
  bad:
	Py_XDECREF(py_globals);
	Py_XDECREF(py_srcfile);
	Py_XDECREF(py_funcname);
	Py_XDECREF(empty_tuple);
	Py_XDECREF(empty_string);
	Py_XDECREF(py_code);
	Py_XDECREF(py_frame);
}
コード例 #20
0
ファイル: spwdmodule.c プロジェクト: Orav/kbengine
static void
sets(PyObject *v, int i, const char* val)
{
  if (val) {
      PyObject *o = PyUnicode_DecodeFSDefault(val);
      PyStructSequence_SET_ITEM(v, i, o);
  } else {
      PyStructSequence_SET_ITEM(v, i, Py_None);
      Py_INCREF(Py_None);
  }
}
コード例 #21
0
ファイル: netbsd.c プロジェクト: xiaobona/psutil
// XXX: This is no longer used as per
// https://github.com/giampaolo/psutil/pull/557#issuecomment-171912820
// Current implementation uses /proc instead.
// Left here just in case.
PyObject *
psutil_proc_exe(PyObject *self, PyObject *args) {
#if __NetBSD_Version__ >= 799000000
    pid_t pid;
    char pathname[MAXPATHLEN];
    int error;
    int mib[4];
    int ret;
    size_t size;

    if (! PyArg_ParseTuple(args, "l", &pid))
        return NULL;
    if (pid == 0) {
        // else returns ENOENT
        return Py_BuildValue("s", "");
    }

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC_ARGS;
    mib[2] = pid;
    mib[3] = KERN_PROC_PATHNAME;

    size = sizeof(pathname);
    error = sysctl(mib, 4, NULL, &size, NULL, 0);
    if (error == -1) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }

    error = sysctl(mib, 4, pathname, &size, NULL, 0);
    if (error == -1) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
    if (size == 0 || strlen(pathname) == 0) {
        ret = psutil_pid_exists(pid);
        if (ret == -1)
            return NULL;
        else if (ret == 0)
            return NoSuchProcess();
        else
            strcpy(pathname, "");
    }

#if PY_MAJOR_VERSION >= 3
    return PyUnicode_DecodeFSDefault(pathname);
#else
    return Py_BuildValue("s", pathname);
#endif

#else
    return Py_BuildValue("s", "");
#endif
}
コード例 #22
0
ファイル: errors.c プロジェクト: cpcloud/cpython
PyObject *PyErr_SetFromWindowsErrWithFilename(
    int ierr,
    const char *filename)
{
    PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
    PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
                                                  PyExc_OSError,
                                                  ierr, name, NULL);
    Py_XDECREF(name);
    return result;
}
コード例 #23
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
PyObject *
Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
                        PyCompilerFlags *flags, int optimize)
{
    PyObject *filename, *co;
    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL)
        return NULL;
    co = Py_CompileStringObject(str, filename, start, flags, optimize);
    Py_DECREF(filename);
    return co;
}
コード例 #24
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
mod_ty
PyParser_ASTFromString(const char *s, const char *filename_str, int start,
                       PyCompilerFlags *flags, PyArena *arena)
{
    PyObject *filename;
    mod_ty mod;
    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL)
        return NULL;
    mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
    Py_DECREF(filename);
    return mod;
}
コード例 #25
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
struct symtable *
Py_SymtableString(const char *str, const char *filename_str, int start)
{
    PyObject *filename;
    struct symtable *st;

    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL)
        return NULL;
    st = Py_SymtableStringObject(str, filename, start);
    Py_DECREF(filename);
    return st;
}
コード例 #26
0
ファイル: future.c プロジェクト: JamMarHer/RepairBox
PyFutureFeatures *
PyFuture_FromAST(mod_ty mod, const char *filename_str)
{
    PyFutureFeatures *ff;
    PyObject *filename;

    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL)
        return NULL;
    ff = PyFuture_FromASTObject(mod, filename);
    Py_DECREF(filename);
    return ff;
}
コード例 #27
0
ファイル: freebsd.c プロジェクト: Zyalia/psutil
PyObject *
psutil_proc_cwd(PyObject *self, PyObject *args) {
    long pid;
    struct kinfo_file *freep = NULL;
    struct kinfo_file *kif;
    struct kinfo_proc kipp;
    const char *encoding_errs;
    PyObject *py_path = NULL;

    int i, cnt;

    if (! PyArg_ParseTuple(args, "l", &pid))
        goto error;
    if (psutil_kinfo_proc(pid, &kipp) == -1)
        goto error;

    freep = kinfo_getfile(pid, &cnt);
    if (freep == NULL) {
        psutil_raise_ad_or_nsp(pid);
        goto error;
    }

    for (i = 0; i < cnt; i++) {
        kif = &freep[i];
        if (kif->kf_fd == KF_FD_TYPE_CWD) {
#if PY_MAJOR_VERSION >= 3
            py_path = PyUnicode_DecodeFSDefault(kif->kf_path);
#else
            py_path = Py_BuildValue("s", kif->kf_path);
#endif
            if (!py_path)
                goto error;
            break;
        }
    }
    /*
     * For lower pids it seems we can't retrieve any information
     * (lsof can't do that it either).  Since this happens even
     * as root we return an empty string instead of AccessDenied.
     */
    if (py_path == NULL)
        py_path = Py_BuildValue("s", "");
    free(freep);
    return py_path;

error:
    Py_XDECREF(py_path);
    if (freep != NULL)
        free(freep);
    return NULL;
}
コード例 #28
0
ファイル: GameEngineC.cpp プロジェクト: Cethric/GameEngineC
bool GameEngineC::loadPython(void) {
    Py_SetProgramName((wchar_t *) "GameEngineC Python");
    Py_Initialize();
    PyObject *pName, *pModule, *pDict, *pFunct;
    PyObject *pArgs, *pValue;
    pName = PyUnicode_DecodeFSDefault("this will fail");
    pModule = PyImport_Import(pName);
    PyErr_Print();
    PyRun_SimpleString("print('hello world from python');print(__name__)\n");
    if (onPythonLoad) {
        onPythonLoad();
    }
    return true;
}
コード例 #29
0
ファイル: errors.c プロジェクト: cpcloud/cpython
void
PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
{
    PyObject *fileobj;
    if (filename != NULL) {
        fileobj = PyUnicode_DecodeFSDefault(filename);
        if (fileobj == NULL)
            PyErr_Clear();
    }
    else
        fileobj = NULL;
    PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
    Py_XDECREF(fileobj);
}
コード例 #30
0
ファイル: pythonrun.c プロジェクト: tiran/cpython
int
PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
{
    PyObject *filename;
    int res;

    filename = PyUnicode_DecodeFSDefault(filename_str);
    if (filename == NULL) {
        PyErr_Print();
        return -1;
    }
    res = PyRun_InteractiveOneObject(fp, filename, flags);
    Py_DECREF(filename);
    return res;
}