1. ariovistus
  2. pyd
Issue #11 resolved

Error: undefined identifier PyDateTimeAPI, did you mean struct PyDateTime_CAPI?

Ivan Smirnov
created an issue

Hi, I'm getting this sort of errors when trying to construct a datetime object from within D:

/celerid/infrastructure/deimos/python/datetime.d(266): Error: undefined identifier PyDateTimeAPI, did you mean struct PyDateTime_CAPI?
/celerid/infrastructure/deimos/python/datetime.d(267): Error: undefined identifier PyDateTimeAPI, did you mean struct PyDateTime_CAPI?
/celerid/infrastructure/deimos/python/datetime.d(267): Error: not a property Py_None()()
pyd_test_module.d(13): Error: template instance deimos.python.datetime.PyDateTime_FromDateAndTime!() error instantiating
def: hello
error: command 'dmd' failed with exit status 1

Did the datetime headers containing import/macros get mapped to D incorrectly or is it wrong use on my side? (I'm running DMD v2.063.2 and Python 2.7.5 on 64-bit opensuse Linux).

Note also the "Error: not a property Py_None()()"?

Thanks!

Here's the minimal D code:

module pyd_test_module;

import pyd.pyd;
import deimos.python.Python;
import std.stdio;

PyObject* hello()
{
    return PyDateTime_FromDateAndTime!()(2010, 2, 3, 4, 5, 6, 7);
}

extern(C) void PydMain()
{
    PyDateTime_IMPORT();
    def!(hello)();
    module_init();
}

Update: The problem is solved if you move PyDateTimeAPI to the global scope, otherwise it can't be seen by the other macros:

*** datetime.d.orig     2013-10-15 17:11:37.001852000 +0100
--- datetime.d  2013-10-15 17:11:51.002727000 +0100
*************** struct PyDateTime_CAPI {
*** 206,213 ****
  enum DATETIME_API_MAGIC = 0x414548d5;

  /// _
  PyDateTime_CAPI* PyDateTime_IMPORT()() {
-     static PyDateTime_CAPI* PyDateTimeAPI;
      if (PyDateTimeAPI == null) {
          PyDateTimeAPI = cast(PyDateTime_CAPI *)
              PyCObject_Import("datetime", "datetime_CAPI");
--- 206,213 ----
  enum DATETIME_API_MAGIC = 0x414548d5;

  /// _
+ static PyDateTime_CAPI* PyDateTimeAPI;
  PyDateTime_CAPI* PyDateTime_IMPORT()() {
      if (PyDateTimeAPI == null) {
          PyDateTimeAPI = cast(PyDateTime_CAPI *)
              PyCObject_Import("datetime", "datetime_CAPI");

Comments (4)

  1. Ivan Smirnov reporter

    In datetime.d:

    PyDateTime_CAPI* PyDateTime_IMPORT()() {
        static PyDateTime_CAPI* PyDateTimeAPI;
        if (PyDateTimeAPI == null) {
            PyDateTimeAPI = cast(PyDateTime_CAPI *)
                PyCObject_Import("datetime", "datetime_CAPI");
        }
        return PyDateTimeAPI;
    }
    
    ...
    
    PyObject* PyDateTime_FromDateAndTime()(int year, int month, int day, int hour, int min, int sec, int usec) {
        return PyDateTimeAPI.DateTime_FromDateAndTime(year, month, day, hour,
                min, sec, usec, Py_None, PyDateTimeAPI.DateTimeType);
    

    In original datetime.h:

    static PyDateTime_CAPI *PyDateTimeAPI = NULL;
    
    #define PyDateTime_IMPORT \
        PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
    
    ...
    
    #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
        PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
            min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
    
  2. Log in to comment